Assignment 1 Computer Network
Assignment 1 Computer Network
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>
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
while (1) {
int new_socket;
if ((new_socket = accept(server_socket, (struct sockaddr *)&address,
(socklen_t*)&addrlen)) < 0) {
perror("accept");
exit(EXIT_FAILURE);
}
// Convert to uppercase
for (int i = 0; i < strlen(buffer); i++) {
buffer[i] = toupper(buffer[i]);
}
…………………………………………………………………………………………
…………………………………….
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>
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Send request
send(sock, request, strlen(request), 0);
// Receive response
char response[1024] = {0};
int valread = read(sock, response, 1024);
close(sock);
return 0;
}
…………………………………………………………………………………………
…………………………………….
Compilation:
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: