Inet sockets, can be used across different computers for networking.
Get two computers in a LAN.
Run the server on one computer with:
./server.out
Get the IP of the server computer with ifconfig
.
Then on the other computer, run:
./client.out 192.168.0.1
Or use some existing client like:
netcat localhost 12345
and then type your messages.
Also try netcat -4
and netcat -6
to exercise different IP versions.
The read
calls on both client and server run inside while loops.
Like when reading from files, the OS may split up messages arbitrarily to make things faster, e.g. one packet may arrive much earlier than the other.
So the protocol must specify a convention of where messages stop. Common methods include:
- a header with a length indicator (e.g. HTTP
Content-Length
) - an unique string that terminates messages. Here we use
\n
. - the server closes connection: HTTP allows that http://stackoverflow.com/a/25586633/895245. Limited of course since the next message requires a reconnect.
TODO: I think if we were using UDP, the size of messages would be known always from the protocol.
If we don't use this on the server bind
, then:
- bind here on the server,
- connect once with a client
- kill server
- try to restart server
- bind fails, because the connection still exists on TIME-WAIT
This allows the bind to happen even it TIME-WAIT is going on.
See also: