






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







Create a
socket. Bind server’s well known port to socket. Convert socket to listening socket. Accept client connection, send reply Terminate connection
The server's well ‐ known port ( for the daytime service) is bound to the socket by filling in an Internet socket address structure and calling bind. specify the IP address as INADDR_ANY, which allows the server to accept a client connection on any interface, in case the server host has multiple interfaces bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(13); /* daytime server Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
By calling listen, the socket is converted into a listening socket, on which incoming connections from clients will be accepted by the kernel. These three steps, socket, bind, and listen, are the normal steps for any
server to prepare what we call the listening descriptor (listenfd in this example).
connection uses what is called a three
way handshake to establish a connection. When this handshake completes, accept returns, and the return value from the function is a new descriptor (connfd) that is called the connected descriptor. This new descriptor is used for communication with the new client.
new descriptor is returned by accept for each client that connects to our server.
The current time and date are returned by the library function time, which returns the number of seconds since the Unix Epoch:
January
Coordinated Universal Time
The next library function, ctime, converts this integer value into a human
readable string
The server closes its connection with the client by calling close. This initiates the normal
connection termination sequence: a
is sent in each direction and each
is acknowledged by the other end. We will say much more about TCP's three
way handshake and the four
packets used to terminate a
connection