Advertisement

03.18.2008 at 09:40AM PDT, ID: 23250824
[x]
Attachment Details

[TCP server in C] How to avoid "Interrupted system call"

Asked by roccogalati in C Programming Language, CYGWIN, TCP/IP

Tags: C

Hi to all!

I'm testing my concurrent tcp server and i'm having some problems...

Sometimes, when i try to test it by using a client, it gives me this error:

< Accepting Connection: Interrupetd System Call >

and it crashes and i have to restart it...

I think it's because there is a problem with the child processes handling... and when i child dies my server receive an EINTR error and it close the connection...

What do you think about this?

I'm posting some code...

Can you help me?
Thanks a lot!

(I'm testing it on Cygwin)Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
/* my signal function */
 
void signal_handler(int sig)
{
	pid_t pid;
	int status;
	switch(sig)
        {
      
		  case SIGCHLD:	
	      	/* for solaris and unix system */
	         signal(SIGCHLD, signal_handler);
	       
	      while( (pid = waitpid(-1,&status,WNOHANG)) > 0)
	     {		    
		      count--; /* child processes counter */
		 }
		break;
	    
		case SIGTSTP:
                printf("\nI'm quitting...\nGood Bye!\n");
                        exit(0);
                break;           
        default:
                // uups -- what's this?
                puts("Unexpected signal received!\n");
                break;
        }
 
 
}
 
 
/* ..... */
 
sock = socket(AF_INET, SOCK_STREAM, 0);  /* SOCK_STREAM per TCP; */
					    
  if (sock < 0) {
    perror("Creating a stream socket");
    exit(1);
  }
  
  
  server.sin_family = AF_INET; 
  server.sin_addr.s_addr = htonl(INADDR_ANY); 
						
  server.sin_port = htons(port);
 
  /* call SO_REUSEADDR before bind() */
  value = 1;
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&value, sizeof(value)) < 0) {
    perror("during setsockopt");
    exit(5);
  }
  
  
  if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
    perror("binding socket");
      exit(2); 
  }
 
  listen(sock, 2); 
  
  signal(SIGCHLD,SIG_IGN);	/* to avoid zombies */
  signal(SIGTSTP, signal_handler); /* trap signal for child processes on unix and solaris */
  signal(SIGCHLD, signal_handler); /* trap CTRL-Z signal */
   
  pid_t pid;
  
   while (1) { 
    
    client_len = sizeof(client);
    printf("\nServer in attesa\n");
    
	if ((fd = accept(sock, (struct sockaddr *) &client, &client_len)) < 0) {
                
				perror("accepting connection");				
				exit(3);
    }
    	 
	  	 
	 if ( count < child_max) {
	 count ++;
	 msg[0] = 0;
	 Writeline(fd, msg, 1);
	 	 
	 if ( (pid = fork() == 0) ){		/* figlio */
      /* guardiamo i dati del client */
      
	  gethostname(host, 40);
	  printf("Accepted client: IP= %s, port=%d, hostname= %s\n",
	     inet_ntoa(client.sin_addr),
	     ntohs(client.sin_port), host);
		 listen_client_requests(fd); 
		 
     			
	 exit(0);			/* when a child ends */
    } 
	
	else
	      close(fd);
	}
	
	else if (count >= child_max){
	 msg[1] = 255;
	 Writeline(fd, msg, 1);
	 close(fd);
	 }
	
	else { 
	      close(fd);
		  		/* padre chiude sempre fd */
    }
	}
	
   return 0;
}
[+][-]03.18.2008 at 10:33AM PDT, ID: 21153601

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.18.2008 at 10:41AM PDT, ID: 21153709

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 10:45AM PDT, ID: 21153772

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 11:02AM PDT, ID: 21153984

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 01:02PM PDT, ID: 21155163

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 01:32PM PDT, ID: 21155471

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 03:18PM PDT, ID: 21156568

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 03:40PM PDT, ID: 21156818

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 03:44PM PDT, ID: 21156875

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 03:47PM PDT, ID: 21156907

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 04:17PM PDT, ID: 21157125

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 04:41PM PDT, ID: 21157247

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 04:51PM PDT, ID: 21157296

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 04:55PM PDT, ID: 21157322

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 05:00PM PDT, ID: 21157367

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.18.2008 at 05:35PM PDT, ID: 21157542

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 12:19AM PDT, ID: 21159107

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 02:28AM PDT, ID: 21159558

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 02:38AM PDT, ID: 21159617

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 03:12AM PDT, ID: 21159752

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 03:18AM PDT, ID: 21159778

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 03:31AM PDT, ID: 21159833

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 03:35AM PDT, ID: 21159849

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 03:47AM PDT, ID: 21159902

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.19.2008 at 03:52AM PDT, ID: 21159932

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, CYGWIN, TCP/IP
Tags: C
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 2
Solution Grade: A
 
 
[+][-]03.19.2008 at 03:58AM PDT, ID: 21159980

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628