Problem mit TCP Server


#1
Hallo @ all,

ich hoffe ihr könnt mir einen Rat geben, ich bin mit meinem Latein am Ende.

Ich habe folgenden code für einen tcp server und in dostuff() funktion werden die
clients behandelt. Ab irgendwann habe ich mehrere gleiche
clients (zombies glaube ich), die in die Datenbank gleiche Sätze
schreiben. Hat jemand eine Erklärung? Was kann ich dagegen tun?



Code:
int main(int argc, char *argv[])
{
int srvrSock; /* Socket descriptor for server */
int clntSock; /* Socket descriptor for client */
struct sockaddr_in echoServAddr; /* Local address */
struct sockaddr_in echoClntAddr; /* Client address */
unsigned short echoServPort; /* Server port */
unsigned int cliAddrLen; /* Length of incoming message */
int pid;
struct sigaction sa;
int loop;

char ip[16];

/* Test for correct number of parameters */
if (argc < 2) {
fprintf(stderr,"Verwendung: %s <TCP SERVER PORT>\n", argv[0]);
exit(1);
}
//end if

/* First arg: local port */
echoServPort = atoi(argv[1]);

/* Create socket for sending/receiving */
if ((srvrSock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
DieWithError("socket() failed");
//end if

/* Construct local address structure */
memset(&echoServAddr, 0, sizeof(echoServAddr)); /* Zero out
structure */
echoServAddr.sin_family = AF_INET; /* Internet
address family */
echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming
interface */
echoServAddr.sin_port = htons(echoServPort); /* Local port */

/* Bind to the local address */
if (bind(srvrSock, (struct sockaddr *) &echoServAddr,
sizeof(echoServAddr)) < 0)
DieWithError("ERROR on binding");
//end if

/* Listen */
if (listen(srvrSock,5)<0)
DieWithError("listen() failed");
//end if

sa.sa_handler = sigchld_handler; // reap all dead processes
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGCHLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}


/* Set the size of the in-out parameter */
cliAddrLen = sizeof(echoClntAddr);

while (1) {
printf(" loop %d \n\n", loop);
loop++;

/* Wait for a client to connect */
if ((clntSock = accept(srvrSock, (struct sockaddr *)
&echoClntAddr, &cliAddrLen)) < 0)
DieWithError("accept() failed");
//end if

/* clntSock is connected to a client! */
strcpy(ip,inet_ntoa(echoClntAddr.sin_addr));
printf("Handling client %s\n", ip);

pid = fork();
if (pid < 0)
{
DieWithError("ERROR on fork");
}
else if (pid == 0)
{
close(srvrSock);
dostuff(clntSock,ip);
close(clntSock);
exit(0);
}
else close(clntSock);

} /* end of while */

return 0; /* we never get here */
}
--

Mit freundlichem Gruß
Burak Senel