From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25407 invoked by alias); 17 May 2011 14:20:17 -0000 Received: (qmail 25390 invoked by uid 22791); 17 May 2011 14:20:15 -0000 X-SWARE-Spam-Status: No, hits=-0.8 required=5.0 tests=AWL,BAYES_40,T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from mailgw03.flightsafety.com (HELO mailgw03.flightsafety.com) (66.109.93.20) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Tue, 17 May 2011 14:20:01 +0000 Received: from mailgw03.flightsafety.com (localhost [127.0.0.1]) by localhost (Postfix) with SMTP id 30BEE6F61 for ; Tue, 17 May 2011 10:20:05 -0500 (CDT) Received: from xht-lga.flightsafety.com (unknown [192.168.31.182]) by mailgw03.flightsafety.com (Postfix) with ESMTP id EB6B46F69 for ; Tue, 17 May 2011 10:20:04 -0500 (CDT) Received: from xgate2k3.flightsafety.com (192.168.31.134) by xht-lga.flightsafety.com (192.168.31.182) with Microsoft SMTP Server id 8.2.176.0; Tue, 17 May 2011 10:19:54 -0400 Received: from srv1163ex1.flightsafety.com ([198.51.28.39]) by xgate2k3.flightsafety.com with Microsoft SMTPSVC(6.0.3790.3959); Tue, 17 May 2011 10:19:54 -0400 Received: from fordpc ([198.51.27.93]) by srv1163ex1.flightsafety.com with Microsoft SMTPSVC(6.0.3790.4675); Tue, 17 May 2011 09:19:53 -0500 Date: Tue, 17 May 2011 14:20:00 -0000 From: Brian Ford Reply-To: cygwin@cygwin.com To: cygwin@cygwin.com Subject: Re: Socket: non-blocking connect and getsockopt SO_ERROR In-Reply-To: <1305549799.2447.40.camel@flander> Message-ID: References: <1305549799.2447.40.camel@flander> MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" X-IsSubscribed: yes Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com X-SW-Source: 2011-05/txt/msg00242.txt.bz2 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