Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

chat program

 
mark smith
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i try to do a chat program

i have the server and the client...

the server allow many client to connect to it

server code


client code


work fine...

when a client send a message to the client, i would like that all client receive it...

what is the correct way to do it?
create a socket for every client and server connect to it?


thanks
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm..

Sockets are not really beginner's level stuff. I'm moving this to our Sockets and INternet Protocols forum.
 
mark smith
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would like to know how in a application client/server how we send received message to all client?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to do this would be to have the Server keep a list of all clients. You would probably maintain a list of sockets to each of the clients. Then you will iterate through the list of sockets and send the message to each one. Let us know how things work out with this, or if you think of anything yourself.

Layne
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same question. Is it really a good idea to have opened sockets to all clients? I'm closing mine so there is my problem. I don't know when the Applet in this case have closed. If you come up with any solutions to this I would be glad the see it.

// Mathias
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to be able to communicate with the client, it seems to me that you have to have an open socket connection. Unless you have some way to reopen the socket with the same client, then you will need to maintain an open connection.

Layne
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my work, the client is a browser and the server is a big AIX box. To send messages from the server to the client we reverse the roles ... the browser becomes the server and the AIX box becomes the client.

The browser has an applet that holds a ServerSocket open. At logon the browser tells the AIX box its ServerSocket address and the AIX box keeps a list of all the applet servers. To broadcast a message to a user, the AIX box looks up the addres for that user's applet, opens a socket and sends the message. To send to all users, it repeats open-send-close for each registered applet.

Does that sound interesting?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic