我是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()
Mom*_*mer 23
我们来看看你的问题.
听取当地网络地址laddr的通知.网络网络必须是面向流的网络:"tcp","tcp4","tcp6","unix"或"unixpacket".有关laddr的语法,请参阅拨号.
用法示例#1:
tcpSock, err := net.Listen("tcp", "0.0.0.0:8080")
Run Code Online (Sandbox Code Playgroud)
用法示例#2
unixSock, err := net.Listen("unix", "/var/app/server.sock")
Run Code Online (Sandbox Code Playgroud)
我们可以在源代码中看到它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
}
Run Code Online (Sandbox Code Playgroud)
有关net.Listen()文档的其他信息
那么,我们可以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")
Run Code Online (Sandbox Code Playgroud)
而且,如果我们深入了解,我们可以看到它的运作方式与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
}
Run Code Online (Sandbox Code Playgroud)
正如您所料,这些函数用于从各种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
}
Run Code Online (Sandbox Code Playgroud)
所以,这应该清楚地表明实际上没有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
}
Run Code Online (Sandbox Code Playgroud)
注意: TCPConn.ReadFrom来自实现io.ReaderFrom ReadFrom方法.
正如您可能已经猜到的那样,我们一遍又一遍地看着相同的模式.这称为实现接口.我将让您使用上述说明作为路线图查找此特定方法及其工作原理.
您最好首先查看有关接口的Effective Go部分.