Docsity

Network Programming: Creating and Managing TCP Sockets in Server-Side Applications, Slides of Network Programming

Partial preview of the text

Download Network Programming: Creating and Managing TCP Sockets in Server-Side Applications and more Slides Network Programming in PDF only on Docsity!

Network

Programming

(Lecture

Server

Create a

TCP

socket.  Bind server’s well known port to socket.  Convert socket to listening socket.  Accept client connection, send reply  Terminate connection

Bind

server’s

well

known

port

to

socket

 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));

Convert

socket

to

listening

socket

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

TCP

server to prepare what we call the listening descriptor (listenfd in this example).

Listen(listenfd,

LISTENQ);

Accept

client

connection,

send

reply

A

TCP

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.

A

new descriptor is returned by accept for each client that connects to our server.

Accept

client

connection,

send

reply

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

(UTC).

The next library function, ctime, converts this integer value into a human

readable string

Terminate

connection

The server closes its connection with the client by calling close. This initiates the normal

TCP

connection termination sequence: a

FIN

is sent in each direction and each

FIN

is acknowledged by the other end.  We will say much more about TCP's three

way handshake and the four

TCP

packets used to terminate a

TCP

connection