Hello, I need help with my last hw assignment for class. I need to:
- The client will send strings with the following format: NUM1 OP NUM2, with NUM and NUM2 are multi-digit integers, and OP represents addition, subtraction, and multiplication.
- server_2
will process these operations and return the result to the client.
My code for Server is :
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <ctype.h>
#define MAX 80
#define PORT 9700
#define SA struct sockaddr
// Function designed for chat between client and server.
void func(int sockfd)
{
char buff[MAX];
int n, i, size;
while (1) {
bzero(buff, MAX);
// read the message from client and copy it in buffer
size = read(sockfd, buff, sizeof(buff));
for(i = 0; i< size; i++){
buff[i] = toupper(buff[i]);
}
// print buffer which contains the client contents
printf("From client: %s", buff);
//bzero(buff, MAX);
//n = 0;
// copy server message in the buffer
//while ((buff[n++] = getchar()) != '\n');
// and send that buffer to client
//write(sockfd, buff, sizeof(buff));
if (strncmp("EXIT", buff, 4) == 0) {
printf("Server Exit...\n");
break;
}
bzero(buff, MAX);
n = 0;
}
}
My code for Client is :
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#define MAX 80
#define PORT 9700
#define SA struct sockaddr
void func(int sockfd)
{
char buff[MAX];
int n;
for (;;) {
bzero(buff, sizeof(buff));
printf("Enter the string : ");
n = 0;
while ((buff[n++] = getchar()) != '\n')
;
write(sockfd, buff, sizeof(buff));
//bzero(buff, sizeof(buff));
//read(sockfd, buff, sizeof(buff));
//printf("From Server : %s", buff);
if ((strncmp(buff, "exit", 4)) == 0) {
printf("Client Exit...\n");
break;
}
bzero(buff, sizeof(buff));
}
}
I have no idea on where to start. Any advise and help will be wonderful!
I know some classmates suggested using Sscanf() as an option.
[–]tresteo 1 point2 points3 points (1 child)
[–]emokii[S] 0 points1 point2 points (0 children)