Question abut concurrent TCP server

Question abut concurrent TCP server

Post by Davi » Wed, 26 Jul 2000 04:00:00



Hi

I am using my simple clientserver program (uses sock_stream) to transmit
some data. Server will receive data and increment an index, each time it
receives a data.

The server is made concurrent. I can send data to server without any
problem, but as soon as, the server receives data 398 times, it wont receive
any more and I should restart my server program and listen in some different
port. Let say I start my server program and listening on a port, and from
client side, sending 500 times 1000 bytes data. The server will receive 398
times, but wont receive the rest of data. I dont know what is going wrong.

I have also other question, if I make the same server as a regular
(iterative) server it will receive all data, but in a strange way. Let say,
the server is listening in a port and the client sending 500 * 1000 bytes,
the server will receive the first data packet and show the index, but will
wait until it has received the entire data packet (499) and later show
index. It is not receiving one by one. I dont know if it should be like
that or there is some thing wrong.

I have my code for both concurrent and iterative server below.

Thanks for your concern  David

/* Here is the Concurrent server  */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#define mesage
     int sockfd, newsockfd, portno, clilen;
     char buffer[5000];
     struct sockaddr_in serv_addr, cli_addr;
     int n, i ;   int nr;
     int childpid;

void error(char *msg)
{
    perror(msg);
    exit(1);

Quote:}

void send_data(){
     i = i+1;
    if (newsockfd < 0){
          error("ERROR on accept");
          exit(0);
    }

   if((n=recv(newsockfd,buffer,5000,0))==-1)
      {
            perror("recv");
            exit(1);
      }
// printf("Here is the %d message: %s\n",i,buffer);
 printf("Here is the %d message with length %d\n",i,strlen(buffer));

   //rearead the buffer
   do {
      bzero(buffer, sizeof buffer);
      if((n=recv(newsockfd,buffer,5000,0))==-1)
      {
            perror("recv");
            exit(1);
      }
   //  printf("Here is the %d message: %s\n",i,buffer);
     if (strlen(buffer) > 0)
     printf("Here is the %d message with length %d\n",i,strlen(buffer));

   }while ((strlen(buffer))!= 0);

Quote:}

int main(int argc, char *argv[])
{
     i = 0;
      nr =0;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

     if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
              error("ERROR on binding");
     listen(sockfd,1000);
     bzero(buffer,5000);

    while(1){
     clilen = sizeof(cli_addr);
     newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
     if ((childpid = fork()==0))
     {
       close(sockfd);
       send_data();
       close(newsockfd);
       exit(0);

    }
      close(newsockfd);
      i = i+1;
  }

Quote:}

/////////////////////

/*  Her is the iterative server */

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#define mesage
     int sockfd, newsockfd, portno, clilen;
     char buffer[5000];
     struct sockaddr_in serv_addr, cli_addr;
     int n, i ;   int nr;

void error(char *msg)
{
    perror(msg);
    exit(1);

Quote:}

void send_data(){
     newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen);
    if (newsockfd < 0){
          error("ERROR on accept");
          exit(0);
    }
    i++;
   if((n=recv(newsockfd,buffer,5000,0))==-1)
      {
            perror("recv");
            exit(1);
      }
// printf("Here is the %d message: %s\n",i,buffer);
 printf("Here is the %d message with length %d\n",i,strlen(buffer));

   //rearead the buffer
   do {
      bzero(buffer, sizeof buffer);
      if((n=recv(newsockfd,buffer,5000,0))==-1)
      {
            perror("recv");
            exit(1);
      }
   //  printf("Here is the %d message: %s\n",i,buffer);
     printf("Here is the %d message with length %d\n",i,strlen(buffer));

   }while ((strlen(buffer))!= 0);

int main(int argc, char *argv[])
{
     i = 0;
      nr =0;
     if (argc < 2) {
         fprintf(stderr,"ERROR, no port provided\n");
         exit(1);
     }
     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0)
        error("ERROR opening socket");
     bzero((char *) &serv_addr, sizeof(serv_addr));
     portno = atoi(argv[1]);
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(portno);

     if (bind(sockfd, (struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
              error("ERROR on binding");
     listen(sockfd,10);
     bzero(buffer,5000);

    while(1){
    clilen = sizeof(cli_addr);
    send_data();
    }

Quote:}

////////////////////////////////////////////
 
 
 

Question abut concurrent TCP server

Post by David Malo » Wed, 26 Jul 2000 04:00:00


I didn't try to compile it, but I think you missed out a chunk of
your code, I'd be suprised it it compiles in the form it was posted.

The first thing I'd check is to make sure you close all your sockets
when you are finished with them, as you may be running out of file
descriptors. The iterative server code doesn't have any calls to
close() in it, but that may be a cut&paste problem.

        David.

 
 
 

Question abut concurrent TCP server

Post by David Schwart » Wed, 26 Jul 2000 04:00:00



>    if((n=recv(newsockfd,buffer,5000,0))==-1)
>       {
>             perror("recv");
>             exit(1);
>       }
> // printf("Here is the %d message: %s\n",i,buffer);
>  printf("Here is the %d message with length %d\n",i,strlen(buffer));

>    //rearead the buffer
>    do {
>       bzero(buffer, sizeof buffer);
>       if((n=recv(newsockfd,buffer,5000,0))==-1)
>       {
>             perror("recv");
>             exit(1);
>       }
>    //  printf("Here is the %d message: %s\n",i,buffer);
>      if (strlen(buffer) > 0)
>      printf("Here is the %d message with length %d\n",i,strlen(buffer));

>    }while ((strlen(buffer))!= 0);

> }

        TCP is a byte stream protocol. It provides a stream of bytes, not a
stream of messages.

        DS

 
 
 

1. highly concurrent tcp server

I'm having to code a server that handles thousands if not tens of thousands
of SSL connections.
My OS of choice is FreeBSD 4.5-RELEASE using kqueue for polling connections.
In some tests I've just whipped up, the server seems to handle a fair few
connections (2000-ish) when I run something like:
 # ab -c 1000 -t 3000 -n 100000 <server-name>

Unfortunately I only seem to be able to run 2-3 ab processes per machine
before ab won't run any more because it runs out of file descriptors I
presume.
I've set the following sysctl options in the kernel:
  kern.ipc.somaxconn=8192
  kern.ipc.maxsockets=16424
  kern.maxfiles=65536
  kern.maxfilesperproc=32768
  ... other performance related options
So don't see why I should be running out of resources so quickly.

You may be wondering what this has to do with programming.. Well, is this
the right way to go about creating this kind of server?
Should each client keep a ssl tcp-connection open for it's entire existance
or should it close the connection after each request. Ideally I need the
server to be able to notify every client or individual clients of changes.

Anyone have any experience in this area? Ideas? Suggestions?

2. Determining Disk Size from OBP?

3. Looking for examples for Concurrent TCP Echo Server ?

4. Sun as Web Server - a good idea?

5. nonblocking concurrent tcp server design

6. PPP / MSTPPP symbolic link problem

7. Concurrent server question

8. ftw(3) and symlinks

9. newbie question abut Linksys and Redhat

10. Newbie Questions abut Kernel and VPN

11. Simple Question abut X mode changes?!

12. socket concurrent server vs. RPC server

13. dispatching server events in concurrent server