Re: connect() returns 22

From: Andrei Voropaev (avorop_at_mail.ru)
Date: 10/30/03


Date: 30 Oct 2003 13:59:58 GMT

On 2003-10-30, Casper H.S *** <Casper.***@Sun.COM> wrote:
> Senthilraja <senthil.raja@adcc.alcatel.be> writes:
>
>> bzero(server_addr, sizeof(struct sockaddr_in));
>> server_addr->sin_family = AF_INET;
>> server_addr->sin_port = htons(80);
>> server_addr->sin_addr.s_addr = 1117850705; //the IP address of
>>dictionary.reference.com
>
> You would need to use "htonl(1117850705)".
>
>> // Connect to the server
>
>> addrlen = sizeof (server_addr);
>
> This is wrong; should be "sizeof *server_addr".
>
>
>> returncode = connect(server_socket,
>> (struct sockaddr *) &server_addr,
>
> This is incorrect; server_addr is a struct sockaddr_in *.

Please do not confuse people. 'connect' expects pointer to struct
sockaddr* so casting is appropriate here to shut up warnings from
compiler. :) The real problem here is that '&server_addr'. server_addr
is already pointer to structure, so he passes pointer to pointer.
>
> Seems like you're mixing example; what people usually do is:
>
> struct sockaddr_in server_addr;
>
> (no malloc) and then that code is OK.

Yes, malloc is not needed. But is perfectly legal.

Andrei