public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Socket: non-blocking connect and getsockopt SO_ERROR
@ 2011-05-16 12:43 Jacob Eiler
  2011-05-16 13:29 ` Corinna Vinschen
  2011-05-17 14:20 ` Brian Ford
  0 siblings, 2 replies; 3+ messages in thread
From: Jacob Eiler @ 2011-05-16 12:43 UTC (permalink / raw)
  To: cygwin

Hi.

I am experiencing an issue with getsockopt when running an application
under CygWin. 

The application (Kannel sms gateway) creates a new socket, attempt to
connect non-blocking to the other host and later calls getsockopt to
check for errors:

s = socket(PF_INET, SOCK_STREAM, 0);
...
flags = fcntl(s, F_GETFL, 0);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
...
connect(s, &addr, sizeof(addr))

connect returns EINPROGRESS and the application has a polling thread
setup to handle timeout and check for changes. In both cases a callback
function is invoked.

The callback function checks the connection by calling 

getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len)

but it returns 0 and no error - even if the connection was never
established. Subsequently the application tries to write data to the
socket - which fails with a "transport endpoint is not connected" error.

The code works just fine on Linux with getsockopt returning an error.
Any pointer on how to get this working will be greatly appreciated.

BR
  Jacob

--  
Jacob Eiler
Apide ApS
e: jacob.eiler@apide.com
t: +45 2374 0486
w: apide.com



--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Socket: non-blocking connect and getsockopt SO_ERROR
  2011-05-16 12:43 Socket: non-blocking connect and getsockopt SO_ERROR Jacob Eiler
@ 2011-05-16 13:29 ` Corinna Vinschen
  2011-05-17 14:20 ` Brian Ford
  1 sibling, 0 replies; 3+ messages in thread
From: Corinna Vinschen @ 2011-05-16 13:29 UTC (permalink / raw)
  To: cygwin

On May 16 14:43, Jacob Eiler wrote:
> Hi.
> 
> I am experiencing an issue with getsockopt when running an application
> under CygWin. 
> 
> The application (Kannel sms gateway) creates a new socket, attempt to
> connect non-blocking to the other host and later calls getsockopt to
> check for errors:
> 
> s = socket(PF_INET, SOCK_STREAM, 0);
> ...
> flags = fcntl(s, F_GETFL, 0);
> fcntl(s, F_SETFL, flags | O_NONBLOCK);
> ...
> connect(s, &addr, sizeof(addr))
> 
> connect returns EINPROGRESS and the application has a polling thread
> setup to handle timeout and check for changes. In both cases a callback
> function is invoked.
> 
> The callback function checks the connection by calling 
> 
> getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len)
> 
> but it returns 0 and no error - even if the connection was never
> established. Subsequently the application tries to write data to the
> socket - which fails with a "transport endpoint is not connected" error.
> 
> The code works just fine on Linux with getsockopt returning an error.
> Any pointer on how to get this working will be greatly appreciated.

Use select.  I'm not sure the underlying Winsock implementation sets
the SO_ERROR value in the same way as on Linux.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Project Co-Leader          cygwin AT cygwin DOT com
Red Hat

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Socket: non-blocking connect and getsockopt SO_ERROR
  2011-05-16 12:43 Socket: non-blocking connect and getsockopt SO_ERROR Jacob Eiler
  2011-05-16 13:29 ` Corinna Vinschen
@ 2011-05-17 14:20 ` Brian Ford
  1 sibling, 0 replies; 3+ messages in thread
From: Brian Ford @ 2011-05-17 14:20 UTC (permalink / raw)
  To: cygwin

On Mon, 16 May 2011, Jacob Eiler wrote:

> I am experiencing an issue with getsockopt when running an application
> under CygWin.
>
> The application (Kannel sms gateway) creates a new socket, attempt to
> connect non-blocking to the other host and later calls getsockopt to
> check for errors:
>
> s = socket(PF_INET, SOCK_STREAM, 0);
> ...
> flags = fcntl(s, F_GETFL, 0);
> fcntl(s, F_SETFL, flags | O_NONBLOCK);
> ...
> connect(s, &addr, sizeof(addr))
>
> connect returns EINPROGRESS and the application has a polling thread
> setup to handle timeout and check for changes. In both cases a callback
> function is invoked.
>
> The callback function checks the connection by calling
>
> getsockopt(s, SOL_SOCKET, SO_ERROR, &err, &len)
>
> but it returns 0 and no error - even if the connection was never
> established. Subsequently the application tries to write data to the
> socket - which fails with a "transport endpoint is not connected" error.
>
> The code works just fine on Linux with getsockopt returning an error.
> Any pointer on how to get this working will be greatly appreciated.

The following pseudo code works portably for me on Solaris and Cygwin:

fds.fd     = s;
fds.events = POLL_WRITE;

if (Poll(&fds, 1, 0) > 0)
{
   len = sizeof(errno);

   getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno, &len)

   if (errno = 0)
	; //connected
}

I think the key is to make sure select/poll returns (as either writable or
with error) before calling getsockopt(SO_ERROR).  HTH.

-- 
Brian Ford
Staff Realtime Software Engineer
VITAL - Visual Simulation Systems
FlightSafety International
the best safety device in any aircraft is a well-trained crew...

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2011-05-17 14:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-16 12:43 Socket: non-blocking connect and getsockopt SO_ERROR Jacob Eiler
2011-05-16 13:29 ` Corinna Vinschen
2011-05-17 14:20 ` Brian Ford

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).