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]

listen socket / poll block


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


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