Hi, I'm doing a client/server with threads instead of forks. The goal is to have multiple clients connect to the server and for each client the server makes a new thread. Although it's working atm, there are things that bother me, especially the sockets. The first thing I want to know is (when using the standard fork model) why the listening socket is closed immediately after the fork?
while(1){
new_fd = accept(sockfd,
(struct sockaddr *)&their_addr,
&sin_size);
if (new_fd == -1) {
perror("Serveur: accept");
}
printf("Serveur: connection recue du client %s\n",
inet_ntoa(their_addr.sin_addr));
if (fork()==0) {
/* this is the child process */
close(sockfd);
if (send(new_fd, "Hi client!\n",11,0)==-1) {
perror("Serveur: send");
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
close(new_fd);
}
The second thing I want to know is since I'm using threads I don't copy anything, so if I close the listening socket it's closed for the entire program. My question: is letting the socket open OK or not/ do I need multiple sockets?