UDP C Server not receiving packets
I know that there are related questions already answered, but I didn't manage to solve my problem.
I have a simple UDP client-server app. Both the client & server look ok, however the server doesn't receive packets from the client, it just infinitely receives the net id, 192 (or maybe the client doesn't send the packages correctly).
I can't seem to figure out the problem, the address & port are both ok, I don't have any firewall, and I even added an exception for the port, just to be sure. If I start the server, I can see that it's listening ok, on the right port (netstat -a -s -p udp). Can you please give me a hint on what's wrong?
Here is my server code:
/*.. includes */
#define PORT 8888
#define NPAC开发者_运维技巧K 10
#define MAXLEN 100
void signalError(char* s){
perror(s);
exit(1);
}
int main()
{
struct sockaddr_in struct_srv, struct_client;
int s,i,cod, numbytes;
size_t clientSize;
int32_t nr;
int32_t sir1[MAXLEN], sir2[MAXLEN], sirComune[MAXLEN], nrEl1, nrEl2, nrComune;
//Creating the socket:
s = socket(PF_INET, SOCK_DGRAM, 0);
if(s==-1) signalError("Error while creating socket!");
memset(&struct_srv, 0, sizeof(struct_srv));
struct_srv.sin_family = AF_INET;
struct_srv.sin_addr.s_addr = htonl(INADDR_ANY);
struct_srv.sin_port = htons(PORT);
s = bind(s, (struct sockaddr*) &struct_srv, sizeof(struct_srv));
if(s==-1) signalError("Bind error. Port is already in use!");
//receive packets:
nrEl1 = -1;
char buf[MAXLEN];
printf("Accepting packets:\n");
//for(i=0;i<NPACK;i++) {
for(;;) {
//Receive packets from client:
clientSize = sizeof(struct_client);
numbytes = recvfrom(s, buf, MAXLEN - 1, 0,
(struct sockaddr*) &struct_client, &clientSize);
buf[numbytes] = '\0';
printf("Packet is %d long.\n", numbytes);
printf("Packet contains %s:\n", buf);
sleep(3);
}
close(s);
return 0;
}
And my client code:
/* includes */
#define SRV_IP "127.0.0.1"
#define NPACK 100
#define MAXLEN 100
#define PORT 8888
void signalError(char* s){
perror(s);
exit(1);
}
int main(void)
{
struct sockaddr_in struct_client;
int s, i, result, size_client = sizeof(struct_client);
int32_t nr, sir1[MAXLEN], sir2[MAXLEN];
s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(s==-1) signalError("Erorr creating socket!");
memset((char*) &struct_client, 0, sizeof(struct_client));
struct_client.sin_family = AF_INET;
struct_client.sin_addr.s_addr = htonl(INADDR_ANY);
struct_client.sin_port = htons(PORT);
if(inet_aton(SRV_IP, &struct_client.sin_addr)==0) {
fprintf(stderr, "inet_aton() failed\n");
exit(1);
}
char buf[MAXLEN];
int len;
for(i=0;i<NPACK;i++) {
printf("Give packet %d:\n", i+1);
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf)-1] = '\0';
printf("I've read %s\n", buf);
printf("Sending packet %d\n", i+1);
result = sendto(s, buf, sizeof(buf)+1, 0,
(struct sockaddr*) &struct_client, size_client);
if(result==-1) signalError("Error sending packets!");
}
close(s);
return 0;
}
Here are a few minor points:
- In your server, set
size_client = sizeof(struct_client)
before every call torecvfrom
. It is both an input and output parameter, so you want to make sure the output from one call does not interfere with the input to the next. - Make sure to set the last byte of
buf
to'\0'
to prevent yourprintf
from reading memory beyond the boundary. - What is
cod
? Where did it come from? I think you meant to testnumbytes
. - Setting
buf[strlen(buf) - 1] = '\0'
chops off the last character of the buffer; is that what you were trying to do?
And the major reason it doesn't work:
You never initialized nr
, so there is no telling what sendto is sending. It certainly isn't sending buf
, which I think was what you wanted... You probably want this:
sendto(s, buf, sizeof(buf), ...)
Unless I'm missing something drastic, you're just sending garbage here.
nr
is never initialized on the sender, except in a comment.
buf
is being initialized, but not used anywhere.
Can you try changing the sendto
part to something like:
result = sendto(s, buf, strlen(buf)+1, 0,
(struct sockaddr*) &struct_client, size_client);
In your client code, the nr
variable that is sent over the net is not initialized and can point to whatever it wants. You probably want to send your buf
array instead.
精彩评论