Detailed Algorithm for the TCP
Detailed Algorithm for the TCP
This algorithm outlines the steps and logic for the provided TCP client program in C. The
program establishes a connection with a server, sends a message, receives a response, and
then closes the connection.
1. Initialization
Actions:
stdlib.h: For memory management and process control functions such as exit().
2. Define constants:
PORT: Set the port number for the connection (e.g., 8080).
BUFFER_SIZE: Set the buffer size for receiving messages from the server (e.g., 1024).
---
2. Create a Socket
Objective: Create a communication endpoint for the client.
Actions:
Parameters:
If socket() returns -1, print an error message and terminate the program with exit().
---
Objective: Define the address and port of the server to which the client will connect.
Actions:
serv_addr.sin_port = htons(PORT): Set the port number, converting it to network byte order
using htons().
3. Convert the server’s IP address to binary format:
---
Actions:
Parameters:
If connect() returns -1, print an error message and terminate the program using exit().
---
5. Communicate with the Server
Actions:
Call send() to transmit the message ("Hello from client") to the server.
Parameters:
Call read() to receive a response from the server into the buffer (buffer).
Parameters:
Use printf() to display the received message: "Message from server: [response]".
---
Actions:
This algorithm describes the steps involved in the provided TCP server program in C. The
server listens for incoming client connections, exchanges messages with the client, and then
closes the connection.
---
1. Initialization
Actions:
2. Define constants:
PORT: Set the port number to 8080 for the server to listen to.
---
2. Create a Socket
Actions:
This creates a socket file descriptor, server_fd, which is used for subsequent communication.
---
Objective: Bind the socket to a specific IP address and port to start listening for incoming
connections.
Actions:
Set sin_port = htons(PORT) to specify the port number in network byte order.
Call bind() with the server_fd socket descriptor, the address structure, and its size.
If bind() returns -1, print an error message and exit the program.
---
4. Listen for Incoming Connections
Actions:
Call listen() on the server_fd socket, specifying a backlog of 3 (maximum number of pending
connections allowed).
If listen() fails (i.e., returns -1), print an error message and exit the program.
---
Objective: Wait for a client to connect and accept the connection once it's established.
Actions:
The function returns a new socket descriptor (new_socket) to handle the communication with
the client.
If accept() fails (i.e., returns -1), print an error message and exit the program.
---
Actions:
Call read() to receive a message from the client and store it in the buffer.
Call send() to send a message ("Hello from server") back to the client.
The message is sent through the new_socket created during the accept() step.
---
Actions:
This step-by-step approach outlines how the server listens for a client, exchanges data, and
then terminates the connection.