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

TCP Concurrent LAB

Uploaded by

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

TCP Concurrent LAB

Uploaded by

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

University of

Central Punjab
(Faculty of Information
Technology)

Course: CSNC 2411


Computer Communications and Networks
(Lab)

Lab 4

Socket Programming:
TCP Client Server Communication
(TCP Concurrent Server)
Lab Manual 04
Objectives
 TCP concurrent server
 Understanding the fork() call
 TCP concurrent server and client communication

Reference Material

TCP Concurrent Server:


A TCP concurrent server is a server that can interact with several clients at the same
time.
 This means multiple clients can connect and send messages to the server
without waiting for others to finish.
 Unlike a basic or “iterative” server (where only one client can connect at a
time), a concurrent server can handle several clients at once.
Example:
Think of a chat server where multiple users (clients) are chatting with the
server at the same time. Each user can type and send messages to the server, and the
server responds independently to each user.

To create a concurrent server, we use a function called fork().

The fork() System Call:


The fork() function in UNIX helps the server handle multiple clients by creating a
separate “child” process for each client connection.
Here’s how it works:
 No arguments: fork() does not take any input.
 Returns twice: It’s called once but gives two results:
o In the parent process (the original server process), it returns the ID of
the child process.
o In the child process (the new process created for the client), it returns 0.

Why fork()?
By using fork(), the server can continue listening for new client connections while
each client is handled by its own child process.
In this process:
 Close New Descriptor: The parent process (original server) closes the new
socket descriptor for the client, as it's not directly communicating.
 Close Old Descriptor: The child process (created by fork()) closes the old
socket descriptor, as it only needs to communicate with its specific client.

Basic Flow of TCP Concurrent Server with Clients

Here’s a simplified flow of how a TCP concurrent server works:

1. Setup socket: The server creates a socket to listen for client connections.
2. Bind and listen: The server binds the socket to an IP address and port, then
listens for incoming connections.
3. Accept and fork: When a client connects, the server accepts the connection
and then uses fork() to create a child process for that client.
4. Process client request: The child process takes care of the client’s request
while the main server continues to listen for other clients.
5. Close connection: When the client is done, the child process closes the
connection and exits.
Flow diagram for TCP Concurrent Server & Client communication

Lab Tasks

Task 1. Your task is to create two client processes and show that both the clients are
communicating with the TCP concurrent server at the same time and server is
responding to both of them accordingly.
[10 Marks]
For example
Server side:
Messages:
Client with Port 1234: Hello Server
Client with Port 1235: Hello Server

Client side:
Client with Port 1234
Message:
Server with Port 8080: Hello Client

Client with Port 1235


Message:
Server with Port 8080: Hello Client

Task 2. Your task is to modify the client program to accept user input and show on the server
side.
[10 Marks]

Task 3. Your task is to modify the server program to log information about each client
connection, saved in a file named detailed_server_log.txt.
[10 Marks]

You might also like