UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-02-23 08:14 AM

reirei
Member
Registered: 2007-02-23
Posts: 6

Re: Client connect to multiple servers

Hello,

I'm very new to sockets and I have a question on client/server.

I set up a server which handles multiple client requests easily with select(). But now I am required to have my client be able to connect to multiple servers. I figured that using select() would be a good choice for this as well but I'm not sure how to do it.

Basically I have something set up like this in my client code:

fd_set activeFdSet, readFdSet;
> ....
> ......
> //Create the client socket
> client_socket  = socket(PF_INET, SOCK_STREAM, 0);
> /Initalize set of active sockets
> FD_ZERO(&activeFdSet);
> FD_SET(client_sock, &activeFdSet);
> .....
> ..initalize socket address....
> .......
> readFdSet = activeFdSet;
> if(select(FD_SETSIZE, &readFdSet, NULLL, NULL, NULL) < 0)
>   ..error..
> for (i=0; i<FD_SETSIZE; ++i)
>       if(FD_ISSET(i, &readFdSet)){
>           if(i == client_sock){
                connect(client_sock, (struct sockaddr *)&serverName,
>                  sizeof(serverName))
>                       FD_SET(????);


I'm confused as to what to put into my FD_SET in the last line. How could I get the server number? Could I use the port number?

Is it even possible to have a client connected to multiple servers this way?
I actually asked my TA and she's not sure how to do it. Awesome. :P

Thanks

Offline

#2 2007-02-23 01:46 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Client connect to multiple servers

Of course it's possible...  But, those bits of pseudo-code have some problems (aside
from just having E-mail style ">" quoting in front of them ;-))...

For one thing, NEVER pass FD_SETSIZE as the first arg to select()!  That's just
horrible...  Sure, it'll work, but it's just horribly inefficient in the extreme...  You're
basically making select() perform at its absolute worst all the time...  The proper
thing to do is to keep track of the value of your highest socket FD, and pass one
more than that value to select()...  Yes, that can be tiresome work sometimes, but
it's worth it...  (If you like, you can even be somewhat lazy in not bothering to ever
decrement that max value when you close FDs, but only worry about incrementing
it when you open new ones...)

But, mainly, your code doesn't seem to make any sense, in that you are doing your
select() for readability prior to actually doing connect()...  Prior to connect(), there is
nothing to read, so select()'ing for readability makes absolutely no sense at that time...
Once you have connect()'d, THEN you can add the socket to the fd_set and perform
select() on it...  Now, I don't have any idea of the exact nature of your particular
client, and how you are getting your list of multiple servers to connect to, so I can't
really say for certain how things should be properly setup...  Is the client going to be
given the list of servers at load time (via command-line or config file or something)?
If so, then you basically just want to create all the separate sockets and do the
connect()'s for each one up front, adding each one to the fd_set as you do...  Then,
you sit in select() waiting for incoming data from each one to read...  (Or, perhaps
you need to write to each one first to initiate something?  It's unclear on what type
of servers you're connecting to, or what the ultimate goal is...)

Offline

#3 2007-02-23 07:14 PM

reirei
Member
Registered: 2007-02-23
Posts: 6

Re: Client connect to multiple servers

Thanks so much for your reply!

The whole select() thing is still confusing to me but I think you may have a cleared at least a bit up. I'm still a bit unclear as to how to get the actual servers to add to FD_SET..

Basically the client adds new servers from the command line. The user can type "add-server host port" to add a new server. The client will also receive messages from the servers at any time.

Thanks again

Offline

#4 2007-02-23 08:50 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Client connect to multiple servers

Offline

#5 2007-02-23 09:12 PM

reirei
Member
Registered: 2007-02-23
Posts: 6

Re: Client connect to multiple servers

haha I have no idea why I said command-line. I think I was half asleep when I typed my last post.
But yes, the user just types in commands and no server is connected to client initially.
The client sends a message to the server right after it connected to it. And that is all it sends.

I think I understand now. Thanks so much for your help!
I have one last question though:

When you have a server connected to multiple clients, you do something like:

.....
..
clientSocket = accept(sock, (struct sockaddr *)&clientName, &size);
.......
....
FD_SET(clientSocket, &activeFdSet);

So for a client's case, is there a function that returns an int indicating the serverSocket so I can add it to the FD_SET? I see that connect only returns 0 on success...

Thanks again

Offline

#6 2007-02-23 10:48 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: Client connect to multiple servers

The value you add to your fd_set is the socket FD value returned by your socket()
calls...  You will create a new socket() for every single connection, and call connect()
on each of them separately...  There is no client-side analog of the single listening
socket that servers call accept() on...  You just call socket() + connect() multiple
times, instead...

Offline

Board footer

Powered by FluxBB