Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

ASSIGNMENT 5

Q. tcp server and client to program in c for mathematical calculation.


//Tcp server//
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>

int priority(char op)


{
if (op == '(' || op == ')')
return 0;
if (op == '+' || op == '-')
return 1;
if (op == '*' || op == '/')
return 2;
return -1;
}

void evaluate(char *expression, char *result)


{
int len = strlen(expression);
char stack_operators[len];
int top_op = -1;

char stack_values[len];
int top_val = -1;

for (int i = 0; i < len; i++)


{
if (expression[i] == '(')
{
stack_operators[++top_op] = '(';
}
else if (expression[i] == ')')
{
while (top_op >= 0 && stack_operators[top_op] != '(')
{
char op = stack_operators[top_op--];
int num2 = stack_values[top_val--] - '0';
int num1 = stack_values[top_val--] - '0';
int res;
switch (op)
{
case '+':
res = num1 + num2;
break;
case '-':
res = num1 - num2;
break;
case '*':
res = num1 * num2;
break;
case '/':
res = num1 / num2;
break;
default:
break;
}
stack_values[++top_val] = res + '0';
}
top_op--; // remove '(' from stack
}
else if (expression[i] >= '0' && expression[i] <= '9')
{
stack_values[++top_val] = expression[i];
}
else
{
while (top_op >= 0 && priority(stack_operators[top_op]) >= priority(expression[i]))
{
char op = stack_operators[top_op--];
int num2 = stack_values[top_val--] - '0';
int num1 = stack_values[top_val--] - '0';
int res;
switch (op)
{
case '+':
res = num1 + num2;
break;
case '-':
res = num1 - num2;
break;
case '*':
res = num1 * num2;
break;
case '/':
res = num1 / num2;
break;
default:
break;
}
stack_values[++top_val] = res + '0';
}
stack_operators[++top_op] = expression[i];
}
}

while (top_op >= 0)


{
char op = stack_operators[top_op--];
int num2 = stack_values[top_val--] - '0';
int num1 = stack_values[top_val--] - '0';
int res;
switch (op)
{
case '+':
res = num1 + num2;
break;
case '-':
res = num1 - num2;
break;
case '*':
res = num1 * num2;
break;
case '/':
res = num1 / num2;
break;
default:
break;
}
stack_values[++top_val] = res + '0';
}

sprintf(result, "%d", stack_values[0] - '0');


}

int main()
{
int sock, connected, bytes_sent, bytes_received;
bool trueValue = true;

char recv_data[1024];
char send_data[1024];
struct sockaddr_in server_addr, client_addr;
int sin_size;

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


{
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(server_addr.sin_zero), 8);
if (bind(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{
perror("Unable to bind");
exit(1);
}
if (listen(sock, 5) == -1)
{
perror("Listen");
exit(1);
}
printf("\nTCPServer Waiting for client on port 5000");
fflush(stdout);
// while (1)
// {
sin_size = sizeof(struct sockaddr_in);
connected = accept(sock, (struct sockaddr *)&client_addr, &sin_size);
printf("\n I got a connection from (%s , %d)", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));

// Receive expression from the client


bytes_received = recv(connected, recv_data, 1024, 0);
recv_data[bytes_received] = '\0';
printf("\nExpression received from client: %s", recv_data);
// Evaluate the expression
char result[1024];
evaluate(recv_data, result);
printf("Evaluated result : %s\n", result);
// Send the result back to the client
bytes_sent = send(connected, result, strlen(result), 0);
if (bytes_sent == -1)
perror("Error sending data to client");
close(connected);
close(sock);
return 0;
}
//tcp client //
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int main()
{int sock, bytes_received;
char send_data[1024], recv_data[1024];
struct hostent *host;
struct sockaddr_in server_addr;
host = gethostbyname("127.0.0.1");
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{perror("Socket");
exit(1);}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(8080);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero), 8);
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
{perror("Connect");
exit(1);}
printf("Enter expression (e.g., 5 + 3): ");
fflush(stdout);
fgets(send_data, sizeof(send_data), stdin);
// Send expression to server
if (send(sock, send_data, strlen(send_data), 0) == -1)
{
perror("Error sending data to server");
close(sock);
exit(1);}
bytes_received = recv(sock, recv_data, 1024, 0);
if (bytes_received == -1)
{ perror("Error receiving data from server");
close(sock);
exit(1); }
recv_data[bytes_received] = '\0';
printf("Result: %s\n", recv_data);
close(sock);
return 0;
}

You might also like