Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Problem using sockets
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

Report this thread to moderator Post Follow-up to this message
Old Post
sam.barker0@gmail.com
03-29-08 01:11 PM


Re: Problem using sockets
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

Report this thread to moderator Post Follow-up to this message
Old Post
Rainer Temme
03-30-08 12:24 AM


Re: Problem using sockets
Rainer 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

Report this thread to moderator Post Follow-up to this message
Old Post
Logan Shaw
03-30-08 03:34 AM


Re: Problem using sockets
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Rainer Weikusat
03-30-08 01:17 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:11 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.