Solved

non-blocking connect() behaviour

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now