Thread: problems with linux UDP server

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    problems with linux UDP server

    Hello, this is my first attempt at creating an UDP client server application. The server (linux) receives a command from the client (windows) and returns the result of that command. The problem is that, for some reason, the server won't bind the socket(the debug printf messages don't show up on the screen), and it stops me from further debugging the program. Here is the source code for the server:


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
     
    #define MAXLEN 100
    #define SERVER_PORT 6069
    
    int main()
    {
         struct sockaddr_in serv,client; 
         int sd,sd2,len,len2;
         char command[MAXLEN];
         char line[100];
    
         sd=socket(AF_INET,SOCK_DGRAM,0);
         if(sd<0)
             {
              fprintf(stderr,"Can't create socket\n");
              return 1;
             }
         printf("socket created\n");
    
         memset((char*)&serv,0,sizeof(serv));
    
         serv.sin_family=AF_INET;
         serv.sin_addr.s_addr=INADDR_ANY; //i used also -- inet_addr("127.0.0.1");
         serv.sin_port=htons(SERVER_PORT);
    
         len2=sizeof(serv);
    
         printf("binding now");
    
         if (bind(sd,(struct sockaddr*)&serv,len2) < 0)
              {
              perror("error binding");
              }
         printf("bound and waiting");
    
         len=sizeof(client);
    
         for( ; ; )
              {
              recvfrom(sd,command,MAXLEN,0,(struct sockaddr*)&client,&len);
                                //receives the command 
              FILE* f;
    
              printf("\n Command '%s' executing now\n",command);
              f=popen(command,"r");
    
              if(f!=NULL)
                   {
                   while(fgets(line,MAXLEN,f)!=NULL)
                        {
                        sendto(sd,line,strlen(line),0,(struct sockaddr*) &client,len); 
                                  //sends result to client
                        }
                   sendto(sd,line,0,0,(struct sockaddr*) &client,len); 
                                //sending 0 to stop the client
                   }
                   else
                   {
                   strcpy(line,"error popening");
                   sendto(sd,line,strlen(line),0,(struct sockaddr*) &client,len);
                   }
              pclose(f);
              }
    close(sd);
    return 0;
    }

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Lopizda View Post
    Hello, this is my first attempt at creating an UDP client server application. The server (linux) receives a command from the client (windows) and returns the result of that command. The problem is that, for some reason, the server won't bind the socket(the debug printf messages don't show up on the screen), and it stops me from further debugging the program.
    So the bind() call fails and perror() prints an error message? What message does it print?

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Thumbs up

    nevermind, solved it. thanks anyway

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > nevermind, solved it. thanks anyway
    Well tell us then, so others can learn from it.

    It's just like reading a book only to find the last page missing!
    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.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    i will just post the code that's relevant. i still don't know what exactly went wrong, even though i did found one of that ellusive windows carriage return-line feed character hiding somewhere (i compiled the program under cygwin). i also moved the "len2=sizeof(serv);" function, but i doubt that one of these two things were the causes. cheers!

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    
    #define MAXLEN 100
    #define SERVER_PORT 6069
    
    int main()
    {
      struct sockaddr_in  serv,client; //addresses of the server and the client
      int sd,sd2,len,len2,n;
      char command[MAXLEN];
      char line[100];
    
      sd=socket(AF_INET,SOCK_DGRAM,0);
      
      if(sd<0)
    	{
    	 fprintf(stderr,"Can't create socket\n");
    	 return 1;
    	}
    
      printf("socket created\n");
    
      len2=sizeof(serv);
    
      memset((char*)&serv,0,sizeof(serv));
    
      serv.sin_family=AF_INET;
      serv.sin_addr.s_addr=inet_addr("127.0.0.1");
      serv.sin_port=htons(SERVER_PORT);
    
      printf("binding now\n");
      if (bind(sd,(struct sockaddr*)&serv,len2) < 0)
    	{
    	 perror("error binding");
    	}
      printf("bound and waiting\n");

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Lopizda View Post
    i still don't know what exactly went wrong
    Then you haven't solved anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-01-2008, 02:44 AM
  2. Multi-threaded UDP Server Problem
    By nkhambal in forum C Programming
    Replies: 0
    Last Post: 07-05-2005, 02:27 PM
  3. Problems with Mandrake Linux
    By frenchfry164 in forum Tech Board
    Replies: 9
    Last Post: 03-01-2003, 04:51 AM
  4. problems in with design of a server project.
    By codec in forum C++ Programming
    Replies: 4
    Last Post: 02-28-2003, 09:11 AM
  5. Simple Server problems...
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 08:51 PM