Solved

non-blocking connect() behaviour

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 96
has22 challenge 11 95
Change to event 1 112
Remove Password Protection from pdf (with password) using an command line executable 5 52
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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…
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.

778 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