Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2005

Tip: Looking for answers? Try searching our database.

programming sockets: insert a client in a vector

Thread view: 
wanderin - 23 Jun 2005 17:07 GMT
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.
Steve Horsley - 23 Jun 2005 20:51 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.