Page 1 of 1

Client list in Java

Posted: Fri Apr 29, 2005 6:20 am
by Neo
How do I maintain a list of clients (dynamic structure) connected to a server in Java?

Re:Client list in Java

Posted: Fri Apr 29, 2005 6:31 am
by distantvoices
Hm. I'd have a collection of threads.

say

Code: Select all

class client extends Thread{
  clientSocket ourSocket;
  int id;
  //other stuff you need (start,stop,run methods f. ex.)
};
objects of this class you can stick into a collection (best is a list container). These collections give you a method called getIterator(), which returns the *next* element out of the list.

Have a search in the java docs about client sockets, server sockets (you know these, do you?) and lists/collections/containers.

I've implemented such a beast (a chat client/server thing - it should be online somewhere, but I don't remember clearly) with a multithreaded server.

I'll look into getting the mentioned sources.

stay safe

Re:Client list in Java

Posted: Fri Apr 29, 2005 6:37 am
by distantvoices
Here are some sources about multithreaded java server.

http://www.distantvoices.org/download/javachat.zip

Have a look at them. You can easily exchange the array in the server class with a collection (list collection)?and store the created threads in it.

Re:Client list in Java

Posted: Fri Apr 29, 2005 6:45 am
by Neo
beyond infinity wrote: objects of this class you can stick into a collection (best is a list container). These collections give you a method called getIterator(), which returns the *next* element out of the list.
Ok I never knew of these list things in Java. Must go and have a look.
Any info would be appreciated.

Re:Client list in Java

Posted: Fri Apr 29, 2005 6:53 am
by distantvoices
my friend, google, gave me some info on list collections *chuckle*:

http://java.sun.com/j2se/1.5.0/docs/api ... /List.html

Re:Client list in Java

Posted: Fri Apr 29, 2005 6:58 am
by distantvoices
here's a bit more:

http://javafaq.nu/article734.html

and explicitly, the linked list example from above article:

Code: Select all


import java.util.*;

public class App {
public static void main(String[] args) {

// Use a List implemented as a LinkedList (which maintains the
// collection as a two-way list with each object pointing to
// both the previous and next object).

List myObjects = new LinkedList();

// Loop to store String objects in the collection.

for (int i = 1; i <= 5; i++) {
myObjects.add(new String("This is object " + i));
}

// Display how many objects are in the collection.

System.out.println("The collection has " + myObjects.size() + " objects");

// Instantiate a ListIterator and use loops to display all
// the objects in both the forward and backward directions.

ListIterator pointer = myObjects.listIterator();
System.out.println("Reading forward:");
while (pointer.hasNext()) {
System.out.println(" " + (String) pointer.next());
}
System.out.println("Reading backward:");
while (pointer.hasPrevious()) {
System.out.println(" " + (String) pointer.previous());
}
}
}

Note: the indentation is not by me. SImple copy&paste. *gg*

hth

Re:Client list in Java

Posted: Fri Apr 29, 2005 7:07 am
by Neo
I tried this

Code: Select all

                    clientList.add(client);
Where client was a Socket object and clientList is a LinkeList
but I keep getting a warning on this line
C:\projects\>javac Server.java -Xlint
Server.java:26: warning: [unchecked] unchecked call to add(E) as a member of the
raw type java.util.ArrayList
clientList.add(client);
^
1 warning

Re:Client list in Java

Posted: Fri Apr 29, 2005 7:26 am
by distantvoices
does it say this if you omit that --Xlint switch?

what's this --Xlint thing doing btw? any result promising searchengine keyword available? I canna find info about.

what does it do when you execute your java class? It's just a warning after all, and byte code should be present IIRC.

edit:
and here is a possible solution:
http://forum.java.sun.com/thread.jspa?t ... ID=2818692

Something in the wake of:

Code: Select all

LinkedList<Socket> clientList = new LinkedList<Socket>();
helps?

Re:Client list in Java

Posted: Fri Apr 29, 2005 7:34 am
by Neo
Note: Server.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
This it what is says without that.
Yeah it works. I just wanted to know what the warning was about

Re:Client list in Java

Posted: Fri Apr 29, 2005 7:36 am
by distantvoices
look at the edit in my previous post. Does the warning go away?

They have introduced something called "generics" in java 1.5

Re:Client list in Java

Posted: Sat Apr 30, 2005 1:56 am
by Neo
Yeah now the warnings gone away.
I was using

Code: Select all

List myList= new LinkedList<Socket>();
Now its changed to

Code: Select all

LinkedList<Socket> clientList= new LinkedList<Socket>();