Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

non-blocking connect() behaviour

Posted on 1997-12-11
1
Medium Priority
?
1,850 Views
Last Modified: 2013-12-26
I've got a quick question regarding the connect() behavior in the
following situation. In a client program I open a socket, set some
options on it (KEEPALIVE, LINGER, REUSEADDR), set it to the
non-blocking mode with fcntl() and then try to connect(). While
everything is fine if a server is running, the situation is not that
great if there is no server.

If there is not server listening on a specified port, then I get the
following results from connect():

1. DG/UX, Solaris:      ECONNREFUSED (Connection refused) - quite
obvious and expected.

2. Linux 2.0.29            EINPROGRESS (Operation in progress)

And the latter seems *really weird* to me. EINPROGRESS is not supposed
to be an error per se, so I don't terminate. Later, I am trying to
check whether the socket is ready to send (using select()) and on
Linux it says that it *is* ready (this is probably because of the
non-blocking mode. Am I right?). Due to some circumstances I am not
allowed to catch SIGPIPE. This basically means, that on Linux the
client program, instead of exiting nicely with a message: "server not
running" and doing some clean-up, it dies with a very "clear"
diagnostics "Broken pipe".

My question is: is there any "standard" regarding the connect()
behavior in such situation (non-blocking mode, no server listening)
and the ERROR to be returned: ECONNREFUSED or EINPROGRESS?

Thanks in advance,

PS. I'm attaching a tiny program, which is doing exactly what I
described. On Solaris and DG/UX it terminates with a 'Connection
refused' message, whereas on Linux, it procceeds further and dies of
SIGPIPE. The only requirement is that nobody is listening on the port
6779.

========================================================================

#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>


void
pexit(char* s)
{
    perror(s);
    exit(1);
}


int
main(void)
{
    int      sockfd, res;
    struct sockaddr_in      servaddr;
    struct linger Linger;
    fd_set fds;
   
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
      pexit("socket error");
   
    res = 0;
   
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &res, sizeof(int)) < 0)
      pexit("setsockopt REUSEADDR error");
      
    Linger.l_onoff    = 1;
    Linger.l_linger   = 0;

    if (setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &Linger, sizeof(Linger)) < 0)
      pexit("setsockopt LINGER error");
      
    res = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &res, sizeof(int)) < 0)
      pexit("setsockopt KEEPALIVE error");
      
    res = fcntl(sockfd, F_GETFL, 0);

    if (fcntl(sockfd, F_SETFL, O_NDELAY | res) < 0)
      pexit("fcntl error");
      
    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
    servaddr.sin_port = htons(6779);
      
    if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0 &&
      errno != EINPROGRESS)
      pexit("connect error");
      
    FD_ZERO(&fds);
    FD_SET(sockfd, &fds);
   
    if (select(sockfd + 1, NULL, &fds, NULL, NULL) < 0)
      pexit("select error");

    if (FD_ISSET(sockfd, &fds))
      printf("Socket %d is ready to send\n", sockfd);
   
    if (write(sockfd, &res, sizeof(res)) < 0)
      pexit("write error");
   
    exit(0);
}



========================================================================

--
Alexander L. Belikoff
Berger Financial Research Ltd.
abel@bfr.co.il
0
Comment
Question by:belikoff
1 Comment
 
LVL 1

Accepted Solution

by:
hno earned 520 total points
ID: 1295800
A connect() call on a non blocking socket should always be a non-blocking call, and as such it can't return a correct response other than "connection in progress" since it takes time to complete a connect... Don't ever count on it returning ECONNREFUSED. This is only possible in a VERY fast LAN environment where the network is faster than you own CPU, or on blocking connects.

In the programs I have been involved in we have always used write to detect failing connetcs after a select(), but it should be possible to use getsockopt(s,SOL_SOCKET,SO_ERROR,..) as well (and this does not generate SIGPIPE).

On Linux getsockopt is even documented on the connect() man page as the way to find the result of a non-blocking connect.

On some platforms (esp Solaris) you can use a second connect() to check the final status of the connect() call.

A note regarding SIGPIPE:
Unless the contacted server is extremly robust, or that it doesn't matter that the application is abruptly terminated when the server aborts the connection, you should take care of SIGPIPE.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question