Results 1 to 10 of 13
Hi there,
I have a problem with my connected UDP server, I want to to send info to the client using send(), but for some reason I always get an ...
- 11-23-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 35
UDP server not sending to client
Hi there,
I have a problem with my connected UDP server, I want to to send info to the client using send(), but for some reason I always get an error message such as: "bad file descriptor", any idea why?
here is a sample of my code
what I am trying to do, is that if for some reason I couldnt open the specified file, i should just send oxff to the client, but I am not able to do it, send() always returns -1 (error), any tips or idea why?Code:while (1) { //recv worked fine if ((bytes = recv(serverSock, buffer, BUFFER_SIZE-1, 0)) < 1 ) { fprintf(stderr,"Failed to receive bytes from server\n"); } file = fopen(buffer, "r"); if( file == NULL ) { perror("stat"); memset(message,0xff,1); messageLength = strlen(message); //PROBLEM if ( send(clientSock,message,mesageLength, 0); != messageLength) { perror("stat"); exit(1); } }
thank u in advance
- 11-23-2009 #2Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Please provide more code where you are creating and binding the sockets, as well as what the buffer size you are sending, etc.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-23-2009 #3Just Joined!
- Join Date
- May 2009
- Posts
- 35
alright...
here is more code, I think i did something wrong in connect() but i am not a %100 sure, thanks in advance
Code:int main(int argc, char *argv[]) { unsigned int localPort; int serverSock; int clientSock; int messageLength; unsigned int clientLen; unsigned char buffer[BUFFER_SIZE]; unsigned char message[MESSAGE_SIZE]; struct sockaddr_in serverAddr; struct sockaddr_in clientAddr; char* ptr; FILE* file; localPort = strtol(argv[1], &ptr, 10); if ((serverSock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) { printf("Failed to create socket\n"); exit(1); } memset(&serverAddr, 0, sizeof(serverAddr)); serverAddr.sin_family = AF_INET; serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); serverAddr.sin_port = htons(localPort); clientAddr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(serverSock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0) { printf("Failed to bind the server socket\n"); exit(1); } while (1) { //let the server connect to the client address so that only one client // maybe processed at a time clientLen = sizeof(clientAddr); if (connect(serverSock, (struct sockaddr *) &clientAddr, clientLen) < 0) { perror("Failed to connect with server"); exit(1); } //recv works gr8 if ((bytes = recv(serverSock, buffer, BUFFER_SIZE-1, 0)) < 1 ) { fprintf(stderr,"Failed to receive bytes from server\n"); } file = fopen(buffer, "r"); if( file == NULL ) { perror("stat"); memset(message,0xff,1); messageLength = strlen(message); //here is the problem I always get destination address required if ( send(serverSock,message,messageLength,0) != messageLength) { perror("stat"); exit(1); } } /* more code here */ clientAddr.sin_addr.s_addr = htonl(AF_UNSPEC); } exit(0); }
- 11-24-2009 #4Just Joined!
- Join Date
- May 2009
- Posts
- 35
anyone has any ideas, i am kinda stuck there for sometime now
- 11-24-2009 #5
I'm not sure if this is any help but I found some examples about this from somewhere. I don't remember where it was but I can paste the codes (Sligthly modified from the original example to reduce the ammount of codes). Maybe you can find some hints from the server part about what migth be the problem in your code.
This is 2 programs, one acts as a UDP server that will simply echo back all the data it receives from the client and another is client that allows you to sends a message to the server and waits for the servers response before closing. Both server and client will printf the message they receive.
First the server part:
UDPEchoServer.c
DieWithError.cCode:#include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket() and bind() */ #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ #include <stdlib.h> /* for atoi() and exit() */ #include <string.h> /* for memset() */ #include <unistd.h> /* for close() */ #define ECHOMAX 255 /* Longest string to echo */ void DieWithError(char *errorMessage); /* External error handling function */ int main(int argc, char *argv[]) { int sock; /* Socket */ struct sockaddr_in echoServAddr; /* Local address */ struct sockaddr_in echoClntAddr; /* Client address */ unsigned int cliAddrLen; /* Length of incoming message */ char echoBuffer[ECHOMAX]; /* Buffer for echo string */ unsigned short echoServPort; /* Server port */ int recvMsgSize; /* Size of received message */ if (argc != 2) /* Test for correct number of parameters */ { fprintf(stderr,"Usage: %s <UDP SERVER PORT>\n", argv[0]); exit(1); } echoServPort = atoi(argv[1]); /* First arg: local port */ /* Create socket for sending/receiving datagrams */ if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) DieWithError("socket() failed"); /* Construct local address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet address family */ echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */ echoServAddr.sin_port = htons(echoServPort); /* Local port */ /* Bind to the local address */ if (bind(sock, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) < 0) DieWithError("bind() failed"); for (;;) /* Run forever */ { /* Set the size of the in-out parameter */ cliAddrLen = sizeof(echoClntAddr); /* Block until receive message from a client */ if ((recvMsgSize = recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &echoClntAddr, &cliAddrLen)) < 0) DieWithError("recvfrom() failed"); printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr)); printf("Received: %s\n", echoBuffer); /* Send received datagram back to the client */ if (sendto(sock, echoBuffer, recvMsgSize, 0, (struct sockaddr *) &echoClntAddr, sizeof(echoClntAddr)) != recvMsgSize) DieWithError("sendto() sent a different number of bytes than expected"); } /* NOT REACHED */ }
And the client part:Code:#include <stdio.h> /* for perror() */ #include <stdlib.h> /* for exit() */ void DieWithError(char *errorMessage) { perror(errorMessage); exit(1); }
UDPEchoClient.c
DieWithError.cCode:#include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */ #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */ #include <stdlib.h> /* for atoi() and exit() */ #include <string.h> /* for memset() */ #include <unistd.h> /* for close() */ #define ECHOMAX 255 /* Longest string to echo */ void DieWithError(char *errorMessage); /* External error handling function */ int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoServAddr; /* Echo server address */ struct sockaddr_in fromAddr; /* Source address of echo */ unsigned short echoServPort; /* Echo server port */ unsigned int fromSize; /* In-out of address size for recvfrom() */ char *servIP; /* IP address of server */ char *echoString; /* String to send to echo server */ char echoBuffer[ECHOMAX+1]; /* Buffer for receiving echoed string */ int echoStringLen; /* Length of string to echo */ int respStringLen; /* Length of received response */ if ((argc < 3) || (argc > 4)) /* Test for correct number of arguments */ { fprintf(stderr,"Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]); exit(1); } servIP = argv[1]; /* First arg: server IP address (dotted quad) */ echoString = argv[2]; /* Second arg: string to echo */ if ((echoStringLen = strlen(echoString)) > ECHOMAX) /* Check input length */ DieWithError("Echo word too long"); if (argc == 4) echoServPort = atoi(argv[3]); /* Use given port, if any */ else echoServPort = 7; /* 7 is the well-known port for the echo service */ /* Create a datagram/UDP socket */ if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) DieWithError("socket() failed"); /* Construct the server address structure */ memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out structure */ echoServAddr.sin_family = AF_INET; /* Internet addr family */ echoServAddr.sin_addr.s_addr = inet_addr(servIP); /* Server IP address */ echoServAddr.sin_port = htons(echoServPort); /* Server port */ /* Send the string to the server */ if (sendto(sock, echoString, echoStringLen, 0, (struct sockaddr *) &echoServAddr, sizeof(echoServAddr)) != echoStringLen) DieWithError("sendto() sent a different number of bytes than expected"); /* Recv a response */ fromSize = sizeof(fromAddr); if ((respStringLen = recvfrom(sock, echoBuffer, ECHOMAX, 0, (struct sockaddr *) &fromAddr, &fromSize)) != echoStringLen) DieWithError("recvfrom() failed"); if (echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr) { fprintf(stderr,"Error: received a packet from unknown source.\n"); exit(1); } /* null-terminate the received data */ echoBuffer[respStringLen] = '\0'; printf("Received: %s\n", echoBuffer); /* Print the echoed arg */ close(sock); exit(0); }
Code:#include <stdio.h> /* for perror() */ #include <stdlib.h> /* for exit() */ void DieWithError(char *errorMessage) { perror(errorMessage); exit(1); }
- 11-24-2009 #6Just Joined!
- Join Date
- May 2009
- Posts
- 35
thanks, man, I looked at your examples they are helpful =)
I think if I used sendto() instead of send, it should work...but that's the problem, I don't want to use sendto(), send should have worked fine since I am connected to the client!!!
anyways anyone else has any ideas
I will try to use sendto() and see what happens, that moment I get home...
- 11-24-2009 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Part of the problem is that UDP is a connectionless protocol. You need to call connect() on each pass thru your loop. From the connect() man page:
Code:Generally, connection-based protocol sockets may successfully connect() only once; connectionless protocol sockets may use connect() multiple times to change their association.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-24-2009 #8Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
BTW, I would strongly suggest you get Comer and Stevens book Internetworking With TCP/IP - Vol. III if you are going to do much of this stuff.
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 11-24-2009 #9Just Joined!
- Join Date
- May 2009
- Posts
- 35
yea I did just that, look at my while loop, in each iteration it will connect() to a client then disconnect, also a weird thing, recv works fine, only send is messed up?
any ideas?
and about this book, I actually have it, and read some of it, but unfortunately, with a tight schedule I don't the luxury of reading alot of it these, but I am pretty sure I will =)
- 11-24-2009 #10Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Well, normally UDP servers use recvfrom() and sendto(). It is possible that there is a problem with your TCP/IP stack in using recv/send for UDP sockets. What distribution+version of Linux are you using? Also which compiler version?
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!


Reply With Quote
