Thread: client-server prog not working and further questions

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    40

    client-server prog not working and further questions

    client.c
    reset; gcc -g -Wall -ansi -pedantic client.c -o tt
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdlib.h> 
    #include <signal.h>
    #include <unistd.h>
    
    int main(void)
    {
      int fd,  ret, nread;
      struct sockaddr_in addr;
      char buffer[50]="aa\nbb\ncc\ndd\nee\n";
      socklen_t addrlen;
        char * pch;
        
    strcat(buffer,"LST\n");
    strcat(buffer,"555\n");
      if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1) exit(1);
      
      memset((void*)&addr,(int)'\0',sizeof(addr));
      addr.sin_family=AF_INET;
      inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr));
      /*addr.sin_addr.s_addr=htonl(INADDR_ANY); * ???*/
      addr.sin_port=htons(9000);
    
      
      printf("strlen(buffer) =%d",strlen(buffer));
          ret=sendto(fd,buffer, strlen(buffer),0,(struct sockaddr*)&addr,addrlen);
        if(ret==-1)exit(1);
        
            pch = strtok (buffer,"\n");
          while (pch != NULL)
          {
        printf ("strtok = %s\n",pch);
        pch = strtok (NULL,"\n");
          }  
        
        return 0;
      
      while(1)
      {
        addrlen=sizeof(addr);
        nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
        if(nread==-1)exit(1);
        
        ret=sendto(fd,buffer,nread,0,(struct sockaddr*)&addr,addrlen);
        if(ret==-1)exit(1);
      }
    }
    server.c
    reset; gcc -g -Wall -ansi -pedantic server.c -o tt
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdlib.h> 
    #include <signal.h>
    #include <unistd.h>
    
    int main(void)
    {
      int fd,  ret, nread;
      struct sockaddr_in addr;
      char buffer[128];
      socklen_t addrlen;
      char * pch;
      
      if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1) exit(1);
      memset((void*)&addr,(int)'\0',sizeof(addr));
      addr.sin_family=AF_INET;
      inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr));
      /*addr.sin_addr.s_addr=htonl(INADDR_ANY); * ???*/
      addr.sin_port=htons(9000);
      
      ret=bind(fd,(struct sockaddr*)&addr,sizeof(addr));
      if(ret==-1)exit(1);
      
        addrlen=sizeof(addr);
        nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
        if(nread==-1)exit(1);
      
        pch = strtok (buffer,"\n");
          while (pch != NULL)
          {
        printf ("strtok = %s\n",pch);
        pch = strtok (NULL,"\n");
          }  
      
      return 0;
      while(1)
      {
        addrlen=sizeof(addr);
        nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
    
        if(nread==-1)exit(1);
        ret=sendto(fd,buffer,nread,0,(struct sockaddr*)&addr,addrlen);
        if(ret==-1)exit(1);
      }
    }
    hi,
    i have those pieces of code of server client code. they are not working for some reason , smb knows why?
    Last edited by tindala; 04-02-2014 at 06:01 PM.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    No idea why it does not work; but, why are you using a size of 128 for buffer in the recvfrom call?
    Doing this results in unpredictable results; this could include your compliant.

    Code:
    char buffer[50]="aa\nbb\ncc\ndd\nee\n";
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You said it was not working, but you didn't tell us how it is not working. Compiler errors? Crashes? Hangs? No output? Strange output? Details about your problem help us

    If you properly formatted and indented your code, it might be a little easier to follow the flow. Take your client code for example:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdlib.h> 
    #include <signal.h>
    #include <unistd.h>
    
    int main(void)
    {
        int fd,  ret, nread;
        struct sockaddr_in addr;
        char buffer[50]="aa\nbb\ncc\ndd\nee\n";
        socklen_t addrlen;
        char * pch;
    
        strcat(buffer,"LST\n");
        strcat(buffer,"555\n");
        if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1) exit(1);
    
        memset((void*)&addr,(int)'\0',sizeof(addr));
        addr.sin_family=AF_INET;
        inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr));
        /*addr.sin_addr.s_addr=htonl(INADDR_ANY); * ???*/
        addr.sin_port=htons(9000);
    
    
        printf("strlen(buffer) =%d",strlen(buffer));
        ret=sendto(fd,buffer, strlen(buffer),0,(struct sockaddr*)&addr,addrlen);
        if(ret==-1)exit(1);
    
        pch = strtok (buffer,"\n");
        while (pch != NULL)
        {
            printf ("strtok = %s\n",pch);
            pch = strtok (NULL,"\n");
        }
    
        return 0;
    
        while(1)
        {
            addrlen=sizeof(addr);
            nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
            if(nread==-1)exit(1);
    
            ret=sendto(fd,buffer,nread,0,(struct sockaddr*)&addr,addrlen);
            if(ret==-1)exit(1);
        }
    }
    Take a look at line 42. What do you think happens there? When do you think lines 44-52 execute?

    If you're competent enough in C to write socket code, you should be able to print useful error messages when something goes wrong (look into the perror function) instead of just exiting silently, and to print other debug messages to see the status of your program as it runs. You should also be able to use a debugger to trace your program's execution and examine the state of everything as it runs and when it fails. A debugger is an invaluable tool for a programmer.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    40
    first of all i want to be able to send the message from client to the server. i have more questions about my code, but 1st of all i want to solve that problem.
    my code is very similar to this one: Writing a simple UDP client/server in a Unix environment
    i get and error on the client side: sendto: Invalid argument

    client.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdlib.h>
    #include <signal.h>
    #include <unistd.h>
     
    int main(void)
    {
      int fd,  ret, nread;
      struct sockaddr_in addr;
      char buffer[128]="aa\nbb\ncc\ndd\nee\n";
      socklen_t addrlen;
        char * pch;
         
    strcat(buffer,"LST\n");
    strcat(buffer,"555\n");
      if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1) exit(1);
       
      memset((void*)&addr,(int)'\0',sizeof(addr));
      addr.sin_family=AF_INET;
      inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr));
      /*addr.sin_addr.s_addr=htonl(INADDR_ANY); * ???*/
      addr.sin_port=htons(9000);
     
       
      printf("strlen(buffer) =%d\n",strlen(buffer));
          ret=sendto(fd,buffer, 128,0,(struct sockaddr*)&addr,addrlen);
        if(ret==-1)
        {
          perror("sendto");
          exit(1);
          
        }
         
            pch = strtok (buffer,"\n");
          while (pch != NULL)
          {
        printf ("strtok = %s\n",pch);
        pch = strtok (NULL,"\n");
          } 
         
        return 0;
       
      while(1)
      {
        addrlen=sizeof(addr);
        nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
        if(nread==-1)exit(1);
         
        ret=sendto(fd,buffer,nread,0,(struct sockaddr*)&addr,addrlen);
        if(ret==-1)exit(1);
      }
    }
    server.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <stdlib.h> 
    #include <signal.h>
    #include <unistd.h>
    
    int main(void)
    {
      int fd,  ret, nread;
      struct sockaddr_in addr;
      char buffer[128];
      socklen_t addrlen;
      char * pch;
      
      if((fd=socket(AF_INET,SOCK_DGRAM,0))==-1) exit(1);
      memset((void*)&addr,(int)'\0',sizeof(addr));
      addr.sin_family=AF_INET;
      inet_pton(AF_INET, "127.0.0.1", &(addr.sin_addr));
      /*addr.sin_addr.s_addr=htonl(INADDR_ANY); * ???*/
      addr.sin_port=htons(9000);
      
      ret=bind(fd,(struct sockaddr*)&addr,sizeof(addr));
      if(ret==-1)exit(1);
      
        addrlen=sizeof(addr);
        nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
        if(nread==-1)
        {
          perror("recvfrom");
          exit(1);
        }
      
        pch = strtok (buffer,"\n");
          while (pch != NULL)
          {
        printf ("strtok = %s\n",pch);
        pch = strtok (NULL,"\n");
          }  
      
      return 0;
      while(1)
      {
        addrlen=sizeof(addr);
        nread=recvfrom(fd,buffer,128,0,(struct sockaddr*)&addr,&addrlen);
    
        if(nread==-1)exit(1);
        ret=sendto(fd,buffer,nread,0,(struct sockaddr*)&addr,addrlen);
        if(ret==-1)exit(1);
      }
    }

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Point to the line that set the value of addrlen; note, this needs to be done before you use the value in most programs.
    I have no resent experience with sockets; its been so long ago, I have forgotten all I know about them.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Tim has the right of it. A few printf statements printing important variables, or using a debugger, and you would have figured that out on your own much sooner. You would do well to implement those techniques when developing and debugging your programs.

    Also, you need to check all of your functions for error codes, and use perror for each one. That includes inet_pton, socket and bind, plus all instances of recvfrom and sendto.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C prog questions... help please
    By willgoodenough in forum C Programming
    Replies: 18
    Last Post: 04-23-2012, 08:24 AM
  2. Client/server problem; server either stops receiving data or client stops sending
    By robot-ic in forum Networking/Device Communication
    Replies: 10
    Last Post: 02-16-2009, 11:45 AM
  3. sockets: Why does server[buffer] > client[message]? + more questions
    By heras in forum Networking/Device Communication
    Replies: 4
    Last Post: 03-10-2008, 04:19 AM
  4. Client - Server
    By sCandal2 in forum Windows Programming
    Replies: 5
    Last Post: 11-16-2004, 06:24 AM
  5. both client & server?
    By pode in forum Networking/Device Communication
    Replies: 3
    Last Post: 09-16-2003, 02:13 PM

Tags for this Thread