![]() |
| | #1 |
| Registered User Join Date: Jun 2007
Posts: 8
| Socket Select problem 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");
}
}
|
| saipkjai is offline | |
| | #2 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| select() destroys the fds bitset and you never reinitialize it. You have to do it every time through the outermost loop. There might be other problems -- I haven't looked carefully. |
| brewbuck is offline | |
| | #3 |
| Registered User Join Date: Jun 2007
Posts: 8
| thx for the fast reply. I think I got the thing working with one little problem. Right now the code looks like the following with your modification suggestion. Code:
while (1) {
result = select(sd+1, &fds, NULL, NULL, NULL);
FD_SET(sd, &fds);
if (result > 0)
{
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\n");
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid == 0) //child process
{
printf("this is the child\n");
/* The sdhas data available to be read */
close(sd);
n = recv(sd2, buf, sizeof(buf), 0);
printf("received size: %d\n", n);
printf("%s\n", buf);
close(sd2);
}
else //parent process
{
printf("This is the parents\n");
close(sd2);
}
}
else
{
printf("WTF");
}
}
else if (result < 0) {
/* An error ocurred, just print it to stdout */
perror("select");
}
else if (result == 0){
printf("select is resulting 0\n");
}
}
I got the server receive something from the client successfully, but I can open receive the msg one time and then it keep printing me bad file descriptor. I tried removed the while loop and it works perfectly fine( print one time). Is there something wrong I need to watch out for as well? |
| saipkjai is offline | |
| | #4 |
| Registered User Join Date: Jun 2007
Posts: 8
| problem solved. it was a silly mistake...forgot to check the EINTR. thank you for all the help |
| saipkjai is offline | |
| | #5 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| |
| brewbuck is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with simple socket client/server program | spencer88 | C Programming | 6 | 05-05-2009 11:05 PM |
| problem in select() in a Multiprotocol client | zealot | Networking/Device Communication | 2 | 04-09-2009 12:45 PM |
| A fifo file and select() problem | itsdafoetime | C Programming | 2 | 03-08-2009 02:23 AM |
| Problem with the socket | shiju | C Programming | 3 | 10-22-2004 04:29 AM |
| socket newbie, losing a few chars from server to client | registering | Linux Programming | 2 | 06-07-2003 11:48 AM |