Solved

non-blocking connect() behaviour

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 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

24 Experts available now in Live!

Get 1:1 Help Now