Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!mintaka!ai-lab!opal!net From: net@opal.cs.tu-berlin.de (Oliver Laumann) Newsgroups: comp.unix.wizards Subject: Re: Socket datagram Message-ID: <3333@kraftbus.cs.tu-berlin.de> Date: 14 May 91 13:27:18 GMT References: <11225@hub.ucsb.edu> Organization: Technical University of Berlin, Germany Lines: 21 In article <11225@hub.ucsb.edu> hubert@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@tub.cs.tu-berlin.de net@tub.UUCP ol@gnu.ai.mit.edu