Home > Archive > Unix Programming > March 2008 > Problem using sockets
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Problem using sockets
|
|
| sam.barker0@gmail.com 2008-03-29, 8:11 am |
| 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
| |
| Rainer Temme 2008-03-29, 7:24 pm |
| sam.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
| |
| Logan Shaw 2008-03-29, 10:34 pm |
| Rainer Temme wrote:
> sam.barker0@gmail.com wrote:
[color=darkred]
> 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
| |
| Rainer Weikusat 2008-03-30, 8:17 am |
| sam.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.
|
|
|
|
|