C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 04-24-2007, 04:28 PM   #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;
}
Lopizda is offline   Reply With Quote
Old 04-24-2007, 05:59 PM   #2
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
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?
brewbuck is offline   Reply With Quote
Old 04-25-2007, 05:17 AM   #3
Registered User
 
Join Date: Apr 2007
Posts: 3
Thumbs up

nevermind, solved it. thanks anyway
Lopizda is offline   Reply With Quote
Old 04-25-2007, 05:35 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,695
> 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.

Salem is offline   Reply With Quote
Old 04-26-2007, 07:53 AM   #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");
Lopizda is offline   Reply With Quote
Old 04-26-2007, 08:42 AM   #6
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by Lopizda View Post
i still don't know what exactly went wrong
Then you haven't solved anything.
brewbuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic Winsock client and Linux server with C? Is it easy? joakim12 C Programming 4 03-01-2008 02:44 AM
Multi-threaded UDP Server Problem nkhambal C Programming 0 07-05-2005 02:27 PM
Problems with Mandrake Linux frenchfry164 Tech Board 9 03-01-2003 04:51 AM
problems in with design of a server project. codec C++ Programming 4 02-28-2003 09:11 AM
Simple Server problems... Unregistered C++ Programming 0 10-20-2001 08:51 PM


All times are GMT -6. The time now is 11:43 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22