Socket programming ..multi-threaded server?

  • Follow


Hi,

I'm currently doing a C++ socket programming running on SunOS 5.8.

Currently, I have a server that creates threads when multiplie clients
connect.

However, now it's required for me to create a multiple-threaded server.
ie, suppose i have an executable file called "server", and if two
people "./server" it, there should be two instances of the server, each
serving multiple clients.

I tried to run two servers, the first one runs fine, the other would
say "Bind: Address already in use." Anyone has a simple code that could
solve this problem? Or is this even possible?

Sincerely,

Shivesh

0
Reply shivesh (11) 3/31/2005 4:43:52 PM

I didnt quite understand the problem. From your description, your
server is already multithreaded. You said your server already creates
threads for new clients.
You can't run two instances of server if you are listening on a port.
If all your clients try to connect on a single port then there is noway
you can run two instances that can accept new connections.  If you have
more than one port that your server listens on, then you can have each
instance of the server listen on a different port.
What are you trying to acheive by running multiple instances of server?
Since your server, as you described, should be able to handle multiple
clients. 

--MSR

0
Reply MSR 3/31/2005 7:02:11 PM


shivesh@gmail.com wrote:
> Hi,
> 
> I'm currently doing a C++ socket programming running on SunOS 5.8.
> 
> Currently, I have a server that creates threads when multiplie clients
> connect.
> 
> However, now it's required for me to create a multiple-threaded server.
> ie, suppose i have an executable file called "server", and if two
> people "./server" it, there should be two instances of the server, each
> serving multiple clients.
> 
> I tried to run two servers, the first one runs fine, the other would
> say "Bind: Address already in use." Anyone has a simple code that could
> solve this problem? Or is this even possible?

You don't create multiple servers, you use one and everytime you 
accept() a connection, you can fork off a new thread to service 
and then close that socket.

Only one process can bind a socket/port at a time.
0
Reply Ulrich 3/31/2005 8:30:50 PM

<shivesh@gmail.com> wrote in message 
news:1112287432.376240.170510@f14g2000cwb.googlegroups.com...

> I tried to run two servers, the first one runs fine, the other would
> say "Bind: Address already in use." Anyone has a simple code that could
> solve this problem? Or is this even possible?

    It depends on what you want to happen. Presumably, both server programs 
are trying to bind to the same port. What do you want to happen when clients 
connect to that port?

    DS 


0
Reply David 4/1/2005 1:33:17 AM

You can't use the same port to bind to at the same time for two
different servers...if you want to run another instance of your server
you have to change the port.  If you are trying to create a server that
handles concurrent processes from multiple clients it should look
something like

accept()
fork()
close(socket)
exit(0);

here is a straight forward example of a simple echo server that handles
concurrent processes..it is written in C but you should be able to get
the idea if that is what you are having problems with

#define MAX_DATA_LEN 1024

void reaper(int sig){
    printf("wait3 started\n");
	wait3(NULL,0,NULL);
	printf("wait3 ended\n");
	signal(SIGCHLD,reaper);
	 }

/* The main program */
int main(int argc, char **argv) {

	/* Types */
	struct sockaddr_in serv_sin;
	struct sockaddr_in req_sin;
	int serv_sock, req_sock, req_len, len, pid, SRV_PORT;
	char data[MAX_DATA_LEN];

	/* Get a socket: PF means Protocol Family */
	serv_sock = socket(PF_INET, SOCK_STREAM, 0);
	signal(SIGCHLD, reaper);
	/* Check for errors (Winsock does this slightly differently) */
	if (serv_sock == -1) {
		perror("socket");
		exit(1);
	}

	/*Get port number from command line*/

	if (strcmp(argv[1],"port")==0)
	{
            SRV_PORT=atoi(argv[2]);
	}

    else
         {
        printf("Invalid Argument\n");
        exit(1);
    }
	/* Set up information for bind */
	/* Clear the structure so that we don't have garbage around */
	memset((void *)&serv_sin, 0, sizeof(serv_sin));

	/* AF means Address Family - same as Protocol Family for now */
	serv_sin.sin_family = AF_INET;

	/* Fill in port number in address (careful of byte-ordering) */
	serv_sin.sin_port = htons(SRV_PORT);

	/* Fill in IP address for interface to bind to (INADDR_ANY) */
	serv_sin.sin_addr.s_addr = htonl(INADDR_ANY);

	printf("Binding to %s.%d ....\n", inet_ntoa(serv_sin.sin_addr),
		   ntohs(serv_sin.sin_port));

	/* Bind to port and interface */
	if (bind(serv_sock, (struct sockaddr *)&serv_sin,
			 sizeof(serv_sin)) == -1) {
		perror("bind");
		exit(1);
	}

	printf("Bound. Listening....\n");

	/* listen (set backlog to 5 requests) */
	if (listen(serv_sock, 5) == -1) {
		perror("listen");
		exit(1);
	}

	/* Do accept cycle */
	req_len = sizeof(req_sin);


	while (((req_sock = accept(serv_sock, (struct sockaddr
*)&req_sin,&req_len)) != -1)){

  	/* We have a new connection! */
		printf("Connection: %s.%d\n", inet_ntoa(req_sin.sin_addr),
			   ntohs(req_sin.sin_port));
     pid = fork();

     if (pid == -1) {
          printf("Fork Error");
          exit(1);}
      else if (pid == 0) {
         /*Read in Data*/
         while((len = recv(req_sock, data, MAX_DATA_LEN, 0))>0){

		if (len == -1) {
			perror("recv");
			exit(1);}
         /*	Send Back Echo*/
			if (send(req_sock, data, len, 0) == -1) {
			perror("send");
			exit(1);}
          }

         close(serv_sock);
         exit(0);
      }     
        
}
    
	perror("accept");
	exit(0);
}

0
Reply wetherbean 4/1/2005 10:38:13 AM

4 Replies
473 Views

(page loaded in 0.084 seconds)

Similiar Articles:













7/25/2012 9:07:27 PM


Reply: