yes, i do know how the fork() and exec() procedures work...
this is the piece of code:
Code:
while(1) { // main accept() loop
sin_size = sizeof(struct sockaddr_in) ;
if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr,&sin_size)) == -1) {
perror("accept");
continue;
}
printf("server: got connection from %s\n ",inet_ntoa(their_addr.sin_addr));
if (!fork()) { // this is the child process
close(sockfd); // child doesn’t need the listener
if (send(new_fd, "Hello, world!\n", 14, 0) == -1)
perror("send");
close(new_fd);
exit(0);
}
close(new_fd); // parent doesn’t need this
}
I think, the program works even without the fork call, as we two sockets, one for listening and on to tranfer data. So, we can use the send() call immediately after the accept() and there is no need to start another process.. Is,there any significant advantage of using the fork() call though??