How can the following code
while(1) {
client_name_len = sizeof(struct sockaddr_in);
newsockfd = accept(sockfd, (struct sockaddr *)&client_name, &client_name_len);
if (newsockfd < 0) {
perror("ERROR on accept");
printf("Finished\n");
close(sockfd);
exit (EXIT_FAILURE);
}
printf("a:");
pthread_t thread1;
int *addr = (int*)malloc(sizeof(int));
*addr = newsockfd;
pthread_create( &thread1, NULL, &ProcessClient, (void*)addr);
}
produce the following output:
a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:ERROR on accept: Too many open files
a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:a:Finished
The problem is that perror("ERROR on accept")
, printf("Finished\n")
and printf("a:")
are executed in the same thread, but the output is mixed.
ProcessClient does not output anything and does not create any threads.
sockfd
is a standard listening tcp socket.