Golang in net package usage (iii)--TCP and UDP and UNIX domain sockets

Source: Internet
Author: User
Tags unix domain socket
This is a creation in Article, where the information may have evolved or changed.

Type TCPADDR//indicates TCP terminal address

Type tcpaddr struct {    IP   IP    Port int    //IPV6 addressing range }

Func resolvetcpaddr (NET, addr string) (*TCPADDR, error)//resolves an address to a TCP address form such as "Host:port" or "[Ipv6-host%zone]:p ort", Resolves the network domain name and port name. NET must be "TCP", "TCP4" or "TCP6", IPv6 address literal/name must be wrapped in square brackets, such as "[:: 1]:80", "[ipv6-host]:http" or "[ipv6-host% Zone]:80 ".

Func (a *tcpaddr) network () string//return address of the net type, "TCP"

Func (a *tcpaddr) string () string


Type Tcpconn//tcpconn is a TCP network connection that implements the Conn interface, where most of the methods are the same as Ipconn, which is not discussed here.
Func dialtcp (NET string, laddr, Raddr *tcpaddr) (*tcpconn, error)
Func (c *tcpconn) Close () error
Func (c *tcpconn) Closeread () error
Func (c *tcpconn) closewrite () error
Func (c *tcpconn) File () (F *os. File, err Error)
Func (c *tcpconn) localaddr () Addr
Func (c *tcpconn) Read (b []byte) (int, error)
Func (c *tcpconn) readfrom (R io. Reader) (Int64, error)
Func (c *tcpconn) remoteaddr () Addr
Func (c *tcpconn) Setdeadline (t time. Time) Error
Func (c *tcpconn) setkeepalive (keepalive bool) error//Setting whether the operating system should send keepalive information in this connection
Func (c *tcpconn) Setkeepaliveperiod (d time. Duration) error//Setting the KeepAlive life cycle
Func (c *tcpconn) Setlinger (sec int) Error//setlinger sets the shutdown behavior of a connection when there is still data waiting to be sent or confirmed in the connection. If Sec<0 (the default), the operating system will complete the send data operation in the background If sec==0, the operating system discards any unsent or unacknowledged data, and when sec>0, the data is sent in the background, which is consistent with sec<0. However, in some operating systems, after SEC seconds, the system discards any unsent data.
The Func (c *tcpconn) Setnodelay (nodelay bool) error//controls whether the operating system delays the sending of packets in order to send fewer packets (Nagle ' s algorithm), the default value is True (no delay), This means that the data will be sent as soon as possible after the write, rather than delayed.
Func (c *tcpconn) setreadbuffer (bytes int) error//Setting the operating system accepts cache size for Conn connection
Func (c *tcpconn) Setreaddeadline (t time. Time) error//Set read timeout
Func (c *tcpconn) setwritebuffer (bytes int) error//Sets the size of the operating system send cache for conn connections
Func (c *tcpconn) Setwritedeadline (t time. Time) error//Set send timeout
Func (c *tcpconn) write (b []byte) (int, error)//implements the Write interface method


Type TCPLISTENER//TCP listener, the client should use different types of snooping instead of the default TCP

Func listentcp (NET string, Laddr *tcpaddr) (*tcplistener, error)//declares the TCP address LADDR and returns a TCP listener, where net must be TCP, TCP4 or TCP6, if the laddr port is 0, LISTENTCP will select an available port, and the caller can take advantage of TcpListener's addr method to obtain the address.
Func (L *tcplistener) accept () (Conn, error)//Implement the Accept method of the listener interface, which waits for the next call and returns a connection
Func (L *tcplistener) accepttcp () (*tcpconn, error)//accept the next call and return a new connection
Func (L *tcplistener) Addr () addr//returns the network address of listener, TCPADDR
Func (L *tcplistener) Close () error//Stop listening for TCP addresses, connections that have been established are not closed

Func (L *tcplistener) File () (F *os. File, err Error)//return to the underlying OS. A copy of file, set to block mode, the caller needs to close the file when finished, close L does not affect the file copy F, and close the file copy F also does not affect TcpListener L, the file handle returned is different from the original network connection file, Using this copy to change the original file properties may or may not work.

Func (L *tcplistener) setdeadline (t time. Time) error//sets the listener's time-out and, if set to 0, disables the timeout setting, which never times out.


Type UDPADDR//address that represents a UDP port

Type udpaddr struct {        IP   IP        Port int        Zone string//IPV6 addressing range}

Func resolveudpaddr (NET, addr string) (*UDPADDR, error)//resolves the addr as a UDP address and returns. The parameter addr format is "Host:port" or "[Ipv6-host%zone]:p ort", which resolves to get the network name and port name; NET must be "UDP", "UDP4", or "UDP6". IPV6 address literal/name must be wrapped in square brackets, such as "[:: 1]:80", "[ipv6-host]:http" or "[ipv6-host%zone]:80".
Func (a *udpaddr) network () string//returns the address of the net name "UDP"

The character representation of the Func (a *udpaddr) String () string//udpaddr


Type udpconn//implements UDP network connection, it implements the interface of Conn and Packetconn
Func dialudp (NET string, laddr, Raddr *udpaddr) (*udpconn, error)//Connect the remote address raddr on the network, where net must be UDP,UDP4 or UDP6, if laddr is not empty, Use a local address for the connection
Func listenmulticastudp (NET string, Ifi *interface, gaddr *udpaddr) (*udpconn, error)//listens for multicast UDP packets on IFI's group address gaddr, IFI Specifies the interface to join and, if IFI is empty, uses the default multicast interface
Func listenudp (NET string, Laddr *udpaddr) (*udpconn, error)//listens to UDP packets bound on local address laddr, where net must be UDP,UDP4 or UDP6, If LADDR is port 0, then LISTENUDP will select an available port port, using the Udpconn Localaddr method to discover the port The returned Udpconn Readfrom and WriteTo methods can be used to accept and send UDP packets.
Func (c *udpconn) close () error//close connection
Func (c *udpconn) File () (F *os. File, err Error)//similar to the file () method in Tcpconn
Func (c *udpconn) localaddr () addr//returns the local network address
Func (c *udpconn) Read (b []byte) (int, error)
Func (c *udpconn) readfrom (b []byte) (int, Addr, error)
Func (c *udpconn) readfromudp (b []byte) (n int, addr *udpaddr, err Error)//reads a packet from C, writes the payload to B and returns the number of bytes written and the address of the package. READFROMUDP can be set to timeout.
Func (c *udpconn) readmsgudp (b, Oob []byte) (n, oobn, flags int, addr *udpaddr, err Error)//READMSGUDP reads a packet from C, copies the payload into B, The associated out-of-band data is copied into OOB, returning the number of bytes copied into B, the number of bytes copied into OOB, the flag of the packet, the source address of the packet, and possible errors.
Func (c *udpconn) remoteaddr () addr//Returns the remote network address
Func (c *udpconn) Setdeadline (t time. Time) error//Implement conn timeout method, set udpconn timeout
Func (c *udpconn) setreadbuffer (bytes int) error
Func (c *udpconn) Setreaddeadline (t time. Time) Error
Func (c *udpconn) setwritebuffer (bytes int) error
Func (c *udpconn) Setwritedeadline (t time. Time) Error
Func (c *udpconn) Write (b []byte) (int, error)
Func (c *udpconn) writemsgudp (b, Oob []byte, addr *udpaddr) (n, oobn int, err error)//WRITEMSGUDP sends a packet via C to address addr, B and OOB respectively Returns the number of bytes written (packet data, out-of-band data) and possible errors for the package payload and the corresponding out-of-band data.
Func (c *udpconn) WriteTo (b []byte, addr addr) (int, error)

Func (c *udpconn) writetoudp (b []byte, addr *udpaddr) (int, error)///C writes a UDP packet to addr, which needs to copy the payload from B. WRITETOUDP can also set the timeout period


Type UNIXADDR//socket terminal address that represents a UNIX domain name

Type unixaddr struct {        Name string        Net  String}

Func resolveunixaddr (NET, addr string) (*UNIXADDR, error)//addr parsing to unixaddr,net refers to the network name for Unix,unixgram or Unixpacket
Func (a *unixaddr) network () string//Return net name, Unix,unixgram or Unixpacket

Func (a *unixaddr) string () string//unixaddr representation of the character form


Type Unixconn//unixconn is the network connection for the UNIX domain name socket
Func Dialunix (NET string, laddr, Raddr *unixaddr) (*unixconn, error)//Connect local address LADDR and remote address RADDR on network protocol net. Where net is "Unix", " Unixgram "," Unixpacket "if LADDR is not nil will use it as the local address.
Func Listenunixgram (NET string, Laddr *unixaddr) (*unixconn, error)//listen for packets bound to local address laddr, where net must be Unixgram, the Readfrom and WriteTo methods that return connections can be used to accept and send address packs
Func (c *unixconn) close () error//close connection
The Func (c *unixconn) Closeread () error//closes the read operation of the connection, and in most cases uses the close
The Func (c *unixconn) Closewrite () error//closes the write operation for the connection, and in most cases uses the close
Func (c *unixconn) File () (F *os. File, err Error)//
Func (c *unixconn) localaddr () Addr
Func (c *unixconn) Read (b []byte) (int, error)
Func (c *unixconn) readfrom (b []byte) (int, Addr, error)
Func (c *unixconn) Readfromunix (b []byte) (n int, addr *unixaddr, err Error)
Func (c *unixconn) Readmsgunix (b, Oob []byte) (n, oobn, flags int, addr *unixaddr, err Error)
Func (c *unixconn) remoteaddr () Addr
Func (c *unixconn) Setdeadline (t time. Time) Error
Func (c *unixconn) setreadbuffer (bytes int) error
Func (c *unixconn) Setreaddeadline (t time. Time) Error
Func (c *unixconn) setwritebuffer (bytes int) error
Func (c *unixconn) Setwritedeadline (t time. Time) Error
Func (c *unixconn) Write (b []byte) (int, error)
Func (c *unixconn) Writemsgunix (b, Oob []byte, addr *unixaddr) (n, oobn int, err error)
Func (c *unixconn) WriteTo (b []byte, addr addr) (n int, err error)

Func (c *unixconn) Writetounix (b []byte, addr *unixaddr) (n int, err error)


Type unixlistener//represents a UNIX domain name for the socket listener, and the client should use the specified different type of listener instead of using the default UNIX domain socket
Func Listenunix (NET string, Laddr *unixaddr) (*unixlistener, error)//using the UNIX domain socket LADDR to create a UNIX listener, This network must be UNIX or Unixpacket.
Func (L *unixlistener) Accept () (c Conn, err error)//
Func (L *unixlistener) Acceptunix () (*unixconn, error)
Func (L *unixlistener) Addr () Addr
Func (L *unixlistener) Close () error
Func (L *unixlistener) File () (F *os. File, err Error)
Func (L *unixlistener) setdeadline (t time. Time) (Err error)

Bugs

On any POSIX platform, when you use the Readfrom or Readfromip method to read data from the "IP4" network, even if there is sufficient space, the full IPV4 packet, including the header domain of the packet, may not be returned. This can happen even if the read or Readmsgip method returns the full packet. This condition cannot be corrected because of compatibility requirements for go 1. Therefore, if you must obtain the full packet, we recommend that you do not use either of these methods, instead of using read or READMSGIP.

In a OpenBSD system, the "TCP" network listens without listening for both IPv4 and IPV6 connections. Because the IPV4 communication in the system is not imported into the IPV6 socket. If necessary, use two independent monitors.


Reference:

http://docscn.studygolang.com/pkg/net/#pkg-constants





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.