Client list in Java
Client list in Java
How do I maintain a list of clients (dynamic structure) connected to a server in Java?
Only Human
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Client list in Java
Hm. I'd have a collection of threads.
say
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
say
Code: Select all
class client extends Thread{
clientSocket ourSocket;
int id;
//other stuff you need (start,stop,run methods f. ex.)
};
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
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Client list in Java
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.
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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Client list in Java
Ok I never knew of these list things in Java. Must go and have a look.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.
Any info would be appreciated.
Only Human
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Client list in Java
my friend, google, gave me some info on list collections *chuckle*:
http://java.sun.com/j2se/1.5.0/docs/api ... /List.html
http://java.sun.com/j2se/1.5.0/docs/api ... /List.html
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Client list in Java
here's a bit more:
http://javafaq.nu/article734.html
and explicitly, the linked list example from above article:
Note: the indentation is not by me. SImple copy&paste. *gg*
hth
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());
}
}
}
hth
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Client list in Java
I tried this
Where client was a Socket object and clientList is a LinkeList
but I keep getting a warning on this line
Code: Select all
clientList.add(client);
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
Only Human
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Client list in Java
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:
helps?
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>();
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Client list in Java
This it what is says without that.Note: Server.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Yeah it works. I just wanted to know what the warning was about
Only Human
-
- Member
- Posts: 1600
- Joined: Wed Oct 18, 2006 11:59 am
- Location: Vienna/Austria
- Contact:
Re:Client list in Java
look at the edit in my previous post. Does the warning go away?
They have introduced something called "generics" in java 1.5
They have introduced something called "generics" in java 1.5
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
BlueillusionOS iso image
Re:Client list in Java
Yeah now the warnings gone away.
I was using
Now its changed to
I was using
Code: Select all
List myList= new LinkedList<Socket>();
Code: Select all
LinkedList<Socket> clientList= new LinkedList<Socket>();
Only Human