Socket datagram

Oliver Laumann net at opal.cs.tu-berlin.de
Tue May 14 23:27:18 AEST 1991


In article <11225 at hub.ucsb.edu> hubert at spica.ucsb.edu writes:
> Hi! I am testing the socket's datagram facility and I can't figure
> out why the following program doesn't work. The a.out give me
> error message like "bad address". Could someone explain it ?
> 
> 	if (recvfrom(s1, mesg4, sizeof(mesg4),0,&sock2, sizeof(sock2))==-1)
> 		perror("receive error\n");

The last argument of recvfrom() is of type int*, not just int (it's an
input-output parameter).  You initialize it with the size of the buffer
that will be filled with the address; recvfrom() fills it with the
actual size of the address stored in the buffer on return.

To fix your program, declare an additional int variable "fromlen" and
invoke recvfrom() like this:

   fromlen = sizeof(sock2);
   if (recvfrom(...,&sock2,&fromlen) ...

--
Oliver Laumann    net at tub.cs.tu-berlin.de  net at tub.UUCP  ol at gnu.ai.mit.edu



More information about the Comp.unix.wizards mailing list