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: Using fork() with a telnet-esque socket connection - comp.lang ...Socket Programming - The NEW Troubleshooters.Com And ... For a socket connection to a server on the same box, a 1 second ... fork()'ed or multi threaded socket I/O ... fork (UDP) server - comp.unix.programmer... Design a multi-threaded server ... server architecture. But if you aren't familiar with multi-threaded programming, can be a ... simple UDP server to be setup, create socket ... socket lock in multithred programming - comp.unix.programmer ...Even Stevens book socket programming, I don't think it mentions about one thread only reading to and the ... I have tried all the case of socket server processes in real ... Reconnecting to the same socket - comp.unix.programmerYes, you can connect to the same server-socket again ... Hi I use one thread as listening thread, accept ... Unix & Linux: Reconnecting to the same socket - programming ... Sockets in gfortran? - comp.lang.fortranAll this socket stuff is working for me, with gfortran. To embark on multi-language programming ... Qt, and multi-threaded ... with the server via streaming TCP/IP socket ... socket in use ? - comp.protocols.time.ntpSocket server includes a big library and will be ... and try to pass the connect socket to another thread by ipc msg. ... socket programming...lsof? - comp.lang.ruby Hi All, Is ... What is the better programming environment - Windows or UNIX ...... it might sound typical or whatever, but I am a student that was programming in ... Communication between server/client (sockets)! - comp.unix ... And, is it better to submit ... New APL2 Programming Language forum - comp.lang.apl... from the distribution list for server change notifications that were sent to newsgroup owners a few months ago. In any case, we have created a new APL2 Programming ... Listen to multiple ports - comp.lang.c... at a server running Apache web server (in MPM mode ie. non-threaded), I ... to limit each instance to a single socket ... get a good book about Unix network programming ... how to pass socket between threads in the same process - comp.unix ...... to pass the connect socket to another thread by ... It connects to server and if ECONNREFUSED, close socket, sleep ... socket lock in multithred programming - comp.unix.programmer ... C# Multi threaded socket programmingC# Multithreaded Socket Programming describes that a Multithreaded Socket Server can communicate with more than one client at the same time in the same network C# Multi threaded Server Socket programmingHow to C# Multi threaded Server Socket programming ... MultiThreaded Server Socket Program here is a C# Console based application, that can handle multiple clients ... 7/25/2012 9:07:27 PM
|