0% found this document useful (0 votes)
26 views

TCP_iterative

The document is a lab manual for a course on Computer Communications and Networks, focusing on TCP client-server communication and socket programming. It outlines the objectives, TCP concepts, the 3-way and 4-way handshake processes, and provides details on socket API functions necessary for implementing a TCP iterative server. Additionally, it includes lab tasks that require students to run server-client code, encrypt and decrypt data, and implement TCP communication following specified requirements.

Uploaded by

MOHSIN AKRAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

TCP_iterative

The document is a lab manual for a course on Computer Communications and Networks, focusing on TCP client-server communication and socket programming. It outlines the objectives, TCP concepts, the 3-way and 4-way handshake processes, and provides details on socket API functions necessary for implementing a TCP iterative server. Additionally, it includes lab tasks that require students to run server-client code, encrypt and decrypt data, and implement TCP communication following specified requirements.

Uploaded by

MOHSIN AKRAM
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

University of Central

Punjab
(Faculty of Information Technology)

Course: CSNC 2411


Computer Communications and Networks
(Lab)

Lab 3

Socket Programming:
TCP Client Server Communication
(TCP iterative server)
Lab Manual 03
Objectives
• Introduction to TCP
• TCP Iterative Server and Client communication
• TCP related Socket API functions

Reference Material

What is TCP?

• TCP is a set of rules that allows applications to send and receive data in a reliable way.
• It’s used whenever we need data to reach the other side without errors and in the correct
order (for example, sending emails or loading web pages).
• TCP is connection-oriented, meaning both the client and server first “connect” before
exchanging data. Think of it as making a phone call: you connect before you start
talking.
• In TCP iterative, the server can handle only one client at a time (this is called an
iterative server).

3-Way Handshake (Establishing a Connection)


The 3-way handshake is a process used by TCP to establish a reliable connection between the
client and server. This handshake ensures that both the client and server are ready to send and
receive data. Here's how it works:

1. SYN (Synchronize): The client starts by sending a SYN packet to the server, saying, "I’d
like to start a connection." This packet includes an initial sequence number, which is a
randomly chosen starting point for the data.

2. SYN-ACK (Synchronize-Acknowledge): The server receives the SYN packet and


responds with a SYN-ACK packet, saying, "I acknowledge your request, and here’s my
sequence number."

3. ACK (Acknowledge): The client receives the SYN-ACK and responds with an ACK
packet, saying, "I received your acknowledgement, and I’m ready for communication."

After this 3-step process, the connection is established, and the client and server can start
exchanging data.
4-Way Handshake (Closing a Connection)
The 4-way handshake is the process used by TCP to gracefully close a connection. It ensures
that both the client and server have finished sending data and are ready to disconnect. Here’s
how it works:

1. FIN (Finish): The client sends a FIN packet to signal that it has finished sending data.

2. ACK (Acknowledge): The server receives the FIN and sends an ACK packet,
acknowledging the request to close.

3. FIN (Finish): The server then sends its own FIN packet to indicate that it, too, is done
sending data.

4. ACK (Acknowledge): The client sends a final ACK to acknowledge the server’s FIN
packet.

After these steps, the connection is fully closed, and both the client and server are aware that the
communication has ended.

Why Handshakes Are Important

These handshakes ensure that both sides are ready to communicate or disconnect. It’s a key part
of TCP’s reliability, as it makes sure that data can be sent and received without errors.

Below is the sequence of system calls, used for both client and server, to communicate using
TCP.

Figure 1: TCP Client Server (Iterative)


A TCP iterative Server can handle only one client at a time. Once the first client’s request is
serviced, the Server can connect to the next waiting or incoming client.

TCP Related Socket API Calls or System Calls

Create Socket:

socket(): Creates a socket (communication endpoint) for either the client or


server.

Int socket (int family, int type, int protocol);


returns socket descriptor; -1 on error and sets errno

family : address family / protocol family


▪ AF_INET for IPv4, AF_INET6 for IPv6
type : type of communication
▪ SOCK_STREAM for TCP
▪ SOCK_DGRAM for UDP
▪ SOCK_RAW for Raw socket
Protocol : protocol within family
▪ typically 0 (except for raw socket)

Bind the Socket:

bind() (Server only): Binds the socket to a server specific IP address and port
number.

int bind (int sockfd, struct sockaddr* serverAddr, int addrlen);


bind a socket to a local IP address & port number
returns 0 on success; -1 on failure and sets errno

sockfd : socket descriptor (returned from socket)


serverAddr : includes IP address and port number
▪ IP address set by kernel if value passed is INADDR_ANY,
else set by caller
▪ port number set by kernel if value passed is 0,
else set by caller
addrlen : length of address structure
▪ sizeof (structsockaddr_in)

Listen:

listen() (Server only): Puts the server in a state where it’s ready to accept
connections from clients.

int listen (int sockfd, int backlog);


server puts socket into listening state (wait for connections rather than initiate a connection)
returns 0 on success; -1 on failure and sets errno

sockfd : socket descriptor


backlog : bound on length of un-accept()ed connection queue
(connection backlog)

Accept:

accept() (Server only): Waits for a client to connect. Once a connection is


accepted, data exchange can begin.

int accept (int sockfd, struct sockaddr* cliaddr, int* addrlen);


server accepts a new connection (first one off the queue of pending connections)
returns a new socket descriptor (connected socket) created by kernel;
-1 on error and sets errno

sockfd : socket descriptor (listening socket)


cliaddr : IP address and port number of client (when returned)
addrlen : length of address structure
▪ addrlen is a value-result argument
▪ caller passes size of client’s socket address structure
▪ kernel returns number of bytes stored in the address structure

Connect:

connect() (Client only): Connects to the server socket using the server's IP address
and port number.
int connect (int sockfd, struct sockaddr* servaddr, int addrlen);
client connects to another socket (server)
returns 0 on success; -1 on failure and sets errno

sockfd : socket descriptor


servaddr : IP address and port number of server
addrlen : length of address structure

Lab Tasks

Task 1. Your task is to run TCP server and client code. After that, compile and run both server
and client, understand the code and paste the screenshot of the output here.
[5 marks]
Task 2. In this task, client needs to read the data from the file and encrypt it. Requirements are
as follows [15 marks]
⚫ Read data from the file named as fileData.txt
⚫ Add 3 in all the lowercase letters of the data (a become d)
⚫ Add 2 in all the uppercase letters of the data (a become c)
⚫ Add 1 in the numeric letter of the data (1 become 2)
⚫ Send the encrypted data to server for decryption

Requirements for the server are as follows


⚫ Receive data from client
⚫ subtract 3 in all lowercase letters of the data (d become a)
⚫ subtract 2 in all the uppercase letters of the data (c become a)
⚫ subtract 1 in all the numeric letters of the data (2 become 1)
⚫ Send back the decrypted data to the client

In the end client will show the decrypted data on terminal sent by the server.
Task 3. In previous Lab, you have done Client-Server communication for UDP. The flow chart
for TCP Client-Server communication is provided in the reference material above.
You are now required to write the code for a TCP iterative Server and Client, following
the steps from flow chart, and run the TCP Client-Server programs. [30 marks]
• Take snap of the Server in listening stage in terminal window.
• When client is connected to Server, show its Port number and process ID in terminal
window.
• Client sends a file name to Server.
• Server sends the file to Client.
• After receiving file, Client closes its connection with Server.
• But Server should keep running and now be ready to service a new Client request.

You might also like