Thread: Basic socket help needed

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    100

    Basic socket help needed

    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;
        }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by johngoodman View Post
    Right now when I run the server, type in the values, followed by running the client it just sits there.
    In your client code you wait for "starting" and "ending" from the server (lines 58 and 59) but the server itself waits for something to read (read() on line 72; only after two other read()'s on lines 77 and 78 you have a write() on line 81).

    Code:
    // Set all the values in the server address to 0
    memset(&serv_addr, '0', sizeof(serv_addr));
    The comment is wrong. This call doesn't set all bytes to 0. It's setting all bytes to 48 (the ASCII code of the character '0').

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    100
    Quote Originally Posted by AndiPersti View Post
    In your client code you wait for "starting" and "ending" from the server (lines 58 and 59) but the server itself waits for something to read (read() on line 72; only after two other read()'s on lines 77 and 78 you have a write() on line 81).

    Code:
    // Set all the values in the server address to 0
    memset(&serv_addr, '0', sizeof(serv_addr));
    The comment is wrong. This call doesn't set all bytes to 0. It's setting all bytes to 48 (the ASCII code of the character '0').

    Bye, Andreas
    Is there a way of passing all the variables I want from the server to the client in one read? Otherwise, yes, I see what you have pointed out but I don't really know how to properly pass the values over to the client.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Personally, I would suggest you read beej, then write a client/server pair test program to send and receive a single integer.

    When that works, you can readily extend it to any sized message you want.

    > Is there a way of passing all the variables I want from the server to the client in one read?
    You're using SOCK_STREAM, so the only guarantees are that bytes sent are received in the order they are sent.

    These are all valid permutations:
    S: "hello world"
    R: "hell"
    R: "o"
    R: " world"

    S: "he"
    S: "llo "
    S: "world"
    R: "hello world"

    While you're talking to localhost, you'll probably never see fragmentation.
    But try talking to some remote server on the other side of the planet out in the boondock's, then you're probably going to need to pay attention to it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I have a problem with basic socket programming using TCP
    By Derek Lake in forum C Programming
    Replies: 4
    Last Post: 01-21-2013, 07:11 PM
  2. Basic thread and socket question
    By tomjava2008 in forum C Programming
    Replies: 1
    Last Post: 05-25-2012, 09:35 PM
  3. Help Needed please! (Basic)
    By Acer in forum C Programming
    Replies: 2
    Last Post: 10-05-2010, 12:21 PM
  4. Basic socket programming
    By cnewbie1 in forum C Programming
    Replies: 1
    Last Post: 03-28-2010, 05:10 PM
  5. Urgent help needed in Socket Programming
    By lisa23 in forum C Programming
    Replies: 2
    Last Post: 09-15-2005, 11:18 PM