Implementation of TCP Client-Server Communication using Sockets
Experiment
Implementation of TCP Client-Server Communication using Sockets
Aim
To implement a simple TCP client-server program using sockets and demonstrate communication between them.
Objective
- To understand the concept of socket programming
- To learn how TCP ensures reliable communication
- To establish communication between client and server processes
Theory
What is a Socket?
A socket is an endpoint for communication between two machines over a network. It allows processes to send and receive data.
What is TCP?
Transmission Control Protocol (TCP) is a connection-oriented protocol that:
- Ensures reliable data transfer
- Guarantees data delivery in order
- Performs error checking and retransmission
Client-Server Model
- Server waits for incoming requests
- Client initiates communication
-
Communication happens via:
- IP Address
- Port Number
Working of TCP Communication
- Server creates a socket
- Server binds it to an IP and port
- Server listens for connections
- Client creates a socket and connects to server
- Data exchange happens
- Connection is closed
Algorithm
Server Side
- Create socket
- Bind socket to IP and port
- Listen for connections
- Accept client connection
- Receive message from client
- Send response
- Close connection
Client Side
- Create socket
- Connect to server
- Send message
- Receive response
- Close connection
Socket Programming in C
Socket programming provides an interface to communicate between processes over a network using system calls.
Key Functions Used
-
socket()→ Create a socket -
bind()→ Assign IP and port -
listen()→ Wait for connections -
accept()→ Accept client request -
connect()→ Client connects to server -
send()/recv()→ Data communication -
close()→ Close socket
TCP Characteristics
- Connection-oriented
- Reliable and ordered delivery
- Error detection and retransmission
Program
๐น Server Program (C)
๐น Client Program (C)
Compilation and Execution
Compile
Run
Open two terminals:
Terminal 1 (Server):
Terminal 2 (Client):
Sample Output
Server
Client
Result
The TCP client-server communication was successfully implemented in C, and reliable data transfer was achieved.
๐ฅ️ Server Code Explanation (Step by Step)
-
server_fd: Socket descriptor for the server -
new_socket: Used for communication with the client
- Structure to store:
- IP address
- Port number
- Address family
struct sockaddr_in {
short int sin_family;
unsigned short int sin_port;
struct in_addr sin_addr;
};
sin_family→ Socket Internet Family-
sin_port→ Socket Internet Port -
sin_addr→ Socket Internet Address
-
addrlen: Size of address structure -
buffer: To store incoming data -
message: Response to client
๐น Step 1: Create Socket
-
AF_INET→ IPv4 -
SOCK_STREAM→ TCP - Returns a socket descriptor
๐น Step 2: Configure Address
-
AF_INET→ IPv4 (AF_INET6→ IPv6 ) -
INADDR_ANY→ Accept connections from any IP -
htons(PORT)→ Convert port to network byte order (htons= Host TO Network Short )
๐น Step 3: Bind Socket
-
Links socket to:
- IP address
- Port number
- Makes server reachable
๐น Step 4: Listen for Connections
- Server starts listening
-
3→ Maximum pending connections (queue size)
๐น Step 5: Accept Client
- Accepts incoming connection
- Returns a new socket for communication
- Original socket continues listening
๐น Step 6: Receive Data
- Reads message sent by client
-
Stores in
buffer
๐น Step 7: Send Response
- Sends data back to client
๐น Step 8: Close Connections
-
Closes:
- Client connection
- Server socket
๐ Client Code Explanation (Step by Step)
-
sock: Client socket descriptor -
serv_addr: Server address info
- Message to send
- Buffer to receive response
๐น Step 1: Create Socket
-
Same as server:
- IPv4 + TCP
๐น Step 2: Set Server Details
-
Define server:
- Address family
- Port
- Converts IP string → binary format required by the system
- "127.0.0.1" means:
- Loopback address (localhost)
- Refers to same machine
๐น Step 3: Convert IP Address ( optional not used here)
- Converts IP string → binary format
-
"127.0.0.1"→ localhost
๐น Step 4: Connect to Server
- Initiates TCP connection
- Performs 3-way handshake
๐น Step 5: Send Data
- Sends message to server
๐น Step 6: Receive Response
- Receives reply from server
๐น Step 7: Close Socket
- Terminates connection
Comments
Post a Comment