Hi
I'm having a little trouble with the select and fork for the server side program that I'm building. The problem is I got the socket and everything working and the basic problem can receive and display the data out. But whenever I put in the select with the fork, the program does not receive and display the data anymore. Can someone point me into the right direction of the problem that I have with the code?
sd is the listening socket descriptor and sd2 is the accepted socket descriptor.
Code:
while (1) {
result = select(sd, &fds, NULL, NULL, NULL);
printf("%d", result);
if (result > 0)
{
printf("entered");
if (FD_ISSET(sd, &fds))
{
alen = sizeof(cad);
if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) {
fprintf(stderr, "accept failed\n");
exit(1);
}
//make child process
printf("forking now");
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid == 0) //child process
{
/* The sd2 has data available to be read */
close(sd);
n = recv(sd2, buf, sizeof(buf), 0);
printf("received size: %d", n);
printf("%d", buf);
close(sd2);
}
else //parent process
{
printf("This is the parents");
close(sd2);
}
}
}
else if (result < 0) {
/* An error ocurred, just print it to stdout */
perror("select");
}
else if (result == 0){
printf("select is resulting 0");
}
}
thank you for all the help