This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

recv() timeout problem

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,


/* 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);

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");
		fprintf(stderr, "After recv(), errno = %i.\n", errno);

	return 0;
Problem reports:
Unsubscribe info:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]