Thread: How to fix this error for my server code.

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    15

    How to fix this error for my server code.

    I have a code but I have searched for over 2 hours on how to fix the error it gives me but I have no luck. The errors have to deal with implicit declaration of pthread . I will attach a screen shot. Pls and thank you(I tried to indent my code)


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <string.h>
    #include <errno.h>
    #include <unistd.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    
    
    void * client_service_thread(void*arg);
    
    
    
    
    int main(int argc, char **argv )
       {
            // sd is the server socket descriptor. csd is client socket descriptor
            int sd, csd, status, on , queueLenghth = 10;
            struct addrinfo request, *result, *p;
            struct sockaddr_ln clientaddr;
            //struct sockaddr_ln clientaddr;
            socklen_t addrlen;
           
           
    
    
         request. ai_flags = AI_PASSIVE; // use my IP
         request.ai_socktype = SOCK_STREAM; //  want TCP/IP  connection_based communication.
         request.ai_protocol = 0;
         request.ai_addrlen = 0;
         request.ai_addr = NULL;
         request.ai_canonname = NULL; 
         request.ai_next = NULL;
         request.ai_family = AF_INET;// want to use IPV4 32 bit IP addrress
    
    
                // get ready to connect
        status = getaddrinfo(0,"4008",&request, &result );
            // result now points to addrinfo
            if (status != 0) {
    
    
                    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
                    exit(EXIT_FAILURE);
            }
            
        for (p = result; p != NULL; p = p->ai_next) 
        {
            
             sd = socket(result->ai_family, result->ai_socktype, result-> ai_protocol);
             
             if (sd == -1) {
                 perror("socket");
                continue;
             }
                
             on = 1;
             // make port reusable by eliminating the adress already in use.
             if(setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,&on,sizeof (on)) != 0) {
                 perror("setsockopt");
                 close(sd);
                 continue;
             }
              // bind the descriptor to the address
              if (bind(sd, p->ai_addr,p->ai_addrlen) == -1) {
                  close(sd);
                  perror("server: bind");
                  continue;
             }  
              
              break;
            
        }
             
         freeaddrinfo(result);// free because we done using it
             
             if (p == NULL) {
                 fprintf(stderr, "server: faiuled to bind\n");
                 exit(1);
             }
             
             if (listen(sd,10) == -1){
                 perror("listen");
                 exit(1);
             }
             
        printf("server: waiting for connections...\n");
             
             
             addrlen = sizeof(clientaddr);
             
             
             while((csd = accept(sd,(struct sockaddr *)&clientaddr, &addrlen)) !=-1){
                 
                  pthread_t tid;
                 
                  int * csdptr;
                 
                  csdptr = malloc(sizeof(csd));
                 *csdptr = csd;
                 
                 pthread_create(&tid,NULL, client_service_thread,csdptr);
                 
             }
             
             if (csd){
                 
             }
        
                     
       }      
       
       // for multithreading
       void * client_service_thread(void*arg){
           int csd;
           csd = *((int*)arg);
           free(arg);
           pthread_detach(pthread_self());
           
           
           
           
           
           
           
           
           
           
       }
    Attached Images Attached Images How to fix this error for my server code.-error-message-png 

  2. #2
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    I think you mean sockaddr_in, not sockaddr_ln. You also need to include pthread.h if you want to use threads.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    ok i will try that. i coulda sworn I include dpthread.h

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    i need the -lpthread to complie right?

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    I think that actually fixed it . I do not see any errors but my printf("server: waiting for connections...\n"); is not showing

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    The first thing I would do is get rid of threads. Threads massively complicate things, and if you're just trying to get a basic server running then that's something you don't need. At first, just handle one connection at a time, then move to multiplexing using select. The purpose here is to enable you to get to my second suggestion.

    The second thing I would do is use a debugger. Single stepping through your code and examining things as they happen is absolutely vital to understanding what's happening. I cannot imagine programming without a debugger when you run into situations like this. I've had a lot of luck with the ddd graphical debugger in the past, it should be easy to learn how to use.

  7. #7
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    yeah i dont know why we have not been thought how to use a bugger yet. I will probably teach myself this summer. The multi threading part was my professor that said we should do it like that.

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    Well if it's a requirement of the assignment then you should, but I would get just the network code working without threading first, then add it after.

  9. #9
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    ok will do

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    57
    You're also using old-style struct initialization. The new style is much easier to read.

    Code:
    struct addrinfo request = {
      .ai_flags = AI_PASSIVE,     // use my IP
      .ai_socktype = SOCK_STREAM, // want TCP/IP connection_based communication.
      .ai_family = AF_INET        // want to use IPV4 32 bit IP addrress
    };
    Any fields not listed there are initialized to 0, and everything is generally much more readable. It has nothing to do with the problem you're having, it's just not something you're going to see often googling around for this stuff because most of the material out there was written... well, a really, really long time ago.

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    473
    Why are you calling getaddrinfo() if you aren't resolving any names? For your server you can simply do:

    Code:
    // change .sin_addr.s_addr = htonl(addr) if you want
    // to listen to a specific interface. Here I'm using 0.0.0.0 (ANY).
    struct sockaddr_in sin = {
      .sin_port = htons(4008)
    };
    
    ...
    bind( fd, (struct sockaddr *)&sin, sizeof sin );
    What you'll get using getaddrinfo() the way you did is simply obtain the ANY (0.0.0.0) address. To get the interfaces addresses isn't portable, here an example for Linux:

    Code:
    #include <arpa/inet.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <ifaddrs.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    
    int main ( void )
    {
      struct ifaddrs *ifaddr, *p;
      int status;
    
      if ( getifaddrs ( &ifaddr ) )
      {
        perror ( "getifaddrs" );
        return EXIT_FAILURE;
      }
    
      for ( p = ifaddr; p; p = p->ifa_next )
        if ( p->ifa_addr && p->ifa_addr->sa_family == AF_INET )
        {
          char host[16];
    
          // You can use p->ifa_addr directly! 
          // getnameinfo() is similar to inet_ntop()...
          if ( status = getnameinfo ( p->ifa_addr, sizeof ( struct sockaddr_in ), 
                                      host, sizeof host,
                                      NULL, 0, 
                                      NI_NUMERICHOST ) )
          {
            fprintf( stderr, "getnameinfo: %s\n", gai_strerror( status ) );
            freeifaddrs( ifaddr );
            return EXIT_FAILURE;
          }
    
          printf ( "Interface : %s\n"
                   "  Address : %s\n", p->ifa_name, host );
        }
    
      freeifaddrs ( ifaddr );
      return EXIT_SUCCESS;
    }
    Compile and run:

    Code:
    $ cc -o test test.c
    $  ./test
    Interface : lo
      Address : 127.0.0.1
    Interface : eth0
      Address : 192.168.25.2

  12. #12
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    yeah i will implement the getnameinfo

  13. #13
    Registered User
    Join Date
    Mar 2019
    Posts
    15
    my professor gave me that part of the code

  14. #14
    Registered User
    Join Date
    Feb 2019
    Posts
    473
    Quote Originally Posted by Bossuche9 View Post
    yeah i will implement the getnameinfo
    I've show this to you as an example. Don't recommend it because the interfaces names change from system to system... Here, in my machine (Linux), my network card interface is enp3s0, not eth0 (I did some editing in the previous message!) - Virtual machines name their own interfaces in a different way... Containers, like those of docker, also do the same...

    Use 0.0.0.0, as the first example, or inform the desired IP address from command line. It is better. As I said, getting the IP for the NIC isn't portable (each OS do in a different way - I think, but not sure, that code using getnameinfo() will not work on MacOS, for example)...
    Last edited by flp1969; 04-21-2019 at 09:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C code along with server
    By santarus in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-22-2011, 09:51 AM
  2. chat server error
    By cute_kuddi in forum Linux Programming
    Replies: 2
    Last Post: 06-09-2011, 10:47 PM
  3. Help with server code !!!
    By Gaurav Singh in forum Networking/Device Communication
    Replies: 5
    Last Post: 03-21-2011, 09:04 AM
  4. X server slaps some template code with a "fatal IO error"
    By Yarin in forum Linux Programming
    Replies: 8
    Last Post: 08-24-2009, 08:47 AM
  5. So this is the code for the client and server
    By Bharat_kumarr in forum C++ Programming
    Replies: 14
    Last Post: 09-09-2004, 10:42 AM

Tags for this Thread