public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* listen socket / poll block
@ 2011-04-06 18:32 Thomas Stalder
  2011-04-18 14:32 ` Corinna Vinschen
  0 siblings, 1 reply; 3+ messages in thread
From: Thomas Stalder @ 2011-04-06 18:32 UTC (permalink / raw)
  To: cygwin

Hello,

I made a small application that block poll function.

the result is :

before pthread_create
after pthread_create
before poll
before shutdown socket
after shutdown socket
before close socket
after close socket


under linux the result is:

before pthread_create
after pthread_create
before poll
before shutdown socket
after shutdown socket
after poll ret=1
error accept failed: Invalid argument


The code of my application are :

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/poll.h>

int SocketFD;

void * my_thread(void* arg)
{
   struct sockaddr_in stSockAddr;
   int ret;
   struct pollfd p;
   SocketFD = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

   if(-1 == SocketFD)
   {
       perror("can not create socket");
       exit(EXIT_FAILURE);
   }

   memset(&stSockAddr, 0, sizeof(stSockAddr));
   stSockAddr.sin_family = AF_INET;
   stSockAddr.sin_port = htons(1100);
   stSockAddr.sin_addr.s_addr = INADDR_ANY;

   if(-1 == bind(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(stSockAddr)))
   {
       perror("error bind failed");
       close(SocketFD);
       exit(EXIT_FAILURE);
   }

   if(-1 == listen(SocketFD, 10))
   {
       perror("error listen failed");
       close(SocketFD);
       exit(EXIT_FAILURE);
   }

   for(;;)
   {
       memset (&p, 0, sizeof (p));
       p.fd = SocketFD;
       p.events = POLLIN;
       p.revents = 0;
       printf("before poll\n");

       ret = poll (&p, 1, -1);
       printf("after poll ret=%d\n", ret);
       int ConnectFD = accept(SocketFD, NULL, NULL);

       if(0 > ConnectFD)
       {
           perror("error accept failed");
           close(SocketFD);
           exit(EXIT_FAILURE);
       }

       /* perform read write operations ...
       read(sockfd,buff,size)*/
       shutdown(ConnectFD, SHUT_RDWR);
       close(ConnectFD);
   }

   close(SocketFD);
   return 0;
}

int main(void)
{
   pthread_t id;
   printf("before pthread_create\n");
   pthread_create(&id, NULL, my_thread, NULL);
   printf("after pthread_create\n");
   sleep(3);
   printf("before shutdown socket\n");
   shutdown(SocketFD,2);
   printf("after shutdown socket\n");
   sleep(3);
   printf("before close socket\n");
   close(SocketFD);
   printf("after close socket\n");
   sleep(30);
}

--
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: listen socket / poll block
  2011-04-06 18:32 listen socket / poll block Thomas Stalder
@ 2011-04-18 14:32 ` Corinna Vinschen
  2011-04-20 13:47   ` Thomas Stalder
  0 siblings, 1 reply; 3+ messages in thread
From: Corinna Vinschen @ 2011-04-18 14:32 UTC (permalink / raw)
  To: cygwin

On Apr  6 18:19, Thomas Stalder wrote:
> Hello,
> 
> I made a small application that block poll function.
> 
> the result is :
> 
> before pthread_create
> after pthread_create
> before poll
> before shutdown socket
> after shutdown socket
> before close socket
> after close socket
> 
> 
> under linux the result is:
> 
> before pthread_create
> after pthread_create
> before poll
> before shutdown socket
> after shutdown socket
> after poll ret=1
> error accept failed: Invalid argument

First of all, thanks for the testcase.

It turns out that the semantics of Winsock's shutdown function are
different from the Linux implementation.  Not overly surprising, but it
makes trying to get the behaviour working the same way as on Linux
a bit awkward.

The problem is that in your scenario the shutdown function works fine on
Linux, and the subsequent call to accept fails because the socket has
been shutdown for reading.  On Winsock, the shutdown function *fails*,
the error code is "socket not connected".  Thus, even if Cygwin fakes
the results for shutdown to trigger the poll call, the accept call does
*not* fail.

I applied a fix which enforces the Linux behaviour.  Please give the
next developer snapshot from http://cygwin.com/snapshots/ a try.


Thanks,
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: listen socket / poll block
  2011-04-18 14:32 ` Corinna Vinschen
@ 2011-04-20 13:47   ` Thomas Stalder
  0 siblings, 0 replies; 3+ messages in thread
From: Thomas Stalder @ 2011-04-20 13:47 UTC (permalink / raw)
  To: cygwin

Works well!

Many thanks,

Thomas

2011/4/18 Corinna Vinschen
> I applied a fix which enforces the Linux behaviour.  Please give the
> next developer snapshot from http://cygwin.com/snapshots/ a try.

--
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-04-20 12:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-06 18:32 listen socket / poll block Thomas Stalder
2011-04-18 14:32 ` Corinna Vinschen
2011-04-20 13:47   ` Thomas Stalder

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