delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2011/04/06/12:19:49

X-Recipient: archive-cygwin AT delorie DOT com
X-SWARE-Spam-Status: No, hits=-1.9 required=5.0 tests=AWL,BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW,T_FRT_STOCK2,T_TO_NO_BRKTS_FREEMAIL
X-Spam-Check-By: sourceware.org
MIME-Version: 1.0
Date: Wed, 6 Apr 2011 18:19:35 +0200
Message-ID: <BANLkTim4WfCJ3w5VuDY4-Ppax5iAZZ3uCQ@mail.gmail.com>
Subject: listen socket / poll block
From: Thomas Stalder <cygwinml AT gmail DOT com>
To: cygwin AT cygwin DOT com
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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=3D1
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)
{
=A0 =A0struct sockaddr_in stSockAddr;
=A0 =A0int ret;
=A0 =A0struct pollfd p;
=A0 =A0SocketFD =3D socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

=A0 =A0if(-1 =3D=3D SocketFD)
=A0 =A0{
=A0 =A0 =A0 =A0perror("can not create socket");
=A0 =A0 =A0 =A0exit(EXIT_FAILURE);
=A0 =A0}

=A0 =A0memset(&stSockAddr, 0, sizeof(stSockAddr));
=A0 =A0stSockAddr.sin_family =3D AF_INET;
=A0 =A0stSockAddr.sin_port =3D htons(1100);
=A0 =A0stSockAddr.sin_addr.s_addr =3D INADDR_ANY;

=A0 =A0if(-1 =3D=3D bind(SocketFD, (struct sockaddr *)&stSockAddr, sizeof(s=
tSockAddr)))
=A0 =A0{
=A0 =A0 =A0 =A0perror("error bind failed");
=A0 =A0 =A0 =A0close(SocketFD);
=A0 =A0 =A0 =A0exit(EXIT_FAILURE);
=A0 =A0}

=A0 =A0if(-1 =3D=3D listen(SocketFD, 10))
=A0 =A0{
=A0 =A0 =A0 =A0perror("error listen failed");
=A0 =A0 =A0 =A0close(SocketFD);
=A0 =A0 =A0 =A0exit(EXIT_FAILURE);
=A0 =A0}

=A0 =A0for(;;)
=A0 =A0{
=A0 =A0 =A0 =A0memset (&p, 0, sizeof (p));
=A0 =A0 =A0 =A0p.fd =3D SocketFD;
=A0 =A0 =A0 =A0p.events =3D POLLIN;
=A0 =A0 =A0 =A0p.revents =3D 0;
=A0 =A0 =A0 =A0printf("before poll\n");

=A0 =A0 =A0 =A0ret =3D poll (&p, 1, -1);
=A0 =A0 =A0 =A0printf("after poll ret=3D%d\n", ret);
=A0 =A0 =A0 =A0int ConnectFD =3D accept(SocketFD, NULL, NULL);

=A0 =A0 =A0 =A0if(0 > ConnectFD)
=A0 =A0 =A0 =A0{
=A0 =A0 =A0 =A0 =A0 =A0perror("error accept failed");
=A0 =A0 =A0 =A0 =A0 =A0close(SocketFD);
=A0 =A0 =A0 =A0 =A0 =A0exit(EXIT_FAILURE);
=A0 =A0 =A0 =A0}

=A0 =A0 =A0 =A0/* perform read write operations ...
=A0 =A0 =A0 =A0read(sockfd,buff,size)*/
=A0 =A0 =A0 =A0shutdown(ConnectFD, SHUT_RDWR);
=A0 =A0 =A0 =A0close(ConnectFD);
=A0 =A0}

=A0 =A0close(SocketFD);
=A0 =A0return 0;
}

int main(void)
{
=A0 =A0pthread_t id;
=A0 =A0printf("before pthread_create\n");
=A0 =A0pthread_create(&id, NULL, my_thread, NULL);
=A0 =A0printf("after pthread_create\n");
=A0 =A0sleep(3);
=A0 =A0printf("before shutdown socket\n");
=A0 =A0shutdown(SocketFD,2);
=A0 =A0printf("after shutdown socket\n");
=A0 =A0sleep(3);
=A0 =A0printf("before close socket\n");
=A0 =A0close(SocketFD);
=A0 =A0printf("after close socket\n");
=A0 =A0sleep(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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019