Jump to content

UDP client-server problem

- - - - -

  • Please log in to reply
3 replies to this topic

#1
calin.burloiu

calin.burloiu

    Newbie

  • Members
  • Pip
  • 1 posts
Hello,

I am trying to create two C++ applications that communicate with the UDP transport protocol in the Linux operating system. I am using Berkeley sockets.
The client sends a datagram using the sendto function and the server receives the packet with recvfrom like this:
recsize = recvfrom(sock, (void *) buffer, 1024, 0,

				(struct sockaddr *) &sa, &fromlen);
After receiving the datagram, the server prints the sender's IP address and port using the structure sockaddr_in sa filled by the recvfrom function. If I test both the client and the server on my localhost (127.0.0.1), the server successfully prints the correct sender's IP and port. But when I put the server on a different machine from the Internet, the server's receivefrom function doesn't fill the sa structure with the source address and port and it prints the IP 0.0.0.0 and the port 0. My local machine is behind a NAT. So the IP printed by the server would normally be my external IP address and a random port chosen by the NAT protocol which maps to my internal IP and port.

What is the problem?

#2
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
where exactlly did you put your server program, is it a virtual server?

it could be that recvfrom returned a error
i would go with :

if((recsize = recvfrom(sock, (void *) buffer, 1024, 0,(struct sockaddr *) &sa, &fromlen))<0){
         errx(1,"%s",gai_error(recsize));}

this way you would get some info on the error and the program will quit with the exit_status=1.

it is also useful to use wireshark with network programming

#3
manux

manux

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 234 posts
Yes, if there's an error the sockaddr struct probably won't be changed, or it might be filled with zeros, although the man pages do not suggest so. Assuming you/something zero'ed the struct before using it or during the error, A might explain B.

#4
ferovac

ferovac

    Learning Programmer

  • Members
  • PipPipPip
  • 84 posts
if a error accures recvfrom zeros everything




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users