DeanR
DeanR

Reputation: 370

multiple threads, java chat client

I'm developing a simple chat client and I would need the server to handle multiple threads (1 per connection)

At the moment I only have 1 user and 1 connection

Thread con = new Thread(new Connection());
con.start();

Connection() is responsible for listening for messages from this particular connection and broadcasting them to each client (at the moment there is only one)

I plan to create an array of Connection objects and create a thread for each but i'm not sure what i should do from here on, what does 'con' actually represent in this case?

Upvotes: 1

Views: 911

Answers (3)

DoubleDouble
DoubleDouble

Reputation: 1493

At the moment you have one Thread and one User, as you said.

Thread con = new Thread(new Connection());
con.start();

If you wanted multiple threads, handling multiple connections, you would want more than one con. (con is a thread, we want multiple threads)

perhaps a for-loop would work for you?

Thread[] connections = new Thread[/*number of connections*/];
for (int i = 0; i < connections.length(); i++)
{
    connections[i].start();
}

to expand even more, in the real world you likely won't know how many connections you will need until people try to connect. In this case you would want to use a Collection of some kind and a while() loop. ArrayList is a good starter.

ArrayList<Thread> connections = new ArrayList<Thread>();
while (true)
{
    Thread c = new Thread(new Connection());
    connections.add(c);
    c.start();
}

Your setup is a little strange, but I am assuming somewhere under the new Connection constructor you are calling ServerSocket.accept(). If you are not, you need someway to only make a thread when a client connects or this while loop will run rampant. Normally, I would have a Connection object which handles its own Thread and other details, rather than a Thread which handles its Connection object. You may want to adapt some of your code in the end.

Upvotes: 0

Paul Gray
Paul Gray

Reputation: 546

If Connection is a custom class containing information about a certain connection (which I assume it is), then you don't want to pass it into the Thread.

You could probably benefit from reading the Java documentation concerning Defining and Starting a Thread. What you probably want is to start a new Thread() every time you receive a connection from a client. You can accomplish this with this snippet:

new Thread(){
    public void run() {
        System.out.println("blah");
    }
}.start();

Whatever code you put inside the run() function will get run inside a thread.

To answer the second part of you question, in your example, your con object represents a single instance of a thread of execution.

Upvotes: 1

Guy Gavriely
Guy Gavriely

Reputation: 11396

see an example of java chat server here, specifically:

private ArrayList<ClientSocket> clients;

...

while (!disconnect){
    Socket skt = srvr.accept();
    ClientSocket client = new ClientSocket(skt);
    keepAlive.addToQueue(client);
    clients.add(client);
}

Upvotes: 1

Related Questions