Note that there are some explanatory texts on larger screens.

plurals
  1. POLinux TCP connect with Select() fails at testserver
    primarykey
    data
    text
    <p>My problem is the following: I'm programming an Interface in Linux to control a GPIB Controller via Ethernet. To do so I open a TCP socket and just send the commands to the Controller. This is working fine so far. The problem I have occured at writing some kind of unit test for my Interface: To check I am using a tcp acceptor from boost lib in a seperate thread and just connect to it instead of the actual controller. This is working too, but only as long as the connect() call from the interface is blocking. However since I need a specified timeout for the connect() call I had to connect with the select() function:</p> <pre><code> // Open TCP Socket m_Socket = socket(PF_INET,SOCK_STREAM,0); if( m_Socket &lt; 0 ) { m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; } struct sockaddr_in addr; inet_aton(m_Host.c_str(), &amp;addr.sin_addr); addr.sin_port = htons(m_Port); addr.sin_family = PF_INET; // Set timeout values for socket struct timeval timeouts; timeouts.tv_sec = SOCKET_TIMEOUT_SEC ; // const -&gt; 5 timeouts.tv_usec = SOCKET_TIMEOUT_USEC ; // const -&gt; 0 uint8_t optlen = sizeof(timeouts); if( setsockopt( m_Socket, SOL_SOCKET, SO_RCVTIMEO,&amp;timeouts,(socklen_t)optlen) &lt; 0 ) { m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; } // Set the Socket to TCP Nodelay ( Send immediatly after a send / write command ) int flag_TCP_nodelay = 1; if ( (setsockopt( m_Socket, IPPROTO_TCP, TCP_NODELAY, (char *)&amp;flag_TCP_nodelay, sizeof(flag_TCP_nodelay))) &lt; 0) { m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; } // Save Socket Flags int opts_blocking = fcntl(m_Socket, F_GETFL); if ( opts_blocking &lt; 0 ) { return ERR_NET_SOCKET; } int opts_noblocking = (opts_blocking | O_NONBLOCK); // Set Socket to Non-Blocking if (fcntl(m_Socket, F_SETFL, opts_noblocking)&lt;0) { return ERR_NET_SOCKET; } // Connect if ( connect(m_Socket, (struct sockaddr *)&amp;addr, sizeof(addr)) &lt; 0) { // EINPROGRESS always appears on Non Blocking connect if ( errno != EINPROGRESS ) { m_connectionStatus = STATUS_CLOSED; return ERR_NET_SOCKET; } // Create a set of sockets for select fd_set socks; FD_ZERO(&amp;socks); FD_SET(m_Socket,&amp;socks); // Wait for connection or timeout int fdcnt = select(m_Socket+1,NULL,&amp;socks,NULL,&amp;timeouts); if ( fdcnt &lt; 0 ) { return ERR_NET_SOCKET; } else if ( fdcnt == 0 ) { return ERR_TIMEOUT; } } //Set Socket to Blocking again if(fcntl(m_Socket,F_SETFL,opts_blocking)&lt;0) { return ERR_NET_SOCKET; } m_connectionStatus = STATUS_OPEN; return x2e::OK; </code></pre> <p>If I use this function I can still connect on the real controller and communicate with it. But if I use my testserver I just can't connect, select just leaves with a return value of 0. So now someone may say that my testserver just doesn't work....but If I use a blocking connect() call I can send to my testserver without any problems... Maybe someone has an idea what I could do...?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload