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

Detailed Algorithm for the TCP

Uploaded by

binduann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Detailed Algorithm for the TCP

Uploaded by

binduann
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Detailed Algorithm for the TCP Client Program

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

Objective: Set up the environment for communication.

Actions:

1. Include necessary libraries:

stdio.h: For input/output operations such as printf and perror.

stdlib.h: For memory management and process control functions such as exit().

string.h: For string manipulation functions such as strlen.

unistd.h: For system calls like close().

arpa/inet.h: For functions related to network communication (e.g., socket(), inet_pton()).

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:

1. Call socket() to create a socket.

Parameters:

AF_INET: Specifies the address family (IPv4).

SOCK_STREAM: Specifies that this is a TCP socket (stream socket).

0: The protocol, which defaults to TCP when using SOCK_STREAM.

2. Check if socket creation fails:

If socket() returns -1, print an error message and terminate the program with exit().

---

3. Configure Server Address

Objective: Define the address and port of the server to which the client will connect.

Actions:

1. Set the address family:

serv_addr.sin_family = AF_INET: Indicates that we are using IPv4.

2. Set the port number:

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:

inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr): Converts the server’s IP address


(127.0.0.1 which is localhost) into binary form and stores it in serv_addr.sin_addr.

---

4. Establish Connection to the Server

Objective: Connect to the server using the configured server address.

Actions:

1. Call connect() to establish a connection:

Parameters:

sock: The socket descriptor created earlier.

(struct sockaddr *)&serv_addr: Pointer to the server's address structure.

sizeof(serv_addr): Size of the server's address structure.

2. Check if connection fails:

If connect() returns -1, print an error message and terminate the program using exit().

---
5. Communicate with the Server

Objective: Send a message to the server and receive a response.

Actions:

1. Send a message to the server:

Call send() to transmit the message ("Hello from client") to the server.

Parameters:

sock: The socket descriptor.

message: The message to send.

strlen(message): The length of the message.

0: Flags (no flags).

2. Print a message indicating the message was sent:

Use printf() to display "Message sent to server".

3. Receive a response from the server:

Call read() to receive a response from the server into the buffer (buffer).

Parameters:

sock: The socket descriptor.

buffer: The buffer where the response will be stored.

BUFFER_SIZE: The size of the buffer.


4. Print the response from the server:

Use printf() to display the received message: "Message from server: [response]".

---

6. Close the Socket

Objective: Close the connection and clean up resources.

Actions:

1. Call close() to close the socket and free associated resources.

Detailed Algorithm for the TCP Server Program

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

Objective: Set up necessary configurations for the server to start.

Actions:

1. Include necessary header files:

stdio.h: For input/output functions (printf, perror).

stdlib.h: For standard library functions such as exit().


string.h: For string manipulation (e.g., strlen).

unistd.h: For system calls like close().

arpa/inet.h: For networking operations (e.g., socket(), bind(), listen(), accept()).

2. Define constants:

PORT: Set the port number to 8080 for the server to listen to.

BUFFER_SIZE: Set buffer size for receiving messages (1024 bytes).

---

2. Create a Socket

Objective: Establish a communication endpoint for the server.

Actions:

1. Create a socket using socket():

Call socket() with the following parameters:

AF_INET: Address family (IPv4).

SOCK_STREAM: Socket type (TCP).

0: Protocol type (defaults to TCP).

This creates a socket file descriptor, server_fd, which is used for subsequent communication.

2. Check for errors in socket creation:


If the socket() function fails (i.e., returns -1), print an error message using perror() and exit the
program using exit().

---

3. Bind the Socket to a Port

Objective: Bind the socket to a specific IP address and port to start listening for incoming
connections.

Actions:

1. Configure server address structure (sockaddr_in):

Set sin_family = AF_INET for IPv4.

Set sin_addr.s_addr = INADDR_ANY to accept connections on any available network interface.

Set sin_port = htons(PORT) to specify the port number in network byte order.

2. Bind the socket to the configured address:

Call bind() with the server_fd socket descriptor, the address structure, and its size.

3. Check for errors in binding:

If bind() returns -1, print an error message and exit the program.

---
4. Listen for Incoming Connections

Objective: Prepare the server to accept incoming client connections.

Actions:

1. Start listening for connections:

Call listen() on the server_fd socket, specifying a backlog of 3 (maximum number of pending
connections allowed).

2. Check for errors in listening:

If listen() fails (i.e., returns -1), print an error message and exit the program.

---

5. Accept a Client Connection

Objective: Wait for a client to connect and accept the connection once it's established.

Actions:

1. Accept an incoming client connection:

Call accept() to block and wait for an incoming connection.

The function returns a new socket descriptor (new_socket) to handle the communication with
the client.

2. Check for errors in accepting the connection:

If accept() fails (i.e., returns -1), print an error message and exit the program.
---

6. Communicate with the Client

Objective: Receive messages from the client and send a response.

Actions:

1. Receive data from the client:

Call read() to receive a message from the client and store it in the buffer.

Print the received message using printf().

2. Send a message back to the client:

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.

Print a confirmation message after sending the message.

---

7. Close the Sockets

Objective: Clean up and close the sockets after communication is complete.

Actions:

1. Close the client connection:


Call close(new_socket) to close the socket associated with the client.

2. Close the server socket:

Call close(server_fd) to close the server socket, freeing up the resources.

This step-by-step approach outlines how the server listens for a client, exchanges data, and
then terminates the connection.

You might also like