0% found this document useful (0 votes)
90 views10 pages

(Computer Network Lab Exam) Arkadeep Roy - Roll 54

This document contains code for a computer network lab exam question involving socket programming between a server and multiple clients. The question involves: 1) The server receives an array of integers from the user, sends the first half to client A and second half to client B. The clients sort their halves and return them to the server, which then merges the sorted arrays. 2) Additional questions involve a client sending a number to the server, which performs calculations and returns the results, and a question where the client and server continuously exchange numbers until one is below a threshold.

Uploaded by

Arkadeep Roy
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)
90 views10 pages

(Computer Network Lab Exam) Arkadeep Roy - Roll 54

This document contains code for a computer network lab exam question involving socket programming between a server and multiple clients. The question involves: 1) The server receives an array of integers from the user, sends the first half to client A and second half to client B. The clients sort their halves and return them to the server, which then merges the sorted arrays. 2) Additional questions involve a client sending a number to the server, which performs calculations and returns the results, and a question where the client and server continuously exchange numbers until one is below a threshold.

Uploaded by

Arkadeep Roy
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

Computer Network(LAB EXAM)

Name :- Arkadeep Roy,


Roll no.: 54,
CSE - 3rd Year,
University Roll no :- 17600118065
University Registration no :- 181760110009
Subject and Code :- Computer Network (Practical Paper): PCC-CS692
_____________________________________________________________

Q1 The server reads ‘n’ integer numbers and stores them in an array; then sends
1st half of the array to the client a and sends 2nd half of the array to the client b.
Client a sorts the 1st half of the array and client b sorts the 2nd half of the array
and they send back the two sorted subarrays to the server. The server receives
two sorted sub-arrays from client a and client b; then merges two sorted
subarrays to get a sorted array of ‘n’ integers and finally, the server displays the
entire sorted array.

Definition:- It is an algorithm based on Socket programming and which is implemented


by using Linux. Basically what the question says is that here we have to use multiple
clients.Select command allows us to monitor multiple file descriptors, waiting until one of
the file descriptors becomes active.If there is some data to be read on one of the sockets
select will provide that information. We can also create two different socket programs.We have
two bind two different socket programs to create one single client server program.

Implementation
Server :-

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

int main() {
int sockfd1, sockfd2, fb1, fb2, len1, len2, i, *p, n, arr[100], upper, lower, count = 1;
char msg[200];
struct sockaddr_in sa1, ca1;
struct sockaddr_in sa2, ca2;
printf("[SERVER] Started Successfully!\n");
sockfd1=socket(AF_INET,SOCK_STREAM,0);
sa1.sin_family=AF_INET;
sa1.sin_addr.s_addr=INADDR_ANY;
sa1.sin_port=1236;

sockfd2=socket(AF_INET,SOCK_STREAM,0);
sa2.sin_family=AF_INET;
sa2.sin_addr.s_addr=INADDR_ANY;
sa2.sin_port=2222;
len1=sizeof(sa1);
i= bind(sockfd1,(struct sockaddr *) &sa1,len1);
if(i == -1) {
printf("[SERVER] [%d %d] Connection cannot be Established with Client A :(\n", sockfd1, i);
exit(i);
}
printf("[SERVER] [%d %d] Connection Established Successfully with Client A!\n",sockfd1, i);
listen(sockfd1,5);
fb1 = accept(sockfd1, (struct sockaddr *) &ca1,&len1);
len2=sizeof(sa2);
i= bind(sockfd2,(struct sockaddr *) &sa2,len2);
if(i == -1) {
printf("[SERVER] [%d %d] Connection cannot be Established with Client B :(\n", sockfd2, i);
exit(i);
}
printf("[SERVER] [%d %d] Connection Established Successfully with Client B!\n",sockfd2, i);
listen(sockfd2,5);
fb2 = accept(sockfd2, (struct sockaddr *) &ca2,&len2);
printf("[SERVER] Enter the size of array: ");
scanf("%d", &n);
arr[0] = 2;
if(n % 2 == 0)
arr[1] = (n + 2) / 2;
else
arr[1] = (n + 3) / 2;

for(i = 2; i <= n + 1; i++) {


printf("[SERVER] Enter array element %d: ", i - 1);
scanf("%d", &arr[i]);
}
send(fb1, &arr, 100 * sizeof(int), 0);
recv(fb1, &arr, 100 * sizeof(int),0);
printf("\b[SERVER] Message Received from Client A!\n");
printf("\b[SERVER] Sorted array: ");
for(i = 2; i <= n + 1; i++)
printf("%d ", arr[i]);
printf("\n");

arr[0] = arr[1] + 1;
arr[1] = n + 1;
send(fb2, &arr, 100 * sizeof(int), 0);
recv(fb2, &arr, 100 * sizeof(int),0);
printf("\b[SERVER] Message Received from Client B!\n");
printf("\b[SERVER] Sorted array: ");
for(i = 2; i <= n + 1; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}

Client 1 :-

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int sockfd, len, i, t, j, k = 0, arr[100], temp[100], n;
char ch, msg[200];
struct sockaddr_in sa,ca;
sockfd=socket(AF_INET,SOCK_STREAM,0);
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=inet_addr("127.0.0.1");
sa.sin_port=1236;
len=sizeof(sa);
i= connect (sockfd,(struct sockaddr *) &sa,len);
if(i == -1) {
printf("[CLIENT] [%d %d] Connection cannot be Established :(\n", sockfd, i);
exit(i);
}
printf("[CLIENT] [%d %d] Connection Established Successfully!\n", sockfd, i);
recv(sockfd, &arr, 100 * sizeof(int),0);
for(i = arr[0]; i <= arr[1]; i++)
temp[k++] = arr[i];
for(i = 0; i < k - 1; i++) {
for(j = 0; j < k - i - 1; j++) {
if(temp[j] > temp[j + 1]) {
t = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = t;
}
}
}
k = 0;
for(i = arr[0]; i <= arr[1]; i++)
arr[i] = temp[k++];
send(sockfd, &arr, 100 * sizeof(int), 0);
printf("[CLIENT] Message sent to the Server successfully!\n");
return 0;
}

Client 2 :-

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int sockfd, len, i, t, j, k = 0, arr[100], temp[100];
char ch, msg[200];
struct sockaddr_in sa,ca;
sockfd=socket(AF_INET,SOCK_STREAM,0);
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=inet_addr("127.0.0.1");
sa.sin_port=2222;
len=sizeof(sa);
i= connect (sockfd,(struct sockaddr *) &sa,len);
if(i == -1) {
printf("[CLIENT] [%d %d] Connection cannot be Established :(\n", sockfd, i);
exit(i);
}
printf("[CLIENT] [%d %d] Connection Established Successfully!\n", sockfd, i);
recv(sockfd, &arr, 100 * sizeof(int),0);
for(i = arr[0]; i <= arr[1]; i++)
temp[k++] = arr[i];
for(i = 0; i < k - 1; i++) {
for(j = 0; j < k - i - 1; j++) {
if(temp[j] > temp[j + 1]) {
t = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = t;
}
}
}
k = 0;
for(i = arr[0]; i <= arr[1]; i++)
arr[i] = temp[k++];
send(sockfd, &arr, 100 * sizeof(int), 0);
printf("[CLIENT] Message sent to the Server successfully!\n");
return 0;
}

Output :-

Discussion:-In this program we have done Socket programming by using client-server and
specially in this program we get to know how to use multiple client-servers by using those
algorithms.
2) The client reads a number (x) and sends it to the server. The server sends 3x
and x^3 to the client.
Definition:- In this program we have to give a number from a user and then we have to
make it 3 times of that number and x^3 of that number by using Client Server by using
Socket programming it is an process which is not as complex as it thought before but
with the help of certain algorithm by using them it can be easily implemented.

Server :-
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int sockfd,fb,len,i,p,res[2];
struct sockaddr_in sa,ca;

sockfd=socket(AF_INET,SOCK_STREAM,0);
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=INADDR_ANY;
sa.sin_port=6001;

len=sizeof(sa);
i= bind (sockfd,(struct sockaddr *) &sa,len);
printf("(%d %d)\n",sockfd,i);
listen(sockfd,5);
fb= accept (sockfd, (struct sockaddr *) &ca,&len);
printf("[%d]\n",fb);
recv(fb,&p,4,0);
printf("Client sent %d\n ",p);
res[0]=p*3;
res[1]=p*p*p;
send(fb,res,50,0);
}

Client:-
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int sockfd,len,i,a,res[2];
struct sockaddr_in sa,ca;
sockfd=socket(AF_INET,SOCK_STREAM,0);
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=inet_addr("127.0.0.1");
sa.sin_port=6001;

len=sizeof(sa);
i= connect (sockfd,(struct sockaddr *) &sa,len);
printf("(%d %d)\n",sockfd,i);
printf("Give a number for the server");
scanf("%d",&a);
send(sockfd,&a,4,0);
recv(sockfd,res,50,0);
printf("Server sent %d\n%d\n",res[0],res[1]);
}

Output:-

Discussion:-Here we get to know how we can use socket programming using Server and
Client to get a number from which we can get attributes of 3 times X and X^3 and after that we
have done by using a certain algorithm in C.
5) The client and server both read a number and send it to each other. It is done till client
reads a number which is less than or equal to 50.

Definition:- In this program, we have to give a number from a user and then we have to
read a number which will be less than equal to 50. This problem is being done by socket
programming by using the concept of Client - Server.Where we have used a certain port
to connect those two programs.

Server:-
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main() {
int sockfd, fb, len, i, send_msg, recv_msg;
struct sockaddr_in sa,ca;
printf("[SERVER] Started Successfully!\n");
sockfd=socket(AF_INET,SOCK_STREAM,0);
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=INADDR_ANY;
sa.sin_port=6001;
len=sizeof(sa);
i= bind (sockfd,(struct sockaddr *) &sa,len);
if(i == -1) {
printf("[SERVER] [%d %d] Connection cannot be Established :(\n", sockfd, i);
exit(i);
}
printf("[SERVER] [%d %d] Connection Established Successfully!\n",sockfd,i);
listen(sockfd,5);
fb= accept (sockfd, (struct sockaddr *) &ca,&len);
while(1) {
recv(fb,&recv_msg, sizeof(recv_msg),0);
printf("\b[SERVER] Message Received from Client!\n");
printf("\b[SERVER] Incoming Message: %d\n", recv_msg);
printf("[SERVER] Enter a message for the Client: ");
scanf("%d", &send_msg);
send(fb, &send_msg, sizeof(send_msg), 0);
if(send_msg > 50) {
printf("[SERVER] Connection closed...\n");
exit(-1);
}
printf("\b[SERVER] Reply sent to the Client Successfully!\n");
}
return 0;
}

Client:-
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main()
{
int sockfd, len, i, send_msg, recv_msg;
struct sockaddr_in sa,ca;
printf("[CLIENT] Started Successfully!\n");
sockfd=socket(AF_INET,SOCK_STREAM,0);
sa.sin_family=AF_INET;
sa.sin_addr.s_addr=inet_addr("127.0.0.1");
sa.sin_port=6001;
len=sizeof(sa);
i= connect (sockfd,(struct sockaddr *) &sa,len);
if(i == -1) {
printf("[CLIENT] [%d %d] Connection cannot be Established :(\n", sockfd, i);
exit(i);
}
printf("[CLIENT] [%d %d] Connection Established Successfully!\n", sockfd, i);
while(1) {
printf("[CLIENT] Send a Message to Server: ");
scanf("%d", &send_msg);
send(sockfd,&send_msg, sizeof(int), 0);
printf("[CLIENT] Message sent to Server Successfully!\n");
recv(sockfd,&recv_msg, sizeof(int), 0);
if(recv_msg > 50) {
printf("[CLIENT] Connection closed...\n");
exit(-1);
}
printf("[CLIENT] Message from Server Received!\n");
printf("[CLIENT] Server Sends: %d\n", recv_msg);
}
return 0;
}
Output:-

Discussion:- We have to give a number from a user and then we have to read a
number which will be less than equal to 50.This problem is being done by socket
programming by using the concept of Client - Server. Where we have used a certain port
to connect those two programs.

You might also like