C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2008, 12:29 AM   #1
Registered User
 
Join Date: Jun 2007
Posts: 8
Socket Select problem

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
saipkjai is offline   Reply With Quote
Old 02-07-2008, 12:50 AM   #2
Senior software engineer
 
brewbuck's Avatar
 
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   Reply With Quote
Old 02-07-2008, 01:31 AM   #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   Reply With Quote
Old 02-08-2008, 03:05 AM   #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   Reply With Quote
Old 02-08-2008, 10:57 AM   #5
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by saipkjai View Post
problem solved.

it was a silly mistake...forgot to check the EINTR.

thank you for all the help
Eh? Why were you receiving a signal?
brewbuck is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:30 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22