public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* recv() timeout problem
@ 2013-07-04 18:34 David Stacey
  2013-07-05  9:00 ` Corinna Vinschen
  0 siblings, 1 reply; 4+ messages in thread
From: David Stacey @ 2013-07-04 18:34 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 415 bytes --]

Please find attached a short programme that demonstrates a problem I'm 
having with recv() timeouts. Under Fedora 19 x64, the test programme 
times out after three seconds (which is the desired behaviour). However, 
when run from Cygwin, the call to recv() never exits.

I am using the latest snapshot (2013-07-03) in 32-bit Cygwin. OS is 
Windows 7 Ultimate x64 SP1.

Many thanks in advance for your help,

Dave.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: recv_timeout.c --]
[-- Type: text/x-csrc; name="recv_timeout.c", Size: 1822 bytes --]

/* Compile: gcc -o recv_timeout recv_timeout.c
*/
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>

#define BUFFSIZE 1024

/* Simple error handler. */
void FatalError(const char* msg)
{
	fprintf(stderr, "Error: %s.\n", msg);
	exit(1);
}

int main()
{
	const in_port_t port = 1025;
	const char* hostname = "localhost";
	int sfd = -1;
	struct hostent* localhost;
	struct sockaddr_in ipa;
	char* addr = NULL;
	char buffer[BUFFSIZE];
	struct timeval timeout;

	/* Open a socket on 'localhost'. */
	sfd = socket(PF_INET, SOCK_DGRAM, 0);
	if (sfd == -1)
		FatalError("Could not create a socket");

	localhost = gethostbyname(hostname);
	if (!localhost)
		FatalError("Could not resolve 'localhost'.");

	ipa.sin_family = AF_INET;
	ipa.sin_port = htons(port);
	addr = localhost->h_addr_list[0];
	memcpy(&ipa.sin_addr.s_addr, addr, sizeof addr);

	/* Bind socket to IP address. */
	if (bind(sfd, (struct sockaddr*)&ipa, sizeof ipa) == -1)
		FatalError("Could not bind socket to IP address");

	/* Set the timeout to 3 seconds. */
	timeout.tv_sec = 3;
	timeout.tv_usec = 0;
	if (setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0)
		FatalError("Could not set recv timeout");

	/* Try to receive some data. As there's no data being sent, this should timeout. */
	printf("Calling recv() now - this should timeout...\n");
	if (recv(sfd, &buffer[0], BUFFSIZE * sizeof(char), 0) >= 0)
		FatalError("Received some data - choose a different port");

	if ((EAGAIN == errno) || (EWOULDBLOCK == errno) || (ETIMEDOUT == errno))
		printf("Hurrah - recv() timed out.\n");
	else
		fprintf(stderr, "After recv(), errno = %i.\n", errno);

	close(sfd);
	return 0;
}

[-- Attachment #3: Type: text/plain, Size: 218 bytes --]

--
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] 4+ messages in thread

* Re: recv() timeout problem
  2013-07-04 18:34 recv() timeout problem David Stacey
@ 2013-07-05  9:00 ` Corinna Vinschen
  2013-07-05 12:42   ` David Stacey
  0 siblings, 1 reply; 4+ messages in thread
From: Corinna Vinschen @ 2013-07-05  9:00 UTC (permalink / raw)
  To: cygwin

On Jul  4 19:34, David Stacey wrote:
> Please find attached a short programme that demonstrates a problem
> I'm having with recv() timeouts. Under Fedora 19 x64, the test
> programme times out after three seconds (which is the desired
> behaviour). However, when run from Cygwin, the call to recv() never
> exits.
> 
> I am using the latest snapshot (2013-07-03) in 32-bit Cygwin. OS is
> Windows 7 Ultimate x64 SP1.
> 
> Many thanks in advance for your help,

Unfortunately that won't work at the moment.

The underlying implementation of recv is nonblocking.  A blocking
Windows recv call is noninterruptible, unfortunately (at least up to
Windows 2003), so this was necessary to handle signals or
thread-cancellation.

Due to its nonblocking nature under the hood, this doesn't support
SO_RCVTIMEO and SO_SNDTIMEO and, surprisingly, we never had a complaint
about that, despite its age.

I can't promise a quick solution, but I put implementing handling of
SO_RCVTIMEO and SO_SNDTIMEO in recv/send on my TODO list.  Of course,
patches are welcome, too.

For the time being, I suggest to use select or poll with timeout
instead.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 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] 4+ messages in thread

* Re: recv() timeout problem
  2013-07-05  9:00 ` Corinna Vinschen
@ 2013-07-05 12:42   ` David Stacey
  2013-07-05 13:11     ` Corinna Vinschen
  0 siblings, 1 reply; 4+ messages in thread
From: David Stacey @ 2013-07-05 12:42 UTC (permalink / raw)
  To: cygwin

On 05/07/13 10:00, Corinna Vinschen wrote:
> On Jul  4 19:34, David Stacey wrote:
>> >Please find attached a short programme that demonstrates a problem
>> >I'm having with recv() timeouts. Under Fedora 19 x64, the test
>> >programme times out after three seconds (which is the desired
>> >behaviour). However, when run from Cygwin, the call to recv() never
>> >exits.
>> >
>> >I am using the latest snapshot (2013-07-03) in 32-bit Cygwin. OS is
>> >Windows 7 Ultimate x64 SP1.
>> >
>> >Many thanks in advance for your help,
> Unfortunately that won't work at the moment.
>
> The underlying implementation of recv is nonblocking.  A blocking
> Windows recv call is noninterruptible, unfortunately (at least up to
> Windows 2003), so this was necessary to handle signals or
> thread-cancellation.
>
> Due to its nonblocking nature under the hood, this doesn't support
> SO_RCVTIMEO and SO_SNDTIMEO and, surprisingly, we never had a complaint
> about that, despite its age.
>
> I can't promise a quick solution, but I put implementing handling of
> SO_RCVTIMEO and SO_SNDTIMEO in recv/send on my TODO list.  Of course,
> patches are welcome, too.
>
> For the time being, I suggest to use select or poll with timeout
> instead.

Thank you for your e-mail, and for replying so quickly. Regarding the 
recv() timeouts: no problem, it's easy enough to work around. On your 
TODO list, file this one under things to do when you've finally got that 
cat.

I think that's the last of my Poco problems out of the way. I'll give it 
a clean build and test, and send an ITP either this evening or tomorrow.

Thanks once again for your help,

Dave.


--
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] 4+ messages in thread

* Re: recv() timeout problem
  2013-07-05 12:42   ` David Stacey
@ 2013-07-05 13:11     ` Corinna Vinschen
  0 siblings, 0 replies; 4+ messages in thread
From: Corinna Vinschen @ 2013-07-05 13:11 UTC (permalink / raw)
  To: cygwin

On Jul  5 13:41, David Stacey wrote:
> On 05/07/13 10:00, Corinna Vinschen wrote:
> >On Jul  4 19:34, David Stacey wrote:
> >>>Please find attached a short programme that demonstrates a problem
> >>>I'm having with recv() timeouts. Under Fedora 19 x64, the test
> >>>programme times out after three seconds (which is the desired
> >>>behaviour). However, when run from Cygwin, the call to recv() never
> >>>exits.
> >>>
> >>>I am using the latest snapshot (2013-07-03) in 32-bit Cygwin. OS is
> >>>Windows 7 Ultimate x64 SP1.
> >>>
> >>>Many thanks in advance for your help,
> >Unfortunately that won't work at the moment.
> >
> >The underlying implementation of recv is nonblocking.  A blocking
> >Windows recv call is noninterruptible, unfortunately (at least up to
> >Windows 2003), so this was necessary to handle signals or
> >thread-cancellation.
> >
> >Due to its nonblocking nature under the hood, this doesn't support
> >SO_RCVTIMEO and SO_SNDTIMEO and, surprisingly, we never had a complaint
> >about that, despite its age.
> >
> >I can't promise a quick solution, but I put implementing handling of
> >SO_RCVTIMEO and SO_SNDTIMEO in recv/send on my TODO list.  Of course,
> >patches are welcome, too.
> >
> >For the time being, I suggest to use select or poll with timeout
> >instead.
> 
> Thank you for your e-mail, and for replying so quickly. Regarding
> the recv() timeouts: no problem, it's easy enough to work around. On
> your TODO list, file this one under things to do when you've finally
> got that cat.

LOL.  Nah, it's an interesting little problem, I'm pretty sure I'll
have a look later this month.

> I think that's the last of my Poco problems out of the way. I'll
> give it a clean build and test, and send an ITP either this evening
> or tomorrow.
> 
> Thanks once again for your help,

No worries.  And thanks for the testcase, I'm sure I'll use it to test
the SO_RCVTIMEO implementation when I get to it.


Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 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] 4+ messages in thread

end of thread, other threads:[~2013-07-05 13:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-04 18:34 recv() timeout problem David Stacey
2013-07-05  9:00 ` Corinna Vinschen
2013-07-05 12:42   ` David Stacey
2013-07-05 13:11     ` Corinna Vinschen

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).