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