Question

when i recvfrom(), the received message is correct, but the source address is totally a mess, why is that happening?

char traid_messageR[MAXDATASIZE];
socklen_t addlen;
struct sockaddr_in source_addr;
if((numbytes=recvfrom(udp_sockfd, traid_messageR, 256, 0, (struct sockaddr*)&source_addr, &addlen)) == -1)
{
    perror("recvfrom");
    exit(1);
}

the result is like this:

(gdb) print source_addr
$1 = {sin_family = 61428, sin_port = 42, sin_addr = {s_addr = 49809}, 
  sin_zero = "\234\352\377\277\310\352\377\277"}

the 49809 looks like a port number, but it is the port number of this receiver...does any one have idea why is this?thanks a lot oh, another thing, i used this in a select() loop, IF_ISSET(und_socked,%fds),then exceute the above code, does this affect?

Was it helpful?

Solution

you didn't assign value to addlen

addlen = sizeof(source_addr)

UPDATE: refer to http://pubs.opengroup.org/onlinepubs/7908799/xns/recvfrom.html The manual says

address_len Specifies the length of the sockaddr structure pointed to by the address argument. ..... If the address argument is not a null pointer and the protocol provides the source address of messages, the source address of the received message is stored in the sockaddr structure pointed to by the address argument, and the length of this address is stored in the object pointed to by the address_len argument.

OTHER TIPS

I found it explained better here:

In this case, addrlen is a value- result argument. Before the call, it should be initialized to the size of the buffer associated with src_addr. Upon return, addrlen is updated to contain the actual size of the source address.

http://man7.org/linux/man-pages/man2/recv.2.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top