1

This question follows from Why is nonblocking socket writable before connect() or accept()? .

The following code spawns a thread that listens for a TCP connection. The main thread connects to the address that the server is listening at.

#include <iostream>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <pthread.h>
#include <semaphore.h>

class SafeSocket
{
public:

  /** Ctor.
   * Creates a nonblocking socket at the specified IP in the AF_INET family and
   * at a dynamic port.
   */
  SafeSocket( const std::string& ip )
  {
    in_addr_t host_ip = inet_network( ip.c_str() );
    if ( ( socket_ = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
    {
      std::cout << "socket() failed: " << errno << " " << strerror( errno )
                << std::endl;
      socket_ = -1;
    }
    sockaddr_in si;
    memset( &si, 0, sizeof( si ) );
    si.sin_family = AF_INET;
    si.sin_port = 0; // Dynamic port
    si.sin_addr.s_addr = htonl( host_ip );
    if ( bind( socket_, (sockaddr*)&si, sizeof si ) )
    {
      std::cout << "bind() failed: " << errno << " " << strerror( errno )
                << std::endl;
      close( socket_ );
      socket_ = -1;
    }
    // Make the socket do nonblocking connect().
    int flags = fcntl( socket_, F_GETFL, 0 );
    fcntl( socket_, F_SETFL, flags | O_NONBLOCK );
  }

  ~SafeSocket()
  {
    if ( socket_ >= 0 )
    {
      shutdown( socket_, SHUT_RDWR );
      close( socket_ );
    }
  }

  operator int() const
  {
    return socket_;
  }

private:

  int socket_;
};

int connectToClient( const SafeSocket& sock, const std::string& clientIp,
                     const int clientPort )
{
  struct sockaddr_in clientAddr;
  memset( &clientAddr, 0, sizeof clientAddr );
  inet_pton( AF_INET, clientIp.c_str(), &clientAddr.sin_addr );
  clientAddr.sin_family = AF_INET;
  clientAddr.sin_port = htons( clientPort );
  return connect( sock, (sockaddr*)&clientAddr, sizeof clientAddr );
}

std::string serverIp( "127.0.0.200" );
int serverPort = 9099; // Random, hopefully unused.
sem_t listenSem;

/** Entry point to pthread.
 */
void* acceptConnection( void* arg )
{
  int listenSock = socket( PF_INET, SOCK_STREAM, 0 );
  if ( listenSock < 0 )
  {
    std::cout << "socket() failed: " << errno << " " << strerror( errno )
              << std::endl;
    return NULL;
  }

  sockaddr_in si;
  si.sin_family = AF_INET;
  inet_aton( serverIp.c_str(), &si.sin_addr );
  si.sin_port = htons( serverPort );

  int optval = 1;
  setsockopt( listenSock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);

  int result = bind( listenSock, (sockaddr*)&si, sizeof si );
  if ( result )
  {
    std::cout << "bind() failed: " << errno << " " << strerror( errno )
              << std::endl;
    close( listenSock );
    return NULL;
  }

  std::cout << "listening on socket " << listenSock << std::endl;
  if ( listen( listenSock, 3 ) )
  {
    std::cout << "listen() failed: " << errno << " " << strerror( errno )
              << std::endl;
    close( listenSock );
    return NULL;
  }

  sem_post( &listenSem );

  fd_set readfds;
  FD_ZERO( &readfds );
  FD_SET( listenSock, &readfds );
  struct timeval ts = { 5, 0 };
  if ( -1 != select( listenSock + 1, &readfds, NULL, NULL, &ts ) )
  {
    if ( FD_ISSET( listenSock, &readfds ) )
    {
      sockaddr_in peerSi;
      socklen_t peerAddrLen = sizeof peerSi;
      memset( &peerSi, 0, peerAddrLen );
      sleep( 3 );
      int acceptSock = accept( listenSock, (sockaddr*)&peerSi, &peerAddrLen );
      if ( acceptSock > 0 )
      {
        std::cout << "accepted connection on socket " << acceptSock
                  << std::endl;
        close( acceptSock );
      }
    }
    else
    {
      std::cout << "did not receive a connection to accept." << std::endl;
    }
  }
  close( listenSock );
  return NULL;
}

int main( int argc, char* argv[] )
{
  sem_init( &listenSem, 0, 0 );

  SafeSocket s( "127.0.0.100" );
  std::cout << "Created socket " << s << std::endl;

  pthread_t tid;
  pthread_create( &tid, NULL, acceptConnection, NULL );

  timespec listenTimeout;
  clock_gettime( CLOCK_REALTIME, &listenTimeout );
  listenTimeout.tv_sec += 5;
  sem_timedwait( &listenSem, &listenTimeout );

  fd_set readFds;
  fd_set writeFds;

  FD_ZERO( &readFds );
  FD_ZERO( &writeFds );

  FD_SET( s, &writeFds );

  timeval timeout = { 5, 0 };
  int result = connectToClient( s, serverIp, serverPort );
  std::cout << "connectToClient() returned " << result << " "
            << errno << " " << strerror( errno ) << std::endl;

  if ( -1 == select( s+1, &readFds, &writeFds, NULL, &timeout ) )
  {
    std::cout << "select() failed: " << errno << " " << strerror( errno )
              << std::endl;
  }

  if ( FD_ISSET( s, &writeFds ) )
  {
    std::cout << s << " is writable!" << std::endl;
    int result = -1;
    socklen_t result_len = sizeof result;
    getsockopt( s, SOL_SOCKET, SO_ERROR, &result, &result_len );
    std::cout << "result: " << result << " " << strerror( result ) << std::endl;
  }

  pthread_join( tid, NULL );
  return 0;
}

Output:

>g++ --version
g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7)
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

>g++ -g main.cpp 
>
>./a.out 
Created socket 3                                             // Immediate
listening on socket 4                                        // Immediate
connectToClient() returned -1 115 Operation now in progress  // Immediate
3 is writable!                                               // Immediate
result: 0 Success                                            // Immediate
accepted connection on socket 5                              // Delayed

EJP, who commented at the noted question, noted that in this situation, the select() blocks for so long as the "target TCP is handshaking."

Is there, then, a way to lengthen this "TCP handshaking" process? I.e. I would like to lengthen the amount of time that the select() blocks waiting for socket s to become writable. Can this be done? You may observe I attempted to do this by adding a sleep() before the accept, but that does not work.

1 Answer 1

2

The handshake follows the following stages:

  • stage 1: connect is running on the client. This runs from when you call connect until at least the client gets the SYN+ACK packet from the server. I say at least because the client kernel may take a while to process that packet. My understanding is that the socket will not be writable until the end of stage 1.

  • Stage 2: the socket is writable on the client; connect is done. The server has not returned from accept. This includes the time when the server is waiting to receive the ACK from the client as well as any time between receiving that ACK and the server application calling accept (plus waiting for that call to accept to get processed)

  • Stage 3: both sides done.

I've made up these stages: the distinction I draw is designed to make it easy to discuss your question not to follow any aspect of the TCP state machine.

There's no useful way to lengthen stage 1, and certainly no way to make stage 1 encompass stage 2. You could add some delay in the client's kernel, but that would be arbitrary and unrelated to anything happening on the network.

The difference in time between stage 1 and stage 2 is all about network latency and processing within the server machine. There's no trigger that the client could act on.

For many applications it's desirable to start writing as soon as the socket is writable. When it isn't, the generally accepted solution is for your protocol to have the server send an initial greeting to the client. Then, wait for the socket to be readable and process that greeting before acting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.