Solved

non-blocking connect() behaviour

Posted on 1997-12-11
1
1,789 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 130 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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