0% found this document useful (0 votes)
1 views11 pages

ASSIGNMENT 3 Network Programming

The document outlines an assignment for creating two TCP socket programs: a client and a server. The client program, client_add.c, sends two integers to the server, server_add.c, which computes and returns their sum. Additionally, it includes instructions for compiling, running the programs, and monitoring TCP connection states using netstat.

Uploaded by

ruthmelly657
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)
1 views11 pages

ASSIGNMENT 3 Network Programming

The document outlines an assignment for creating two TCP socket programs: a client and a server. The client program, client_add.c, sends two integers to the server, server_add.c, which computes and returns their sum. Additionally, it includes instructions for compiling, running the programs, and monitoring TCP connection states using netstat.

Uploaded by

ruthmelly657
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/ 11

ASSIGNMENT 3

Members

1. 22/05611 Alvin Kipkoech

2. 22/04373 Christian meso

3. 22/05489 Kamitu Titus Muuo

4. 22/05861 Wachira Charles Wanjohi

5. 22/05237 wasike Joseph

6. 24/00102 Peter Mwangi

7. 22/06418 Dolla Jesse

8. 22/07049 macharia Sammy

9. 21/05057 Ian Munene

You are supposed write two separate TCP socket programs that implements both Client/Server

parts of a communication such that Client will send two integers and Server will reply with the

sum of those integers. In your programs you should clearly indicate the header files that are used

a) Write the client_add.c client program that take the two integers as an argument input to the

program or in run-time from the user. After receiving the reply of the Server, Client will

show the user final result. You can use sprintf() function to convert an integer to a string, and

use atoi() function to convert a string to an integer.

b) Write the server_add.c server program that replies with the sum of numbers received from

the client program. The server process should work in connection-oriented and concurrentserver

mode.
c) At the terminal, indicate how the two programs are compiled, build and executed

d) At the terminal, use netstat to indicate how TCP connection, establishment and termination

are achieved. What are the various TCP states?

Answers:

Client_add.c

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <sys/types.h>

#include <sys/socket.h>

#define SERVER_IP "127.0.0.1"

#define SERVER_PORT 8080

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

int sock;

struct sockaddr_in server_addr;

char buffer[1024];

int num1, num2, sum;


if (argc == 3) {

num1 = atoi(argv[1]);

num2 = atoi(argv[2]);

} else {

printf("Enter two integers: ");

scanf("%d %d", &num1, &num2);

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {

perror("Socket creation failed");

exit(1);

server_addr.sin_family = AF_INET;

server_addr.sin_port = htons(SERVER_PORT);

server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);

if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {

perror("Connection failed");

exit(1);

sprintf(buffer, "%d %d", num1, num2);


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

recv(sock, buffer, sizeof(buffer), 0);

sum = atoi(buffer);

printf("Sum received from server: %d\n", sum);

close(sock);

return 0;

The code is then saved on emacs, we have used emacs as our editor
The following command is run to open emacs:
emacs client_add.c
2. Server_add.c

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

#define SERVER_PORT 8080


void handle_client(int client_sock) {
char buffer[1024];
int num1, num2, sum;

recv(client_sock, buffer, sizeof(buffer), 0);


sscanf(buffer, "%d %d", &num1, &num2);

sum = num1 + num2;


sprintf(buffer, "%d", sum);

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

close(client_sock);
exit(0);
}

int main() {
int server_sock, client_sock;
struct sockaddr_in server_addr, client_addr;
socklen_t addr_size;

if ((server_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {


perror("Socket creation failed");
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(SERVER_PORT);
server_addr.sin_addr.s_addr = INADDR_ANY;

if (bind(server_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {


perror("Bind failed");
exit(1);
}

if (listen(server_sock, 5) == -1) {
perror("Listen failed");
exit(1);
}

printf("Server is listening on port %d...\n", SERVER_PORT);

while (1) {
addr_size = sizeof(client_addr);
client_sock = accept(server_sock, (struct sockaddr *)&client_addr, &addr_size);

if (client_sock == -1) {
perror("Accept failed");
continue;
}

if (fork() == 0) {
close(server_sock);
handle_client(client_sock);
}
close(client_sock);
}

return 0;
}
Here are the screenshots when saving the code to emac editor
Now we use gcc to compile the programs, and here are the commands to run:

gcc client_add.c -o client


gcc server_add.c -o server
Now we run the server on a new terminal using the following command:
./server

Now we run the client on a new terminal and we enter the two numbers using the following
command:
./client 10 12

Now we check the TCP Connections using Netstat and we are going to run the following
command:
netstat -tanp | grep :8080

This shows:

 LISTEN → Server is waiting for connections.


 ESTABLISHED → Client-server connection is active.
 TIME_WAIT → Connection is closing.
 CLOSE_WAIT → Server is waiting for client to close.
 FIN_WAIT → Connection is terminating.

To monitor changes dynamically, we use the following command:


watch -n 1 "netstat -tanp | grep :8080"

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy