我是Go的新手,作为我的第一个测试项目之一,我想编写一个使用UDP的简单客户端/服务器程序。我得到了它的工作,但有很多方法可以做到这一点,我想知道哪种方式最好。
net.Listen()vs net.ListenUDP()vs net.ListenPacket()
net.Read()vs net.ReadFrom()vs net.ReadFromUDP()
net.Write()vs net.WriteTo()vs net.WriteToUDP()
答案 0 :(得分:20)
让我们检查一下你的问题。
Listen在本地网络地址laddr上发布。网络网络必须是面向流的网络:“tcp”,“tcp4”,“tcp6”,“unix”或“unixpacket”。有关laddr的语法,请参阅拨号。
用法示例#1:
tcpSock, err := net.Listen("tcp", "0.0.0.0:8080")
用法示例#2
unixSock, err := net.Listen("unix", "/var/app/server.sock")
我们可以看到in the source net.Listen()
作为调用net.ListenTCP
或net.ListenUnix
的switch语句或默认错误:
func Listen(net, laddr string) (Listener, error) {
la, err := resolveAddr("listen", net, laddr, noDeadline)
if err != nil {
return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: err}
}
var l Listener
switch la := la.toAddr().(type) {
case *TCPAddr:
l, err = ListenTCP(net, la)
case *UnixAddr:
l, err = ListenUnix(net, la)
default:
return nil, &OpError{
Op: "listen",
Net: net,
Addr: la,
Err: &AddrError{Err: "unexpected address type", Addr: laddr},
}
}
if err != nil {
return nil, err // l is non-nil interface containing nil pointer
}
return l, nil
}
的其他信息
那么,我们可以从初始比较中消除net.ListenUDP
;并查看net.ListenPacket()
。
ListenPacket在本地网络地址laddr上宣布。网络网必须是面向分组的网络:“udp”,“udp4”,“udp6”,“ip”,“ip4”,“ip6”或“unixgram”。有关laddr的语法,请参阅拨号。
用法示例#1:
pListener, err := net.ListenPacket("ip4", "0.0.0.0:9090")
而且,如果我们深入了解,我们可以看到它的运作方式与net.Listen()
非常相似:
func ListenPacket(net, laddr string) (PacketConn, error) {
la, err := resolveAddr("listen", net, laddr, noDeadline)
if err != nil {
return nil, &OpError{Op: "listen", Net: net, Addr: nil, Err: err}
}
var l PacketConn
switch la := la.toAddr().(type) {
case *UDPAddr:
l, err = ListenUDP(net, la)
case *IPAddr:
l, err = ListenIP(net, la)
case *UnixAddr:
l, err = ListenUnixgram(net, la)
default:
return nil, &OpError{
Op: "listen",
Net: net,
Addr: la,
Err: &AddrError{Err: "unexpected address type", Addr: laddr},
}
}
if err != nil {
return nil, err // l is non-nil interface containing nil pointer
}
return l, nil
}
正如您所料,这些函数用于从各种net
连接中读取。
如果查看网络包 - 您可以看到所有Read()
函数都来自实现Conn
接口的类型。
Conn接口定义为:
......一种通用的面向流的网络连接。
为了实现net.Conn
,您需要具备以下方法:
type Conn interface {
// Read reads data from the connection.
// Read can be made to time out and return a Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetReadDeadline.
Read(b []byte) (n int, err error)
// Write writes data to the connection.
// Write can be made to time out and return a Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetWriteDeadline.
Write(b []byte) (n int, err error)
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
Close() error
// LocalAddr returns the local network address.
LocalAddr() Addr
// RemoteAddr returns the remote network address.
RemoteAddr() Addr
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A deadline is an absolute time after which I/O operations
// fail with a timeout (see type Error) instead of
// blocking. The deadline applies to all future I/O, not just
// the immediately following call to Read or Write.
//
// An idle timeout can be implemented by repeatedly extending
// the deadline after successful Read or Write calls.
//
// A zero value for t means I/O operations will not time out.
SetDeadline(t time.Time) error
// SetReadDeadline sets the deadline for future Read calls.
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error
// SetWriteDeadline sets the deadline for future Write calls.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
SetWriteDeadline(t time.Time) error
}
所以,这应该清楚地表明实际上没有net.Read()
;但是,Read()
函数对实现net.Conn
接口的类型进行操作。
与net.Read()一样,所有实现都来自实现接口。在这种情况下,该接口为net.PacketConn
PacketConn是一种通用的面向分组的网络连接。
type PacketConn interface {
// ReadFrom reads a packet from the connection,
// copying the payload into b. It returns the number of
// bytes copied into b and the return address that
// was on the packet.
// ReadFrom can be made to time out and return
// an error with Timeout() == true after a fixed time limit;
// see SetDeadline and SetReadDeadline.
ReadFrom(b []byte) (n int, addr Addr, err error)
// WriteTo writes a packet with payload b to addr.
// WriteTo can be made to time out and return
// an error with Timeout() == true after a fixed time limit;
// see SetDeadline and SetWriteDeadline.
// On packet-oriented connections, write timeouts are rare.
WriteTo(b []byte, addr Addr) (n int, err error)
// Close closes the connection.
// Any blocked ReadFrom or WriteTo operations will be unblocked
// and return errors.
Close() error
// LocalAddr returns the local network address.
LocalAddr() Addr
// SetDeadline sets the read and write deadlines associated
// with the connection.
SetDeadline(t time.Time) error
// SetReadDeadline sets the deadline for future Read calls.
// If the deadline is reached, Read will fail with a timeout
// (see type Error) instead of blocking.
// A zero value for t means Read will not time out.
SetReadDeadline(t time.Time) error
// SetWriteDeadline sets the deadline for future Write calls.
// If the deadline is reached, Write will fail with a timeout
// (see type Error) instead of blocking.
// A zero value for t means Write will not time out.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
SetWriteDeadline(t time.Time) error
}
注意:TCPConn.ReadFrom来自实现io.ReaderFrom ReadFrom方法。
正如您可能已经猜到的那样,我们一遍又一遍地看着相同的模式。这称为实现接口。我将让您使用上述解释作为路线图来查找这种特殊方法及其工作原理。
你最好首先看一下Effective Go parts about interfaces。
提供了更多net
个包裹信息
答案 1 :(得分:0)
如果您只使用UDP数据包,最好的解决方案是使用UDP功能。
例如,net.ListenUDP()可以监听udp,udp4和udp6包。 net.ListenPacket可以监听所有UDP数据包,也可以监听ip,ip4,ip6和unixgram数据包。 net.Listen()可以侦听tcp,tcp4,tcp6,unix和unixpacket包。