Transport Layer
Transport layer protocols are the next OSI layer on top of IP and offer a communication channel abstraction. The two most common protocols today are TCP, which offers a connection-oriented communication channel, and UDP, a connectionless protocol.
In Go, the way you interact with both protocols is similar, even though the underlying packet exchange may be completely different. At a high level, there are only a few things that you need to keep in mind when dealing with TCP or UDP:
- Each TCP or UDP application works with a corresponding connection represented by a concrete
TCPConn
orUDPConn
type, respectively. - Go has other connection types with overlapping features like
PacketConn
that deals with connectionless protocols (UDP, IP),Conn
that covers IP, TCP and UDP, andUnixConn
for connections to Unix domain sockets. We only focus onTCPConn
andUDPConn
in this section. - Clients use
net.DialTCP
andnet.DialUDP
to open a socket to a remote address. - Servers use
net.ListenUDP...