Link to home
Create AccountLog in
Avatar of Adyy
Adyy

asked on

EINPROGRESS errno in connect

I am writing an application (TCP server) that creates a nonblocking socket. When it executes connect function it returns the error

EINPROGRESS

the connect function describes what follow

The socket is nonblocking; the connection can't be completed immediately. It's possible to do a select() for completion by selecting the socket for writing.

What I really do not know is how to use select function when using sockets.

Thank you for your help
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi Adyy,

My sincere recommendation, read the *complete* guide at the following link ... It will form the basics of your network programming ... Highly recommended
http://beej.us/guide/bgnet/output/htmlsingle/bgnet.html#select

Another tutorial on select
http://www.lowtek.com/sockets/select.html

Feel free to ask for any clarifications that you might require.

Cheers!
sunnycoder
Avatar of Adyy
Adyy

ASKER

Hi Sunnycoder:

I have been working on my application.

I finally got an idea about using select. My problem now is what follows: My application is not a TCP server is a TCP client, it creates a non-blocking socket and I get a EINPROGRESS error after calling connect, I am now using select to wait for completition selecting the socket for writing.

But when I run the TCP Client(my application) and the server is not running select returns as if it was ready for writing (return value is 1)

If the PC running the server is not connected to the network the select function returns with timeout (as I thought)

Here are some functions

...
    if(ioctl(iSockFd, FIONBIO, &on) < 0)
   {
      perror("ioctl F SETFL, FNDELAY");
   }

   if (connect(iSockFd,(struct sockaddr *)&serv_addr[wEscl],sizeof(serv_addr[wEscl])) < 0)
   {
               perror("ERROR en Connect a write");
   }


   iCarEnv = writeTimeOut( iSockFd,buff_TX,iTotBytes, 10 ) ;

...


int writeTimeOut(int iSockW, BYTE*buffTo, INT iTotByt, int TimeOut )
{
   fd_set fds;
   int n;
   struct timeval tv;

   FD_ZERO(&fds);
   FD_SET(iSockFd, &fds);

   tv.tv_sec = TimeOut;
   tv.tv_usec = 0;

   n = select(iSockFd + 1, NULL, &fds, NULL, &tv);

    if (n == 0)
   {
       return -2;
   }

      
   if (n == -1)
  {
     return -1;
  }

  return write( iSockW,buffTo,iTotByt ) ;  // the program blocks here

}
>But when I run the TCP Client(my application) and the server is not running select returns as if it was ready for writing
>(return value is 1)

>If the PC running the server is not connected to the network the select function returns with timeout (as I thought)
Thats strange .. Do a netstat and see if the port is in use

Code looks Ok to me ... is it exact copy paste or a rewrite here .. often there are typos such as = instead of == that can wreak havoc.

I hope you determine the return value of select by printing ... ??

Lastly how did you determine where your code was blocking?
Avatar of Adyy

ASKER

The return value for select was n = 1.  I do a printf call after the select function.

In the code I include before I just delete the printf calls. I found that the app blocks in the write call because I include printf calls before an after each function.


When the problem arise I try to ping the server IP and I got the response for a packet and then the second no response and other no response. There was a problem on the network, but I just want to ensure that my application would not block if it works on a bad network.

I will appreciate any comment

Thank you

 
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer