TCP_iterative
TCP_iterative
Punjab
(Faculty of Information Technology)
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).
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.
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.
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.
Create Socket:
bind() (Server only): Binds the socket to a server specific IP address and port
number.
Listen:
listen() (Server only): Puts the server in a state where it’s ready to accept
connections from clients.
Accept:
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
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
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.