TCP and UDP sockets
Sockets are the building blocks of networking. Servers listen and clients dial using sockets to bind together and share information. The Internet Protocol (IP) layer specifies the address of a machine, but the Transmission Control Protocol (TCP) or the User Datagram Protocol (UDP) specify which port on the machine should be used.
The main difference between the two is the connection state. TCP keeps the connection alive and verifies that messages are received. UDP just sends a message off without receiving an acknowledgement from the remote host.
Creating a server
Here is an example server. The tcp
argument for net.Listen()
can be changed to udp
if you want to change protocol:
package main import ( "net" "fmt" "log" ) var protocol = "tcp" // tcp or udp var listenAddress = "localhost:3000" func main() { listener, err := net.Listen(protocol, listenAddress) if err != nil { log.Fatal("Error creating listener. ", err) } log.Printf("Now listening for...