This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes chat program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "chat program" Watch "chat program" New topic

chat program

mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 251

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?

Joel McNary

Joined: Aug 20, 2001
Posts: 1824


Sockets are not really beginner's level stuff. I'm moving this to our Sockets and INternet Protocols forum.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 251
i would like to know how in a application client/server how we send received message to all client?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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.


Java API Documentation
The Java Tutorial
Mathias Nilsson
Ranch Hand

Joined: Aug 21, 2004
Posts: 367
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

Joined: Dec 06, 2001
Posts: 3061
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.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: chat program
It's not a secret anymore!