Re: Recvfrom for UDP sockets

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



S P wrote:

> I am writing a simple program in which a machine A
> broadcasts UDP messages on a subnet. Another machine B
> on the same subnet receives this message using
> "recvfrom". 
> 
> int recvfrom(int s, void *buf, size_t len, int flags,
> struct sockaddr *from, socklen_t *fromlen);
> 
> The last 2 parameters of recvfrom give information
> about the sender of the message - machine A. The
> information is IP address, port number, and family
> (AF_INET etc.).
> If I compile the program using gcc option -O2,
> recvfrom works & receives the message correctly, but
> no information about the sender. If option -O2 is not
> used, this works fine.

Did you initialise the fromlen argument? From the recvfrom(2) manpage:

	The argument fromlen is a value-result parameter, initial­
	ized  to  the size of the buffer associated with from, and
	modified on return to indicate  the  actual  size  of  the
	address stored there.

I.e.:

	struct sockaddr_in from;
	socklen_t fromlen = sizeof(from);

	recvfrom(skt, buf, len, flags, (struct sockaddr *) &from, &fromlen);

-- 
Glynn Clements <glynn.clements@virgin.net>
-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux