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

Assignment 1 Computer Network

The document describes a TCP client-server program where the server receives requests from the client, processes them by converting letters to uppercase, and returns the response. It provides code snippets and explanations for creating the server and client sockets, sending and receiving data between them, and handling multiple clients.

Uploaded by

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

Assignment 1 Computer Network

The document describes a TCP client-server program where the server receives requests from the client, processes them by converting letters to uppercase, and returns the response. It provides code snippets and explanations for creating the server and client sockets, sending and receiving data between them, and handling multiple clients.

Uploaded by

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

Assignment 1

PS2
202017b2009
Adarsh Chand
Q. Write a C program to make the TCP server to receive a request, to process the
request, and to send back the response. The client program needs to read the
request string from a file and store the response string in another file. The name
of the file needs to be passed as the argument to the main method of the client
program. The server program needs to accept the request string, change all
lowercase letters to uppercase letters, and return the result.
Note: Upload your assignment files, which consist of an executed C program with
output. Program output must contain student ID, name, and time of execution.
Please upload a screenshot of the output.

Solution –
Server Side:
1. Socket Creation: The server creates a TCP socket using socket system call.
This establishes a communication endpoint for the server.
2. Socket Binding: The server binds the socket to a specific port (8080 in this
case) using bind. This tells the system that the server is willing to accept connections
on that port.
3. Listening for Connections: The server enters a listening state using listen.
This instructs the operating system to queue incoming connection requests on the
specified socket.
4. Accepting Connection: When a client attempts to connect, the server accepts
the connection using accept. This creates a new socket for communication with that
specific client.
5. Receiving Request: The server receives data (the request string) from the
client using read. The data is read from the newly created socket for that client.
6. Processing Request: In this example, the server iterates through the received
request string and converts all lowercase letters to uppercase letters.
7. Sending Response: The server sends the processed response (uppercase
request) back to the client using send. The data is sent on the same socket used for
receiving the request.
8. Closing Connection: Once communication with the client is complete, the
server closes the client socket using close.
9. Repeating: The server goes back to step 3 (listening for connections) to wait
for new client connections. This loop continues indefinitely unless the server process
is terminated.
Server Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>

#define PORT 8080

int main(int argc, char *argv[]) {


int server_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);

// Creating socket file descriptor


if ((server_socket = socket(AF_INET, SOCK_STREAM, 0)) == 0) {
perror("socket failed");
exit(EXIT_FAILURE);
}

// Forcefully attaching socket to the port 8080


if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR |
SO_REUSEPORT, &opt, sizeof(opt))) {
perror("setsockopt");
exit(EXIT_FAILURE);
}

address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);

// Bind the socket to the address


if (bind(server_socket, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("bind failed");
exit(EXIT_FAILURE);
}

// Listen for connections


if (listen(server_socket, 3) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}

while (1) {
int new_socket;
if ((new_socket = accept(server_socket, (struct sockaddr *)&address,
(socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}

char buffer[1024] = {0};


valread = read(new_socket, buffer, 1024);

// Convert to uppercase
for (int i = 0; i < strlen(buffer); i++) {
buffer[i] = toupper(buffer[i]);
}

send(new_socket, buffer, strlen(buffer), 0);


printf("Hello message sent\n");
close(new_socket);
}

// Close the socket


close(server_socket);
return 0;
}

…………………………………………………………………………………………
…………………………………….

Client Side:

1. Socket Creation: The client creates a TCP socket using socket similar to the
server.
2. Server Address Setup: The client defines the server's IP address (127.0.0.1 for
localhost in this case) and port number (8080) into a sockaddr_in structure.
3. Connecting to Server: The client attempts to connect to the server
using connect. This sends a connection request to the server at the specified address
and port.
4. Reading Request from File: The client opens a request file in read mode ("r")
using fopen. It then reads the request string from the file into a buffer using fgets.
5. Sending Request: The client sends the request string read from the file to the
server using send. The data is sent on the socket established with the server.
6. Receiving Response: The client waits for the server's response and receives it
using read. The response (uppercase request) is stored in a buffer.
7. Storing Response in File: The client opens a response file in write mode ("w")
using fopen. It then writes the received response data to the file using fwrite.
8. Closing Connection: The client closes the socket connection using close.
9. Success Message (Optional): The client can print a success message
indicating the request was sent and the response was saved.

Client Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>

#define PORT 8080

int main(int argc, char const *argv[]) {


if (argc < 3) {
fprintf(stderr, "Usage: %s <request_file> <response_file>\n", argv[0]);
exit(EXIT_FAILURE);
}

int sock = socket(AF_INET, SOCK_STREAM, 0);


struct sockaddr_in serv_addr;

memset(&serv_addr, '0', sizeof(serv_addr));

serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);

// Convert IPv4 address from string to binary form


if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
perror("inet_pton");
exit(EXIT_FAILURE);
}

if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {


printf("Connection Failed \n");
exit(EXIT_FAILURE);
}

// Read request from file


FILE* request_fp = fopen(argv[1], "r");
if (request_fp == NULL) {
perror("Failed to open request file");
exit(EXIT_FAILURE);
}
char request[1024] = {0};
fgets(request, sizeof(request), request_fp);
fclose(request_fp);

// Send request
send(sock, request, strlen(request), 0);

// Receive response
char response[1024] = {0};
int valread = read(sock, response, 1024);

// Store response in file


FILE* response_fp = fopen(argv[2], "w");
if (response_fp == NULL) {
perror("Failed to open response file");
exit(EXIT_FAILURE);
}

fwrite(response, sizeof(char), valread, response_fp);


fclose(response_fp);

printf("Request sent and response saved successfully.\n");

close(sock);
return 0;
}

…………………………………………………………………………………………
…………………………………….

Compilation:

10. Save the files:


Save the server code as server.c.
Save the client code as client.c.
11. Compile the server:
Open a terminal in the directory where the file ‘server.c’ was saved
Use a C compiler like GCC to compile the server code: ‘gcc server.c -o server’

12. Compile the client:


Open a terminal in the directory where the file ‘client.c’ was saved
Compile the client code: ‘gcc client.c -o client’
Running:
13. Start the server:
In a terminal, run the compiled server program: ‘./server’
14. Open a new terminal and run the client:
In a separate terminal window, run the compiled client program, providing the
request and response file names as arguments:’./client request.txt response.txt’
…………………………………………………………………………………………
…………………………………….

Overall Flow:
 The server sets up a listening environment on a specific port, waiting for client
connections.
 The client initiates a connection request to the server's IP address and port.
 Once connected, the client sends a request string (read from a file) to the
server.
 The server receives the request, processes it (uppercase conversion), and sends
back a response.
 The client receives the response, stores it in a file (specified as an argument),
and closes the connection.
 The server goes back to listening for new connections, and the client process
can terminate or potentially send another request.

…………………………………………………………………………………………
…………………………………….

Output:

// Output for Server


Hello message sent
Hello message sent
Hello message sent

// Output for Client


Request sent and response saved successfully
Request sent and response saved successfully
The content of the response file you specified (response.txt in the example) will be the uppercase version
of the request string that was sent from the client.

You might also like