prev
next
Sockets
- What are sockets?
Sockets provide an interface to network protocols. Their purpose is to make
network programming simpler than programming directly to the network protocol.
Sockets most often use TCP/IP as the underlying protocol, but can use virtually
any of the common network protocols.
- How do sockets function?
There are several modes of operation available for sockets, but a very common
mode is to establish a socket listener that listens on some port, say 4040,
for connection requests.
When a socket client, from another process or a remote computer, requests a
connection on port 4040, the listener spawns a new thread that starts up a socket
server on a new port, say 5051.
From that time on the socket client and socket server communicate on port 5051.
Either one can send data, in the form of a group of bytes, to the other.
Meanwhile the listener goes back to listening for connection requests on port
4040.
The receiving socket, either client or server, has a buffer that stores bytes
of data until the receiver
thread reads them. If the receiver buffer becomes full, the sender thread will
block on a send call until the receiver reads some of the data out of the buffer.
For this reason, it is a good idea to assign a thread in the receiver to empty
the buffer and enqueue the data for a worker thread to digest.
prev
next