Client list in Java

Programming, for all ages and all languages.
Post Reply
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Client list in Java

Post by Neo »

How do I maintain a list of clients (dynamic structure) connected to a server in Java?
Only Human
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Client list in Java

Post 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
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Client list in Java

Post 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.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Client list in Java

Post 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.
Only Human
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Client list in Java

Post 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
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Client list in Java

Post 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
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Client list in Java

Post 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
Only Human
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Client list in Java

Post 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?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Client list in Java

Post 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
Only Human
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Client list in Java

Post 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
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Client list in Java

Post 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>();
Only Human
Post Reply