Problem with tcp server


 
Thread Tools Search this Thread
Top Forums Programming Problem with tcp server
# 1  
Old 01-29-2010
Problem with tcp server

Hello @ all,

I hope you can give me some advice Smilie

I will be following code for a tcp server and doStuff () function, the
clients treated. From some point, I have several identical
clients (zombies, I think), the same records in the database
write. Has anyone an explanation? What can I do?

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 */
}

--

Best regards
Burak Senel

not: sorry, for my bad english... and thank you PLUDI

Last edited by yumos; 01-29-2010 at 11:53 AM.. Reason: code tags, please...
# 2  
Old 01-29-2010
Do you know if sigchld_handler is actually being called?

Furthermore, whenever sigchld_handler is called, be sure to check for more than one child. SIGCHLD signals won't queue up if there's more than one, it'll just be missed. Which is annoying since you can't guarantee there'll never be a zombie with a callback but at least you can stop them from accumulating...

I don't see anything to do with a database here so it's impossible to be sure, but is it possible you're storing a pointer to the ip char array instead of a copy of its contents? That would mean every entry in whatever your database is would show the most recent IP address.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Too much TCP retransmitted and TCP duplicate on server Oracle Solaris 10

I have problem with oracle solaris 10 running on oracle sparc T4-2 server. Os information: 5.10 Generic_150400-03 sun4v sparc sun4v Output from tcpstat.d script TCP bytes: out outRetrans in inDup inUnorder 6833763 7300 98884 0... (2 Replies)
Discussion started by: insatiable1610
2 Replies

2. UNIX for Dummies Questions & Answers

Tcp connection to Linux server fails

I am trying to send json messages to a port on a linux server from a remote server running a .net program. I have one implementation running with successful incoming messages to port 1514. I tried to replicate the same thing but just to another port but cannot get it to work as I get the following... (3 Replies)
Discussion started by: unienewbie
3 Replies

3. Solaris

TCP listner on Solaris server

Hi, I am having a solaris server. I want to start a dummy TCP listner on UNIX OS on a specific port can anyone please let me know the process. IP ADDRESS: 123.123.123.123 Port: 8010 (1 Reply)
Discussion started by: mayank2211
1 Replies

4. Programming

Concurrent TCP client/server

I made a program and now I need to make it concurrent. Can someone pls help me do this ? The code is this: #include <sys/types.h> #include <sys/socket.h> #include <sys/time.h> #include <netinet/in.h> #include <errno.h> #include <unistd.h> #include <stdio.h> #include <string.h>... (4 Replies)
Discussion started by: Johnny22
4 Replies

5. IP Networking

TCP server dies after few hours of inactivity

We have a NAS application which can be accessed by both HTTP and HTTPS connections. The issue we are facing is that the tcp server instance that initiates the HTTP access dies after a few hours of inactivity(the NAS application was kept idle for around 10 hours). However, the tcpserver that... (1 Reply)
Discussion started by: swatidas11
1 Replies

6. Red Hat

tcp/ip problem

how the data from disk is loaded into memory and then it transfered to tcp/ip packet. how i can find how many pages are loaded into memory by that process what is the rate of context switch for that process. (5 Replies)
Discussion started by: amar20
5 Replies

7. Solaris

TCP Problem

I am running a Java Client on Solaris 9 which communicates with the Server using TCP/IP. The client transmits a FIN packet to server. The server sends a ACK, FIN enters LAST_ACK state and then waits for ACK from client. The client did not respond back leaving the server in LAST_ACK itself. Also... (0 Replies)
Discussion started by: diarun
0 Replies

8. Programming

How to check TCP server status

Please tell me according to C/C++ socket programming; how client can check whether server is running or not during TCP communication. (1 Reply)
Discussion started by: mansoorulhaq
1 Replies

9. UNIX for Dummies Questions & Answers

Tcp-server

I have true64 Unix running and there a scales in the sytem which connect thru a com-server to the network. they have their own IP-address and are communicating over port 8000. when I telnet to the com-servers and the print function of the scale is executed I can see the data coming. I need to know... (1 Reply)
Discussion started by: albinhess
1 Replies

10. Programming

Tcp Ip Server

i am programming a tcp_ip server which intends to listen permanently to a client . the client can disconnect and connect again and the server accept it(by this point it works).The problem is when the client lose connection without a disconnect command and my code can't get it and keeps waiting for... (4 Replies)
Discussion started by: massimo_ratti
4 Replies
Login or Register to Ask a Question