Programming/C Win32 MFC

while(-1) 이 될까?

구차니 2019. 5. 24. 12:18

코드 분석하다 보니 어떻게 작동할지 몰라서 해보니

헐.. while(0) 이냐 !0 이냐 정도로 밖에 작동하지 않네

예제 소스가 이상한 조건문이 되어버렸네...

 

client_sock에 -1이 들어와도 while은 중단되지 않고 0일때만 중단되는데

그러면 if(client_sock < 0)은 <=0이 아니라 들어갈 방법이 없네?

while( (client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c)) )
{
puts("Connection accepted");

pthread_t sniffer_thread;
new_sock = malloc(1);
*new_sock = client_sock;

if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
{
perror("could not create thread");
return 1;
}

//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
puts("Handler assigned");
}

if (client_sock < 0)
{
perror("accept failed");
return 1;
}

[링크 : https://www.binarytides.com/server-client-example-c-sockets-linux/]