Writing TCP servers
A TCP server is a program that listens to connection requests on a network port. Once a connection is established with a client, the communication between the client and the server takes place over a net.Conn
object. The server may continue to listen for new connections. This way, a single server can communicate with many clients.
How to do it...
- Select a port that will connect to the clients server.
This is usually a matter of application configuration. The first 1,024 (
0
to1023
) ports usually require a server program to have root privileges. Most of these ports are reserved for well-known server programs, such as port 22 for ssh, or port 80 for HTTP. Ports 1024 and above are ephemeral ports. Your server program can use any port number of 1,024 and above without additional privileges as long as no other program is listening to it.Use port number 0 to let the kernel pick a random unused port. You can create a listener for port
0
, and then query the listener...