@@ -256,7 +256,7 @@ Go语言中通过net包中的`DialTCP`函数来建立一个TCP连接,并返回
|
||||
|
||||
func handleClient(conn net.Conn) {
|
||||
conn.SetReadDeadline(time.Now().Add(2 * time.Minute)) // set 2 minutes timeout
|
||||
request := make([]byte, 128) // set maxium request length to 128KB to prevent flood attack
|
||||
request := make([]byte, 128) // set maxium request length to 128B to prevent flood attack
|
||||
defer conn.Close() // close connection before exit
|
||||
for {
|
||||
read_len, err := conn.Read(request)
|
||||
@@ -273,7 +273,7 @@ Go语言中通过net包中的`DialTCP`函数来建立一个TCP连接,并返回
|
||||
conn.Write([]byte(daytime))
|
||||
} else {
|
||||
daytime := time.Now().String()
|
||||
conn.Write([]byte(daytime))
|
||||
conn.Write([]byte(daytime))
|
||||
}
|
||||
|
||||
request = make([]byte, 128) // clear last read content
|
||||
@@ -298,7 +298,7 @@ TCP有很多连接控制函数,我们平常用到比较多的有如下几个
|
||||
|
||||
func (c *TCPConn) SetReadDeadline(t time.Time) error
|
||||
func (c *TCPConn) SetWriteDeadline(t time.Time) error
|
||||
|
||||
|
||||
用来设置写入/读取一个连接的超时时间。当超过设置时间时,连接自动关闭。
|
||||
|
||||
func (c *TCPConn) SetKeepAlive(keepalive bool) os.Error
|
||||
@@ -312,7 +312,7 @@ Go语言包中处理UDP Socket和TCP Socket不同的地方就是在服务器端
|
||||
func ResolveUDPAddr(net, addr string) (*UDPAddr, os.Error)
|
||||
func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err os.Error)
|
||||
func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err os.Error)
|
||||
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error
|
||||
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error)
|
||||
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err os.Error)
|
||||
|
||||
一个UDP的客户端代码如下所示,我们可以看到不同的就是TCP换成了UDP而已:
|
||||
|
||||
Reference in New Issue
Block a user