![]() |
| | #1 |
| Registered User Join Date: Apr 2007
Posts: 3
| problems with linux UDP 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 | |
| | #2 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| Quote:
| |
| brewbuck is offline | |
| | #3 |
| Registered User Join Date: Apr 2007
Posts: 3
| nevermind, solved it. thanks anyway |
| Lopizda is offline | |
| | #4 |
| and the hat of Jobseeking 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! |
| Salem is offline | |
| | #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 | |
| | #6 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| |
| brewbuck is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |