interesting behavior.. this right ?

Ian Main (imain@vcc.bc.ca)
Sat, 12 Apr 1997 15:49:02 -0700 (PDT)


Hi, I'm no expert at this, and I don't know what the correct behavior is,
but this seems a little odd.

If you call a bind before connect(), and you bind to an address that has
no route to the host your trying to reach, the connect() will hang. I set
up an alarm to try to get around this, but it only seems to work once,
after that, the signal won't break it out of the connect call. (I even
had to kill -9 it once..)

Here's an example of what I'm talking about. It always hangs on the
connect call.. shouldn't it return an error ?

Like I say, I don't know the proper behavior but I just thought I'd check.
:) If anyone knows how to work around this, I'd be very greatful.

Ian

---------------------------------------------------------------

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(void)
{
struct sockaddr_in addr;
struct sockaddr_in local_addr;
int sock;

sock = socket(AF_INET, SOCK_STREAM, 0);

/* bind to localhost */
local_addr.sin_family = AF_INET;
local_addr.sin_port = htons(0);
inet_aton("127.0.0.1", &local_addr.sin_addr);

if (bind(sock, (struct sockaddr *) &local_addr, sizeof(struct sockaddr_in)) < 0)
perror("bind");

/* connect to remote host (sunsite's IP) */
addr.sin_family = AF_INET;
addr.sin_port = htons(23);
inet_aton("152.2.254.81", &addr.sin_addr);

printf("connecting...\n");

if (connect(sock, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)
perror("connect");

printf("done.");
return(0);
}