I'm trying to complete a basic socket program. I have a class which is the server and I have a client which is in a different class. The master is supposed to pass a given range of values to the client and find all the prime values in that range. But I'm having problems getting the server to pass the "starting" & "ending" variables to the client. Right now when I run the server, type in the values, followed by running the client it just sits there. If you see anything wrong with my code, please point it out because socket code is pretty ugly, thanks!
Here is my code for the server.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main() {
char* names[] = {"localhost"};
int ports[] = {7000};
int starting, ending;
printf("Enter starting & ending values: \n");
scanf("%d %d",&starting, &ending);
// Get a socket of the right type
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
printf("ERROR opening socket");
exit(1);
}
// port number
int portno = 7000;
// server address structure
struct sockaddr_in serv_addr;
// Set all the values in the server address to 0
memset(&serv_addr, '0', sizeof(serv_addr));
// Setup the type of socket (internet vs filesystem)
serv_addr.sin_family = AF_INET;
// Basically the machine we are on...
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// Setup the port number
// htons - is host to network byte order
// network byte order is most sig bype first
// which might be host or might not be
serv_addr.sin_port = htons(portno);
// Bind the socket to the given port
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("ERROR on binding\n");
exit(1);
}
// set it up to listen
listen(sockfd,5);
int newsockfd;
struct sockaddr_in cli_addr;
socklen_t clilen = sizeof(cli_addr);
// Wait for a call
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0) {
printf("ERROR on accept");
exit(1);
}
int buffer = 0;
memset(&buffer, '\0', sizeof(buffer));
bzero(&buffer,sizeof(buffer));
int n = read(newsockfd,&buffer,sizeof(buffer));
if (n < 0) {
printf("ERROR reading from socket\n");
exit(1);
}
read(newsockfd,&starting,sizeof(starting));
read(newsockfd,&ending,sizeof(ending));
printf("Number of primes found as master is: %d\n",buffer);
n = write(newsockfd,"I FOUND %D PRIME NUMBERS",sizeof(buffer));
if (n < 0) {
printf("ERROR writing to socket\n");
exit(1);
}
close(newsockfd);
close(sockfd);
return 0;
}
Here is my code for the client.
Code:
#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>
int isPrime(int num);
int main(){
// Socket pointer
int sockfd;
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0) {
fprintf(stderr,"ERROR opening socket\n");
exit(0);
}
// port number
int portno = 7000;
// server address structure
struct sockaddr_in serv_addr;
// Set all the values in the server address to 0
memset(&serv_addr, '0', sizeof(serv_addr));
// Setup the type of socket (internet vs filesystem)
serv_addr.sin_family = AF_INET;
// Setup the port number
// htons - is host to network byte order
// network byte order is most sig bype first
// which might be host or might not be
serv_addr.sin_port = htons(portno);
// Setup the server host address
struct hostent *server;
server = gethostbyname("localhost");
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length); /// dest, src, size
// Connect to the server
if (connect(sockfd,(struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
printf("ERROR connecting\n");
exit(0);
}
int starting;
int ending;
read(sockfd,&starting,sizeof(starting));
read(sockfd,&ending,sizeof(ending));
int buffer = 0;
int primeCount = 0;
printf("starting: %d ending: %d", starting, ending);
for(int i = starting; i < ending; i++){
if(isPrime(i)){
buffer++;
}
}
printf("Number of primes in the given range: %d\n", primeCount);
int n = write(sockfd,&buffer,sizeof(buffer));
if (n < 0) {
printf("ERROR writing to socket\n");
exit(0);
}
memset(&buffer, '\0', sizeof(buffer));
n = read(sockfd,&buffer,sizeof(buffer));
if (n < 0) {
printf("ERROR reading from socket\n");
exit(0);
}
close(sockfd);
return 0;
}
int isPrime(int num){
for (int i = 2; i < num; i++){
if (num % i == 0 && i != num) return 0;
}
return 1;
}