Socket Communications Socket Communications work in the usual UNIX paradigm, using file descriptors. Thus, I/O to a socket once it exists is through the same read() and write() calls that one would use for a disk file. There are two basic methods for using a socket. The client method, and the server method. A client can simply set up a socket to a given host, on a given port, and connect. A server must create a socket and then "stick it on a port" to wait for an incoming connection. The fundamental difference is in the listen() and accept() calls that a server must make, versus a connect() call made by a client. A socket is created similarly at the beginning of both types. A socket structure must be created as well as pointers to host and service information structures. These will be filled in later. Host information is filled in by a gethostbyname() call. This information is then copied into the socket information structure being built. If necessary, a getservbyname() call is also made. A socket() call is then made. This sets up a socket, of the given address and data transmission type, returning a file descriptor for the program to use. This is where the two diverge. At this point, the client can make a connect() call which will "hook up" the socket with the desired host, in the desired way (this is achieved by passing it the socket information structure filled in earlier, which is how it knows who to connect to and suchlike). The server must make a bind() call, which attaches a socket to the port given in the socket information structure, and a listen() call, which specifically orders the socket that it will be awaiting connections. Then, when the server is ready to take connections, an accept() call is made. This is a blocking call, which will cause the program to sleep until a connection comes in on the port. It then returns a file descriptor, just like the client's connect() call did. Now the two can communicate using their file descriptors using standard I/O methods.