newbie asking about socketing programming

I have some question about socket programming, clarify me if I have any misunderstanding
Say if I want to build a multi-client IM like this one:
http://java.sun.com/developer/onlineTraining/Programming/BasicJava2/socket.html, the Example 2: Multithreaded Server Example
What I’m able to do is, every client can send a message to the server. And the server receives those messages sent by all clients. Every client receives his OWN response reply by the server.
But this is not IM means. IM means client can talk to client. But with my code, that’s only every client talk to the server separately. I would like to know how to write the code such that client can pass a message to the server. The server then broadcast that message to all clients to let them see.

As my code, everything starts from the clients’ action. But if I want to notify other client (recipient) that a message appears, it somehow needs to trigger by the server.
What is the proper way to make a communication between clients?

This may be naïve for you, or I didn’t explain the question well. Anyway, any advices are welcome.
thank you.

In a client-server architecture, each client can only talk to the server. To send an IM from client to client, client1 sends a message to the server, which sends a message to client2, telling them, “this is from client1”. In your app, you need to implement server logic when a message is received from a client. You can forward that to another client, respond with the same message, send it to all clients, send new messages to any of the clients, etc.

thanks Nate, you clarify my concept.
another question.

if i want to build a multiplayer game, part of the screen there is a text field for players to chat
then should i use 2 thread? one for chating(e.g. BufferedReader/PrintWriter), another for players’ movement (e.g. ObjectInputStream/ObjectOutputStream)
since if I integrate them in a single thread, the run method will block either by BufferedReader.readLine() or ObjectInputStream.readObject()
thanks :stuck_out_tongue:

You’ll have lots of different types of messages flowing back and forth, so there’s no reason
why chat messages shouldn’t be just another type. No need to use a separate stream.

You game logic should never block for I/O. I/O will stall unpredictably and you definitely
do not want that to affect rest of the game. Outgoing messages should be queued
and incoming messages should be received, blessed, and queued for your game engine
to eat.