Hi.
In my chat program, I want to create a Vector in my
Server, in which all my clients will be inserted.
My Server run in a thread, but not my Clients: they're
just instances of a class.
First: do I really create a thread for making running
my Client program?
Second: If not, how to insert my client socket in my
Vector?
I created a Vector, every connection is inserted,
but it seems to me there's only one client. Indeed,
in a System.out.println, I only see in my server side
the current client socket. Because of that, I guess,
I can't send the messages of a new connected to the others.
> Hi.
>
> In my chat program, I want to create a Vector in my
> Server, in which all my clients will be inserted.
Sounds like a good plan. This makes it easy to send a message to
all clients. You will want to remove disconnected clients again
of course.
> My Server run in a thread, but not my Clients: they're
> just instances of a class.
>
> First: do I really create a thread for making running
> my Client program?
There are two possible approaches:
Use a thread for each client - each thread will normally be
blocked in a socket read call. Or use the java.nio selector to
tell your one and only thread when a socket needs servicing.
> Second: If not, how to insert my client socket in my
> Vector?
You get the client socket by calling ServerSocket.accept(). Just
call the Vector's add(object) method.
> I created a Vector, every connection is inserted,
> but it seems to me there's only one client. Indeed,
> in a System.out.println, I only see in my server side
> the current client socket. Because of that, I guess,
> I can't send the messages of a new connected to the others.
This sounds like a logic error to me. Without code to look at, I
cannot guess at the problem.
Steve
wanderin - 23 Jun 2005 22:59 GMT
Hi steve, and thanks for your answer.
Here's my main() method, hoping it will help you, to understand my
questions:
public static void main (String[] args)throws IOException {
// initialisation de la socket server
int PORT = 7777;
ServerSocket S_SOCK = new ServerSocket(PORT);
System.out.println("waiting for connections...\n");
startWatcher();
Vector vect = new Vector();
while (true){
try{
Socket listener = S_SOCK.accept();
vect.add(listener);
for (int i = 0; i < vect.size(); i++){
System.out.println("new socket: " + vect.elementAt(i));
}
Server neo_c = new Server(listener);
Thread t = new Thread(neo_c);
t.start();
}
catch (Exception excli){
System.err.println("socket couldn't initialize: " + excli);
}
}
}
Steve Horsley a écrit :
>> Hi.
>>
[quoted text clipped - 31 lines]
>
> Steve
Steve Horsley - 24 Jun 2005 23:14 GMT
> Hi steve, and thanks for your answer.
> Here's my main() method, hoping it will help you, to understand my
[quoted text clipped - 27 lines]
> }
> }
Ah. I think I see your problem. The code is OK as far as it goes,
but the Server class is not given any way to access the main
class (the one who's code you have posted but not named above).
How about passing a reference to the main class to each Server
you create. Then the server can call the main class when it wants
to send to the other sockets (and remove itself from the Vector
when the connection closes). So in the code above, create the
server with:
Server neo_c = new Server(listener, this);
I also suggest that perhaps you should store Servers in
theVector, not Sockets.
Also create new methods:
void broadcast(String msg) {
for(int i = 0 ; i < vect.size() ; i++) {
Server s = (Server) vect.get(i);
server.send(msg)
}
}
void drop(Server srv) {
vect.remove(srv);
}
Now your Servers can call mummy.broadcast(msg) to send a message
to all servers, and mummy.drop(this) when they disconnect.
HTH
Steve