Does someone know how much pointers and such stuff a child and a parent shares? The program should fork, and the child handle the file (sending it), and the parent all the connections.
The problem is that the child don't know which connections that are active =/ Can someone see what's wrong? (The code around "printf(" >> %d", i);" in the child outputs only the process id (3), and the parent outputs all connections (as the child should do also)).
...includes, process() and checkpasswd()...
int main (int argc, char **argv){
...alot of init code...
// Initialize the active sockets
FD_ZERO (&master);
FD_ZERO (&read_fds);
FD_SET (sock, &master);
if( fork() == 0 ){
// Child process
...
printf("Child!\n");
// Open the file
pos = 0;
while(true){
read_fds = master;
printf("Child sockets:");
for( i = 0; i < FD_SETSIZE; i++){
if (FD_ISSET (i, &read_fds) > 0)
printf(" >> %d", i); <----------------------
}
printf("\n");
// Sleep for a while
sleep(2);
}
printf("No more child =(\n");
fclose(fp);
exit(0);
}
else{
// Parent process
while (true){
read_fds = master;
printf("Active sockets:");
for( i = 0; i < FD_SETSIZE; i++){
if (FD_ISSET (i, &read_fds) && i != sock)
printf(" >> %d", i); <----------------------
}
printf("\n");
// Wait for an active socket
printf("\nWaiting...\n");
if (select (FD_SETSIZE, &read_fds, NULL, NULL, NULL) < 0){
perror ("Select error");
exit(1);
}
// Loop through all sockets
for (i = 0; i < FD_SETSIZE; i++){
if (FD_ISSET (i, &read_fds)){
// Got a new connection on the original socket
if (i == sock){
...FD_SET...
}
// Data arriving on an "old" socket -> process the data
else{
...
} // end if (i==sock)
} // end if (FD_ISSET)
} // end for (FD_SETSIZE)
} // inf loop
} // end (fork)
close(sock);
return(0);
}
... I doubt anyone understand what I want though... should sleep instead.