0% found this document useful (0 votes)
105 views6 pages

Cs 270 Assignment 3

Uploaded by

api-744667975
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)
105 views6 pages

Cs 270 Assignment 3

Uploaded by

api-744667975
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

CS 270 Assignment 3

Hunter Brazee
April 18th, 2024

1 Introduction
This program is a client-server program that will calculate a value based on the
operator and two numbers entered by the user. This calculator uses post-fix
notation. The expression is read on a single prompt on the client side, then the
server program will calculate and return the answer.

2 What is Needed
1. Setup server connection with a port
2. Connect client to the port
3. Have client read in two integers and operator from user
4. Use post-fix notation when typing in values.
5. Server calculate and return the answer.
6. Print the answer on the client side, then end the connection

3 Programming Log
3.1 Tuesday 4/17
I started by downloading the sample code provided on the Linux How To page.
Then I used the code and ran it on the server to test how the server and client
work together. After that, I modified the client to take the acceptable format
of two integers then operator, in post fix style. Then, I added a switch case to
the server to identify what operation is being used, and set up the computation
for each type of problem. Then, it is setup correctly for the client to print the
answer, which was calculated by the server. Overall I spent around four to five
hours working on the assignment, with messing around and testing the server
and client connections and implementing the calculator computation.

1
4 Things Learned/Encountered
Reading over the website given in the instructions for the assignment was very
helpful in learning about how the interaction between the server and client.
Using the initial code listed on the website was interesting to test how the
connection worked, and modifying the server side to do calculations and the
client side to accept the correct expressions helped show how the connections
work. I was very worried when testing the server that I would accidentally mess
it up and leave the server constantly running. Setting up the client to read the
input but not do calculations was also something new, and was interesting to
see how it worked of the server doing the calculations and sending the answer
back to the client.

5 Output
Script started on 2024-04-18 [Link]-07:00
bash-4.4$ ./client localhost ^H^[[K^H^[[K 4520
Enter the expression (operand1 operand2 operator): 4 2 +
Result: 6
bash-4.4$ ./client localhost 4520
Enter the expression (operand1 operand2 operator): 23 14 -
Result: 9
bash-4.4$ ./client localhost 4520
Enter the expression (operand1 operand2 operator): 3 7 *
Result: 21
bash-4.4$ ./client localhost 4520
Enter the expression (operand1 operand2 operator): 25 5 /
Result: 5
bash-4.4$ ./client localhost 4520
Enter the expression (operand1 operand2 operator): 14 5 %
Result: 4
exit

Script done on 2024-04-18 [Link]-07:00

6 Program Files
6.1 server.c
/*
Hunter Brazee
CS-270 Assignment 3
This file is server.c, which is the server compenent for the server/client assignment
This file takes in the input from the client,
and figures out which calculation needs to be made

2
Then this calculates the expression and returns the answer to the client
It can do addition, subtraction, multiplication, division and mod.
The server opens the socket, looks for the numbers,
calls the calculation, then closes the socket.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

void error(const char *msg)


{
perror(msg);
exit(1);
}

int performOperation(int num1, int num2, char operator)


{
switch(operator) { //Switch case for finding which operator is being used
case ’+’:
return num1 + num2;
case ’-’:
return num1 - num2;
case ’*’:
return num1 * num2;
case ’/’:
if(num2 == 0) { //Error for dividing by zero
fprintf(stderr, "ERROR: Division by zero\n");
exit(EXIT_FAILURE);
}
return num1 / num2;
case ’%’:
if(num2 == 0) { //Error message for doing mod 0
fprintf(stderr, "ERROR: Modulo by zero\n");
exit(EXIT_FAILURE);
}
return num1 % num2;
default: //Error for not typing in correct operators
fprintf(stderr, "ERROR: Invalid operator\n");
exit(EXIT_FAILURE);
}
}

3
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno;
socklen_t clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;

if (argc < 2) { //Checks for no port number given


fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}

sockfd = socket(AF_INET, SOCK_STREAM, 0); //Creates socket


if (sockfd < 0)
error("ERROR opening socket");

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


portno = atoi(argv[1]);

serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

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


error("ERROR on binding");

listen(sockfd, 5); //Accepts incoming connections


clilen = sizeof(cli_addr);

while(1) { //Waiting for incoming connection


newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");

bzero(buffer, 256);
n = read(newsockfd, buffer, 255); //Reads the data from client
if (n < 0)
error("ERROR reading from socket");

int num1, num2;


char operator;
sscanf(buffer, "%d %d %c", &num1, &num2, &operator);
//Calls computation function
int result = performOperation(num1, num2, operator);

4
bzero(buffer, 256);
sprintf(buffer, "%d", result);

n = write(newsockfd, buffer, strlen(buffer)); //Writes back to the client


if (n < 0)
error("ERROR writing to socket");

close(newsockfd); //Closes the socket


}

close(sockfd);
return 0;
}

6.2 client.c
/*
Hunter Brazee
CS-270 Assignment 3
This file is client.c, which is the client compenent for the server/client assignment
This file takes an input from a user, and sends it to the server
The input will be a postfix notation for an expression to solve
The server will solve the expression,
then send the answer back to the client, which is then printed
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

void error(const char *msg)


{
perror(msg);
exit(EXIT_FAILURE);
}

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


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

5
char buffer[256];
if (argc < 3) { //Prints error message if port and host not typed correctly
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(EXIT_FAILURE);
}

portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) //Error message when opening the socket
error("ERROR opening socket");

server = gethostbyname(argv[1]);
if (server == NULL) { //Error when using non-valid host
fprintf(stderr,"ERROR, no such host\n");
exit(EXIT_FAILURE);
}
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("ERROR connecting");

printf("Enter the expression (operand1 operand2 operator): ");


//Enter integers and operator
bzero(buffer,256);
fgets(buffer,255,stdin);

n = write(sockfd, buffer, strlen(buffer)); //Writes the expression to the server

if (n < 0) //Error message when writing to the socket


error("ERROR writing to socket");

bzero(buffer,256);
n = read(sockfd, buffer, 255); //Reads in the answer from the server

if (n < 0) //Error when reading from the socket


error("ERROR reading from socket");

printf("Result: %s\n", buffer); //Prints the results from the server

close(sockfd);
return 0;
}

You might also like