2021UCP1387_CN_Assign-1
2021UCP1387_CN_Assign-1
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
// Create a socket
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd < 0){
error("Error opening socket");
}
portno = atoi(argv[1]);
while(1){
bzero(buffer, 255);
int n = read(newsockfd, buffer, 255); // Read data from the client
bzero(buffer, 255);
fgets(buffer, 255, stdin); // Get input from the user
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
portno = atoi(argv[1]);
while(1){
bzero(buffer, 255);
int n = read(newsockfd, buffer, 255); // Read data from the client
bzero(buffer, 255);
fgets(buffer, 255, stdin); // Get input from the user
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
portno = atoi(argv[1]);
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);
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;
}
if(choice != 5){
goto S; // Go back to the start to perform another operation
}
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
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);
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);
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>
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);
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');
}
}
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;
}
// 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>
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;
}
// close sockets
close(client_socket) ;
//close(server_sockId) ;
return 0;
}
Output: