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

2021UCP1387_CN_Assign-1

Uploaded by

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

2021UCP1387_CN_Assign-1

Uploaded by

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

Computer Networks

Naveen chouhan
2021ucp1387
Question 1.
Chat bot between server and client.
Server code:
#include <stdio.h> // Standard header file for C programs
#include <stdlib.h> // Different library, macros, and built-in functions
#include <string.h> // String manipulation functions
#include <unistd.h> // POSIX operating system API
#include <sys/types.h> // Definitions of data types used in the system
#include <sys/socket.h> // Socket programming structures and functions
#include <netinet/in.h> // Constants and structures for internet operations

void error(const char *msg){


perror(msg); // Print an error message and error description
exit(1); // Terminate the program with an error code
}

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

if(argc < 2){


fprintf(stderr, "Port No not provided!! Program terminated\n");
exit(1);
}

int sockfd, newsockfd, portno;


char buffer[255]; // Message to be sent or received

struct sockaddr_in serv_addr, cli_addr;


socklen_t clilen;

// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
error("Error opening socket");
}

// Clear the serv_addr structure


bzero((char*) &serv_addr, sizeof(serv_addr));

portno = atoi(argv[1]);

serv_addr.sin_family = AF_INET; // Set the address family to IPv4


serv_addr.sin_addr.s_addr = INADDR_ANY; // Allow connections from any IP address
serv_addr.sin_port = htons(portno); // Set the port number

// Bind the socket to a specific address and port


if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
error("Binding failed");
}

// Listen for incoming connections, allowing up to 5 pending connections


listen(sockfd, 5);
clilen = sizeof(cli_addr);

// Accept a connection request and create a new socket for communication


newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);

if(newsockfd < 0){


error("Error on accepting");
}

while(1){
bzero(buffer, 255);
int n = read(newsockfd, buffer, 255); // Read data from the client

if(n < 0){


error("Error on reading");
}

printf("Client: %s\n", buffer);

bzero(buffer, 255);
fgets(buffer, 255, stdin); // Get input from the user

// Send the user input to the client


n = write(newsockfd, buffer, strlen(buffer));
if(n < 0){
error("Error on writing");
}

int i = strncmp("Bye", buffer, 3);


if(i == 0){
break; // Exit the loop if "Bye" is received from the client
}
}

// Close the sockets and terminate the program


close(newsockfd);
close(sockfd);

return 0;
}

Client code:
#include <stdio.h> // Standard header file for C programs
#include <stdlib.h> // Standard library functions
#include <string.h> // String manipulation functions
#include <unistd.h> // POSIX operating system API
#include <sys/types.h> // Definitions of data types used in the system
#include <sys/socket.h> // Socket programming structures and functions
#include <netinet/in.h> // Constants and structures for internet operations

void error(const char *msg){


perror(msg); // Print an error message and error description
exit(1); // Terminate the program with an error code
}

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

if(argc < 2){


fprintf(stderr, "Port No not provided!! Program terminated\n");
exit(1);
}

int sockfd, newsockfd, portno;


char buffer[255]; // Message to be sent or received

struct sockaddr_in serv_addr, cli_addr;


socklen_t clilen;
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
error("Error opening socket");
}

// Clear the serv_addr structure


bzero((char*) &serv_addr, sizeof(serv_addr));

portno = atoi(argv[1]);

serv_addr.sin_family = AF_INET; // Set the address family to IPv4


serv_addr.sin_addr.s_addr = INADDR_ANY; // Allow connections from any IP address
serv_addr.sin_port = htons(portno); // Set the port number

// Bind the socket to a specific address and port


if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
error("Binding failed");
}

// Listen for incoming connections, allowing up to 5 pending connections


listen(sockfd, 5);
clilen = sizeof(cli_addr);

// Accept a connection request and create a new socket for communication


newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen);

if(newsockfd < 0){


error("Error on accepting");
}

while(1){
bzero(buffer, 255);
int n = read(newsockfd, buffer, 255); // Read data from the client

if(n < 0){


error("Error on reading");
}

printf("Client: %s\n", buffer);

bzero(buffer, 255);
fgets(buffer, 255, stdin); // Get input from the user

// Send the user input to the client


n = write(newsockfd, buffer, strlen(buffer));
if(n < 0){
error("Error on writing");
}

int i = strncmp("Bye", buffer, 3);


if(i == 0){
break; // Exit the loop if "Bye" is received from the client
}
}

// Close the sockets and terminate the program


close(newsockfd);
close(sockfd);
return 0;
}
Output:

Question 2.
Calculator:
Server code:
#include <stdio.h> // Standard header file for C programs
#include <stdlib.h> // Different library, macros, and built-in functions
#include <string.h> // String manipulation functions
#include <unistd.h> // POSIX operating system API
#include <sys/types.h> // Definitions of data types used in the system
#include <sys/socket.h> // Socket programming structures and functions
#include <netinet/in.h> // Constants and structures for internet operations

void error(const char *msg){


perror(msg); // Print an error message and error description
exit(1); // Terminate the program with an error code
}

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

if(argc < 2){


fprintf(stderr, "Port No not provided!! Program terminated\n");
exit(1);
}

int sockfd, newsockfd, portno, n;


char buffer[255]; // Message to be sent or received

struct sockaddr_in serv_addr, cli_addr;


socklen_t clilen;

sockfd = socket(AF_INET, SOCK_STREAM, 0); // Create a socket


if(sockfd < 0){
error("Error opening socket");
}

bzero((char*) &serv_addr, sizeof(serv_addr)); // Clear the serv_addr structure

portno = atoi(argv[1]);

serv_addr.sin_family = AF_INET; // Set the address family to IPv4


serv_addr.sin_addr.s_addr = INADDR_ANY; // Allow connections from any IP address
serv_addr.sin_port = htons(portno); // Set the port number

// Bind the socket to a specific address and port


if(bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
error("Binding failed");
}

listen(sockfd, 5); // Listen for incoming connections, allowing up to 5 pending connections


clilen = sizeof(cli_addr);

newsockfd = accept(sockfd, (struct sockaddr*)&cli_addr, &clilen); // Accept a connection request

if(newsockfd < 0){


error("Error on Accepting");
}

int num1, num2, ans, choice;

S: n = write(newsockfd, "Enter Number 1: ", strlen("Enter Number 1")); // Prompt user for number 1
if(n < 0){
error("Error Writing to socket");
}
read(newsockfd, &num1, sizeof(int)); // Read number 1 from the client
printf("Client - Number 1 is : %d\n", num1);

n = write(newsockfd, "Enter Number 2: ", strlen("Enter Number 2")); // Prompt user for number 2
if(n < 0){
error("Error Writing to socket");
}
read(newsockfd, &num2, sizeof(int)); // Read number 2 from the client
printf("Client - Number 2 is : %d\n", num2);

n = write(newsockfd, "Enter the choice:


\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit\n",
strlen("Enter your choice : Enter the choice:
\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division\n5.Exit\n"));
read(newsockfd, &choice, sizeof(int)); // Read the user's choice

printf("Client-choice is:%d\n", choice);

switch(choice) {
case 1:
ans = num1 + num2;
break;
case 2:
ans = num1 - num2;
break;
case 3:
ans = num1 * num2;
break;
case 4:
ans = num1 / num2;
break;
case 5:
goto Q; // Exit the program
break;
}

write(newsockfd, &ans, sizeof(int)); // Send the result to the client

if(choice != 5){
goto S; // Go back to the start to perform another operation
}

Q: close(newsockfd); // Close the sockets


close(sockfd);

return 0;
}
Client code:
#include <stdio.h> // Standard header file for C programs
#include <stdlib.h> // Standard library functions
#include <string.h> // String manipulation functions
#include <unistd.h> // POSIX operating system API functions
#include <sys/types.h> // Definitions of data types used in the system
#include <sys/socket.h> // Socket programming structures and functions
#include <netinet/in.h> // Constants and structures for internet operations
#include <netdb.h> // Functions and structures for working with networked hosts

void error(const char *msg){


perror(msg); // Print an error message and error description
exit(1); // Terminate the program with an error code
}

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


int sockfd , portno , n;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[255];
if(argc < 3){
fprintf(stderr , "Port No not provided !! Program terminated\n");
exit(1);
}

portno = atoi(argv[2]);
sockfd = socket(AF_INET , SOCK_STREAM , 0);

if(sockfd < 0){


error("Error in opening Socket");
}

server = gethostbyname(argv[1]);
if(server == NULL){
fprintf(stderr , "Error, No such host");
}

bzero((char*)&serv_addr , sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char*) server->h_addr , (char*) &serv_addr.sin_addr.s_addr , server->h_length);
serv_addr.sin_port = htons(portno);

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


error("Connection Failed");
}

int num1 , num2, choice, ans;

S: bzero(buffer, 255);
n = read(sockfd, buffer, 255);
if(n < 0){
error("Error in Reading From Socket");
}
printf("Server - %s\n", buffer);
scanf("%d", &num1);
write(sockfd, &num1, sizeof(int));

bzero(buffer, 255);
n = read(sockfd, buffer, 255);
if(n < 0){
error("Error in Reading From Socket");
}
printf("Server - %s\n", buffer);
scanf("%d", &num2);
write(sockfd, &num2, sizeof(int));

bzero(buffer, 255);
n = read(sockfd, buffer, 255);
if(n < 0){
error("Error in Reading From Socket");
}
printf("Server - %s\n", buffer);
scanf("%d", &choice);
write(sockfd, &choice, sizeof(int));

if(choice == 5){
goto Q;
}
read(sockfd, &ans, sizeof(int));
printf("Server - the answer is : %d\n", ans);

if(choice != 5){
goto S;
}

Q: printf("Exit Successfully");
close(sockfd);
return 0;
}

Output:

Question 3:
Expression calculator:
Server code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

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


int server_socket, client_socket;
struct sockaddr_in server_address, client_address;
socklen_t client_address_size = sizeof(client_address);

// Create a TCP socket for the server


server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
perror("Error creating server socket");
return 1;
}

server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = INADDR_ANY;
int portno = atoi(argv[1]);
server_address.sin_port = htons(portno);

// Bind the socket


if (bind(server_socket, (struct sockaddr *)&server_address,
sizeof(server_address)) == -1) {
perror("Error binding socket");
return 1;
}

// Listen for incoming connections


if (listen(server_socket, 5) == -1) {
perror("Error listening");
return 1;
}

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

// Accept a new connection from the client


client_socket = accept(server_socket, (struct sockaddr *)&client_address,
&client_address_size);
if (client_socket == -1) {
perror("Error accepting connection");
close(server_socket);
return 1;
}

printf("Connection established with client\n");

// Send message to client


char expression_msg[] = "Enter Expression: ";
int send_count = send(client_socket, expression_msg, sizeof(expression_msg), 0);

// Receive response from client


char response[1024];
int receive_count = recv(client_socket, response, sizeof(response), 0);
printf("Client response: %s\n", response);

int num1 = 0, num2 = 0;


int len = sizeof(response);
char op;
int f1 = 1, f2 = 0;
printf("len -> %d \n", len);

int i = 0;
while (response[i] != '\0') {
char ch = response[i++];
if (ch - '0' >= 0 && ch - '9' <= 9 && f1) {
num1 = 10 * num1 + (ch - '0');
} else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
op = ch;
f1 = 0;
f2 = 1;
} else if (ch - '0' >= 0 && ch - '9' <= 9 && f2) {
num2 = num2 * 10 + (ch - '0');
}
}

printf("First Number is : %d\n", num1);


printf("Operation is : %c\n", op);
printf("Second Number is : %d\n", num2);

int result;
switch (op) {
case '+':
result = num1 + num2;
break;

case '-':
result = num1 - num2;
break;

case '*':
result = num1 * num2;
break;

case '/':
result = num1 / num2;
break;

default:
break;
}

printf("Result is : %d\n", result);

// Send result to client


char result_msg[1024];
// Convert int to string
snprintf(result_msg, sizeof(result_msg), "%d", result);
send(client_socket, result_msg, sizeof(result_msg), 0);
printf("Result is : %s\n", result_msg);

// Close sockets
close(client_socket);
close(server_socket);

return 0;
}

Client code:.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>

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


int client_socket;
struct sockaddr_in server_address;

// Create a TCP socket for the client


client_socket = socket(AF_INET, SOCK_STREAM, 0);
if (client_socket == -1) {
perror("Error creating client socket");
return 1;
}

// Set family name for server address


server_address.sin_family = AF_INET;

// Set IP address of server


//int x = atoi(argv[1]);
server_address.sin_addr.s_addr = inet_addr(argv[1]);

// Set port number for server address

server_address.sin_port = htons(atoi(argv[2]));
// Connect to the server
if (connect(client_socket, (struct sockaddr*)&server_address,
sizeof(server_address)) == -1) {
perror("Error connecting to server");
close(client_socket);
return 1;
}

// Receive msg from server


char msg[1024] ;
recv(client_socket, msg, sizeof(msg), 0) ;
printf("%s ", msg) ;
char response[1024] ;
scanf("%s", response) ;

// send response to server

// char response[] = "35+100" ;


int receive_count = send(client_socket, response, sizeof(response), 0) ;
//printf("client response: %s", response) ;

// Receive expression rsult from server


char exp_result[1024] ;
recv(client_socket, exp_result, sizeof(exp_result), 0) ;
printf("expression result: %s\n", exp_result) ;

// close sockets
close(client_socket) ;
//close(server_sockId) ;

return 0;
}
Output:

You might also like