Code Comments
Programming Forum and web based access to our favorite programming groups.Hi,
I am trying to connect to a couple of serve[one after the other [in
case I cannot connect to one of them]
The code I am using is
rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
if(rc<0)
{
printf("cannot bind port %u\n",SERVER_PORT);
perror("error ");
exit(1);
}
if (connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))
< 0) {
{
printf("cannot connect\n");
//exit(1);
return;
}
In some cases I can bind the port ,but I cannot connect.And the
program hangs there till it times out.How can can I avoid this?
Cheers,
Sam
Post Follow-up to this messagesam.barker0@gmail.com wrote: > rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr)); ... > if (connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0) ... > In some cases I can bind the port ,but I cannot connect.And the > program hangs there till it times out.How can can I avoid this? Binding a port to a local address is usually unnecessary. Exceptions are those cases where the server program will only comunicate with the client if the client uses a certain local port number (or a certain range). The connect call blocks until the connection is extablished or until its clear that it has failed to connect. The only way around this behaviour is to use the socket in nonblocking mode. In this case connect() will return immediately with returncode -1 and errno set to EINPROGRESS. Hope this helps...Rainer
Post Follow-up to this messageRainer Temme wrote: > sam.barker0@gmail.com wrote: > The connect call blocks until the connection is extablished or until > its clear that it has failed to connect. The only way around this > behaviour is to use the socket in nonblocking mode. In this case > connect() will return immediately with returncode -1 and errno set to > EINPROGRESS. But you're still going to need a timeout. You send a packet to the remote side, and it will reply back with a response eventually. But how quickly? It depends on network latency, network congestion (and dropout rates), and the load on the remote host. The only thing a non-blocking connect() is going to get you is more control over the timeout. Well, I guess it also gives you the ability to try both connections simultaneously by doing two non-blocking connect()s and then sharing one timeout for both of them. - Logan
Post Follow-up to this messagesam.barker0@gmail.com writes:
> I am trying to connect to a couple of serve[one after the other [in
> case I cannot connect to one of them]
>
> The code I am using is
> rc = bind(sd, (struct sockaddr *) &localAddr, sizeof(localAddr));
> if(rc<0)
> {
> printf("cannot bind port %u\n",SERVER_PORT);
> perror("error ");
> exit(1);
> }
>
> if (connect(sd, (struct sockaddr *) &servAddr, sizeof(servAddr))
> < 0) {
> {
> printf("cannot connect\n");
> //exit(1);
> return;
> }
>
> In some cases I can bind the port ,but I cannot connect.And the
> program hangs there till it times out.How can can I avoid this?
Only by not trying to connect. What you can do (as was hinted at in
the other two postings) is create two sockets, enable non-blocking
mode for both (=> fnctl(3)), call connect for both and then use 'a
synchronous I/O multiplexing call' (poll or select) to wait for one of
the sockets to become writable. As soon as it does, the connection has
been established and you can then use the established connection and
abandon the other attempt by closing the corresponding socket
descriptor.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.