Java Client/Server chat room sending messages as objects - java

My goal is to create a server which receives a Message object from ObjectInputStream, and then echos that Message object to ObjectOutputStream.
Upon writing a message, the client sends the Message type object to the server, the received Message type object (as well as the Message type object from other clients connected to the server) is then received and decoded to the client's gui.
The Message object has a String, and other font information.
My problem is that the server does not echo back the Message type object. I have a feeling that I am not casting properly, but at this point I'm just fishing with trying different things.
The idea is to make the server transparent from the client - is this possible, ie: server does not know anything about the TestObject class? Is there a way around this?
Server code:
import java.net.*;
import java.io.*;
public class Server
{
public SimpleServer() throws IOException, ClassNotFoundException
{
ServerSocket ss = new ServerSocket(4000);
Socket s = ss.accept();
ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
// the 'echo' functionality of the server
Object to = null;
try
{
to = ois.readObject();
}
catch (ClassNotFoundException e)
{
System.out.println("broke");
e.printStackTrace();
}
oos.writeObject(to);
oos.flush();
// close the connections
ois.close();
oos.close();
s.close();
ss.close();
}
public static void main(String args[]) throws IOException, ClassNotFoundException {
new SimpleServer();
}
}
Client code:
import java.net.*;
import java.io.*;
public class Client
{
public SimpleClient() throws IOException, ClassNotFoundException
{
Socket s = new Socket( "localhost", 4000 );
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = new ObjectInputStream( s.getInputStream());
TestObject to = new TestObject( 1, "object from client" );
// print object contents
System.out.println( to );
oos.writeObject(to);
oos.flush();
Object received = ois.readObject();
// should match original object contents
System.out.println(received);
oos.close();
ois.close();
}
public static void main(String args[]) throws IOException, ClassNotFoundException
{
new SimpleClient();
}
}
class TestObject implements Serializable
{
int value ;
String id;
public TestObject( int v, String s )
{
this.value=v;
this.id=s;
}
#Override
public String toString()
{
return "value=" + value +
", id='" + id;
}
}
Thank you for your time!
EDIT:
This is the output created:
I get this message when the client connects to the server:
Exception in thread "main" java.lang.ClassNotFoundException: TestObject
And this is the client output when I run the client:
value=1, id='object from client Exception in thread "main" java.net.SocketException: Connection reset

First thing : TestObject class is not visible in your Server code. That's why it is throwing ClassNotFoundException.
Solution to this : Put your TestObject class in seperate file and then use import statement to import that class.
Second: Try to print the value of received object at server side. And made changes to your client code as below
Object received = null;
while(received==null)
{
received = ois.readObject();
}
System.out.println(received);

I don't see any problem with your code except you need to flush the oos stream after you write to it in both the server and the client.
oos.flush();
Also for making the server transparent of the client, you can avoid casting on server(which is you are already doing) and the end point should worry about the TestObject which is your client.

Related

Java Publisher Sever chat program

I am trying to create a chat application which has one publisher, one server and multiple subscribers. The publisher(Sending to port 8000) sends a message to the server(listening on port 8000 and 5000) and which forwards it further to the subscriber(listening on port 5000).
Now so far I can create multiple publishers and the communication between server and publisher is working, however, I am not able to send it to the subscriber the message sent by the publisher
Server Side Code
package serverclient;
import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Server extends Thread{
private Socket socket;
private int clientNumber;
public Server(Socket socket, int clientNumber){
this.socket = socket;
this.clientNumber = clientNumber;
if(socket.getLocalPort() == 5000)System.out.print("\nSubscriber "+ clientNumber +" is connected to the server");
if(socket.getLocalPort() == 8000)System.out.print("\nPublisher "+ clientNumber +" is connected to the server");
}
#Override
public void run(){
try {
BufferedReader dStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
while(true){
synchronized(this){
String clMessage = dStream.readLine();
System.out.println("\n"+clMessage);
// if(socket.getLocalPort() == 5000){
out.println("Hey the server is sending the message to subscriber");
// }
//out.println("Hey the publisher has sent the message : " + clMessage);
}
}
} catch (IOException ex) {
System.out.print("\nError has been handled 1\n");
}finally{
try {
socket.close();
} catch (IOException ex) {
System.out.print("\nError has been handled 2\n");
}
}
}
public static void main(String [] args) throws IOException{
int subNumber = 0;
int pubNumber = 0;
ServerSocket servSockpub = new ServerSocket(8000);
ServerSocket servSocksub = new ServerSocket(5000);
try {
while (true) {
Server servpub = new Server(servSockpub.accept(),++pubNumber);
servpub.start();
System.out.print("\nThe server is running on listen port "+ servSockpub.getLocalPort());
Server servsub = new Server(servSocksub.accept(),++subNumber);
servsub.start();
System.out.print("\nThe server is running on listen port "+ servSocksub.getLocalPort());
}
} finally {
servSockpub.close();
servSocksub.close();
}
}
}
publisher code
package serverclient;
import java.net.*;
import java.io.*;
public class Publisher {
public static void main (String [] args) throws IOException{
Socket sock = new Socket("127.0.0.1",8000);
// reading from keyboard (keyRead object)
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
// sending to client (pwrite object)
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
System.out.println("Start the chitchat, type and press Enter key");
String receiveMessage,sendMessage;
while(true)
{
sendMessage = keyRead.readLine(); // keyboard reading
pwrite.println(sendMessage); // sending to server
pwrite.flush(); // flush the data
if((receiveMessage = receiveRead.readLine()) != null) //receive from server
{
System.out.println(receiveMessage); // displaying at DOS prompt
}
else{
System.out.print("Null");
}
}
}
}
subscriber
package serverclient;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class Subscriber {
public static void main (String [] args) throws IOException{
Socket sock = new Socket("127.0.0.1",5000);
// receiving from server ( receiveRead object)
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
System.out.println("Recive side");
String receiveMessage, sendMessage;
while(true)
{
System.out.print("Hey man " + receiveRead.readLine() + "\n");
if((receiveMessage = receiveRead.readLine()) != null) //receive from server
{
System.out.println(receiveMessage); // displaying at DOS prompt
}
else{
System.out.print("Null");
}
}
}
}
Any help is appreciated. I just want to figure out why subscriber is not reciveing message
There are many possibilities to handle realm time communication issues. I myself prefer the use of Events / EventListeners.
Currently in your program there is no communication between the Server as such and the threads which handle the subscriber connection.
Also on a side node: even with a proper communication between publisher connection threads and subscriber connection threads it won't work now since you are using the same Server class. This does not only violate the Single-Responsibility-Principle but will also prevent the server from ever sending a message to the Subscriber.
Let's say you have establish a connection and your server class is now connected with the subscriber. What will happen?
The subscriber will loop until there is a message on the input stream of his socket. Good that is exactly what we want. But what does the server do? The truth is exactly the same. The first few statements in the try block of your Server's run method are to create a BufferedReader and read from it until a message receives. And now we have a socket on each site which will infinitly wait for some kind of message to arrive (which will obviously never happen since both are waiting for something).
To prevent this you should check if there is anything to read on the stream first:
while ( true )
{
if ( socket.getInputStream().available() != 0 )
{
// reading logic goes here....
synchronized ( this )
{
String clMessage = dStream.readLine();
System.out.println( "\n" + clMessage );
out.println( "Hey the server is sending the message to subscriber" );
}
}
// what shall be done when not reading.
}
Now the second part. If you want to communicate between threads you need to implement some logic to do so. As stated above I love the concept of Listeners so i will show an example where I make use of them:
MessageReceivedListener.java
import java.util.EventListener;
public interface MessageReceivedListener
extends EventListener
{
public void onMessageReceived( String message );
}
Note: The interface does not have to extend EventListener since EventListener
is just a tagging interface. I myself still prefer to have this as a reminder for what purpose the interface is there.
Server.java (excerpt)
// New constructor since we will pass a Listener now. Also new local variable for it.
public Server( Socket socket, int clientNumber, MessageReceivedListener mrl )
{
this.socket = socket;
this.clientNumber = clientNumber;
this.mrl = mrl;
if ( socket.getLocalPort() == 5000 )
System.out.print( "\nSubscriber " + clientNumber + " is connected to the server" );
if ( socket.getLocalPort() == 8000 )
System.out.print( "\nPublisher " + clientNumber + " is connected to the server" );
}
The new constructor provides a way to pass the MessageReceivedListener to the Server object. Alternatively you can alsocreate a setter for it.
synchronized ( this )
{
String clMessage = dStream.readLine();
System.out.println( "\n" + clMessage );
out.println( "Hey the server is sending the message to subscriber" );
mrl.onMessageReceived( clMessage );
}
This is where the magic happens. After whe receive the message we just pass it to the onMessageReceived(String message) method of the listener. But what does it do exactly? This is what we define when creatinga Server object.
Here are two examples, one with anonymous classes (Java 7 and before) and on with lambdas (Java 8 and later).
Example Java 7 and earlier
Server servpub = new Server( servSockpub.accept(), ++pubNumber,
new MessageReceivedListener()
{
#Override
public void onMessageReceived( String message )
{
// call nother local method
// this method would need to be a static method of Server
// because it's in the scope of your server class
sendMessageToSubscribers(message);
}
} );
Here we pass an anonymous class as our MessageReceivedListener object and define it's behaviour (in this case just calling another method which will handle the rest.
Now since our MessageReceivedListener interface does only contain one method we can also see it as a functional interface and therefore use lambdas to shorten the code and improve readability.
Example with Lambda (Java 8 and later)
Server servpub = new Server( servSockpub.accept(), ++pubNumber, Server::sendMessageToSubscribers);
In this specific case we only have one argument which we want to pass to a method and therefore can use a method reference.
How to actually implement the method sendMessageToSubs(String message) is up to you. But you need to keep track of how many Threads with subscriber connections have been created and how you want to reference them.

Java - Error Sending ImageIcons from server to client via ObjectOutputStream read/writeObject()

Little background, currently trying to complete an assignment for my degree, trying to implement an 'Auction House' localhost server-client program in which clients can bid on items saved on the server. When a new client connects to the server the user has to register his/her name and then the server will send the client all the item information.
Currently all the other information sends correctly however when I try to send the images of the items to the clients via ObjectOutputStream the server always fails to send the ImageIcons, when I then disconnect the client I am met with this error message:
Exception in thread "main" java.io.StreamCorruptedException: invalid type code: 53 at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at AuctionClient.retrieveItemInformation(AuctionClient.java:365)
at AuctionClient.main(AuctionClient.java:341)
I think it might have something to do with that Im trying to send ImageIcons directly through the output stream without utilising serialisable but our java lecturer told us that ImageIcon didn't require that to function properly so i'm unsure, any advice would be great! I will place the code for server/clients below!
Server thread code:
public ClientHandler(Socket socket, BiddingItem item1, BiddingItem item2, int number) throws IOException
{
client = socket;
outputImage = new ObjectOutputStream(client.getOutputStream());
input = new Scanner(client.getInputStream());
output = new PrintWriter(client.getOutputStream(), true);
startedItem1 = item1;
startedItem2 = item2;
clientNumber = number;
images = new ImageIcon[2];
images[0] = startedItem1.getImage();
images[1] = startedItem2.getImage();
}
Trying to send information to client:
public void run()
{
//previous code...
for (int i=0; i<images.length;++i)
{
try
{
outputImage.writeObject(images[i]);
}
catch (IOException ioEx)
{
System.out.println("\n**ERROR: Unable to send Item " + i + " image to client!**");
}
try
{
outputImage.flush();
}
catch (IOException e1)
{
System.out.println("\n**ERROR: Unable to flush OutputObjectStream!**");
}
}
Client creating input stream:
public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException
{
//previous code
try
{
txtBoxServerMessage.setText("CLIENT: Attempting to connect to server...");
Thread.sleep(2000);
socket = new Socket(host, PORT);
inputFromServer = new Scanner(socket.getInputStream());
inputImageFromServer = new ObjectInputStream(socket.getInputStream());
outputToServer = new PrintWriter(socket.getOutputStream(), true);
}
catch(IOException ioEx)
{
txtBoxServerMessage.setText("CLIENT-ERROR: Unable to connect to server! Exiting...");
Thread.sleep(2000);
System.exit(1);
}
}
Function that client uses to accept information:
private static void retrieveItemInformation() throws InterruptedException, ClassNotFoundException, IOException
{
txtBoxServerMessage.setText("CLIENT: Waiting for Item Information...");
Thread.sleep(3000);
clientNumber = Integer.parseInt(inputFromServer.nextLine());
item1Code = inputFromServer.nextLine();
item1Desc = inputFromServer.nextLine();
item1Deadline = inputFromServer.nextLine();
item1AuctionValue = Double.parseDouble(inputFromServer.nextLine());
item2Code = inputFromServer.nextLine();
item2Desc = inputFromServer.nextLine();
item2Deadline = inputFromServer.nextLine();
item2AuctionValue = Double.parseDouble(inputFromServer.nextLine());
item1Image = (ImageIcon)inputImageFromServer.readObject(); // READING IMAGE 1
item2Image = (ImageIcon)inputImageFromServer.readObject(); // READING IMAGE 2
txtBoxServerMessage.setText("DEBUG: FINISHED IMAGE");
txtBoxServerMessage.setText("CLIENT: Information Recieved! Displaying...");
Thread.sleep(1000);
comboItemSelect.addItem(item1Code);
comboItemSelect.addItem(item2Code);
txtBoxClientNumber.setText(String.valueOf(clientNumber));
updateDisplay();
}
Cheers!
M
The problem is this:
outputImage = new ObjectOutputStream(client.getOutputStream());
// ...
output = new PrintWriter(client.getOutputStream(), true);
You can't wrap a single OutputStream in two different wrappers. Their individual states and buffering are not compatible.
In particular, if you write even a single byte using anything other than your ObjectOutputStream, the object stream is corrupted.
If you want to send both objects and text, you can write a String object to the ObjectOutputStream. You also can call ObjectOutputStream.writeUTF directly.
Problem solved, deleted the Printwriter & Scanner streams and replaced them with Object streams in both the clients and the Server and utilised the .writeObject & readObject methods, I also needed to ensure that the paths for the images were correctly implemented. Thanks for the help!

ObjectOutputStream not sending data (server stuck at socket.accept())

I am trying to send an object over a connection using Java. This is my client side code. The server receives the data the first time, but the second time is stuck at socket.accept(). Am i doing something wrong here? Thank you!
public class Client
{
public static void main(String args[])
{
try
{
Socket socket = new Socket("localhost", 40003);
ClientObject c = new ClientObject(socket);
c.init();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
class ClientObject
{
Socket socket;
public ClientObject(Socket socket)
{
this.socket = socket;
}
public void init()
{
try
{
// computation
SendObject so = new SendObject(toSend1, toSend2, rand, username.length());
// send all of this
OutputStream o = socket.getOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(o);
oos.writeObject(so);
o.flush();
InputStream i = socket.getInputStream();
ObjectInputStream ois = new ObjectInputStream(i);
// works, data received from the server
so = (SendObject)ois.readObject();
// further computation
so = new SendObject(null, digest, rand + 1, -1);
oos.reset();
oos.writeObject(so);
// doesn't work :(
o.flush();
}
}
Two things:
Flush the ObjectOutputStream instead of the underlying OutputStream.
The client it's using the same stream and thus the same connection to write to the server, hence the server should not expect a second accept but rather keep reading the stream of the same connection. You can of course also set up a new connect (Socket) and send the second object over that socket. Then the server should accept another socket.
Looks for me, that the server has successfully accepted the connection, otherwise you would not be able to send and receive. Look at your server code the problem may be that you close() the socket connection after the server has received from the client. Pleas provide your server code to have a look at.

ObjectInputStream and getInputStream

I'm working on a school project and the following codes are an example provided for building the project (should work without any problem but not...). There was no compiling error but when I use telnet to test it the following message shows:
java.io.StreamCorruptedException: invalid stream header: 56543130
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at ThreadedDataObjectHandler.run(ThreadedDataObjectServer.java:41)
Line 41 is
ObjectInputStream in =new ObjectInputStream(incoming.getInputStream());
Here are my codes:
import java.io.*;
import java.net.*;
import java.util.*;
public class ThreadedDataObjectServer {
public static void main(String[] args ) {
try
{ ServerSocket s = new ServerSocket(3000);
for (;;)
{ Socket incoming = s.accept( );
new ThreadedDataObjectHandler(incoming).start();
}
}
catch (Exception e)
{ e.printStackTrace();
}
}
}
class ThreadedDataObjectHandler extends Thread
{
DataObject myObject = null;
private Socket incoming;
private String greeting="Hello!";
public ThreadedDataObjectHandler(Socket incoming)
{
this.incoming = incoming;
}
public void run()
{ try
{
ObjectInputStream in =new ObjectInputStream(incoming.getInputStream());
ObjectOutputStream out =new ObjectOutputStream(incoming.getOutputStream());
myObject = (DataObject)in.readObject();
System.out.println("Message read: " + myObject.getMessage());
myObject.setMessage("Got it!");
System.out.println("Message written: " + myObject.getMessage());
out.writeObject(myObject);
in.close();
out.close();
incoming.close();
}
catch (Exception e)
{ e.printStackTrace();
}
}
}
class DataObject implements Serializable{
protected String message;
public DataObject(){
message="";
}
public void setMessage(String m){
message=m;
}
public String getMessage(){
return message;
}
}
What I tried was to switch the order of statements ObjectInputStream in=... and ObjectOutputStream out=... but no luck. Please help...thanks.
From what I understood from the comments you are trying to read the objects from a telnet connection using ObjectInputStream.
You cannot do that. If you are going to use ObjectInputStream then you need the other connecting program to write using a ObjectOutputStream.
You telnet client don't really give a shit about the Java ObjectOutputStream, ObjectInputStream and Serialization.
So I'd try something like a InputStreamReader wrapped in a BufferedReader.
If you just want to test the connectivity just write a small java program that will connect to your program instead of using telnet.
David as I mentioned in the comments you have to write a client which uses ObjectOutputStream to send the same DataObject to the server socket.
Since you are expecting DataObject a client needs to send the DataObject. When you use telnet it connects but from there you cannot send the DataObject in a way java Object stream understands.
Please see http://zerioh.tripod.com/ressources/sockets.html for server/client example.
Also since its some school exercise try to understand the concept and do not just copy.

how to communicate between client and server in java

I have a chat program. Now the code works for communicate between client and server via command line. But it gives an exception (java.net.SocketException: Socket is closed) while running. Please help me to fix that problem.
In a java chat program,how will the communication be implemented between client and server?
ie.
client<-->server (between server and client)
or
client A<-->server<-->client B (server act as a bridge between two clients)
Is the 2 way communication can be implemented through a single socket?
Are there any other methods ?
How to communicate more than one client simultaneously?
server code
class Server
{
ServerSocket server;
Socket client;
public Server()
{
try
{
server = new ServerSocket(2000);
System.out.println("\tServer Started..........");
while (true)
{
client = server.accept();
Send objsend = new Send(client);
Recive objrecive = new Recive(client);
//client.close();
}
}
catch (Exception e)
{
System.out.println("Exception4 " + e);
}
}
public static void main(String arg[])
{
new Server();
}
}
class Recive implements Runnable
{
Socket client;
public Recive(Socket client1)
{
client=client1;
Thread trsend=new Thread(this);
trsend.start();
}
public void run()
{
ObjectInputStream ois;
Message M=new Message();
try
{
ois = new ObjectInputStream(client.getInputStream());
M = (Message)ois.readObject();
M.display();
ois.close();
}
catch (Exception e)
{
System.out.println("Exception1 " + e);
}
}
}
class Send implements Runnable
{
Socket client;
public Send(Socket client1)
{
client=client1;
Thread trrecive=new Thread(this);
trrecive.start();
}
public void run()
{
Message M=new Message();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
try
{
System.out.println("Me(server)");
M.strmessage=br.readLine();
ObjectOutputStream oos=new ObjectOutputStream(cli ent.getOutputStream());
oos.writeObject((Message)M);
oos.flush();
oos.close();
}
catch (Exception e)
{
System.out.println("Exception " + e);
}
}
}
client code
class Client
{
public static void main(String arg[])
{
try
{
Send objsend=new Send();
Recive objrecive=new Recive();
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}
class Send implements Runnable
{
public Send()
{
Thread trsend=new Thread(this);
trsend.start();
}
public void run()
{
try
{
Message M=new Message();
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
while(true)
{
System.out.println("Me(client)");
M.strmessage=br.readLine();
Socket client=new Socket("localhost",2000);
ObjectOutputStream oos=new ObjectOutputStream(client.getOutputStream());
oos.writeObject((Message)M);
oos.flush();
oos.close();
}
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}
class Recive implements Runnable
{
public Recive()
{
Thread trrecive=new Thread(this);
trrecive.start();
}
public void run()
{
try
{
while(true)
{
Socket client=new Socket("localhost",2000);
ObjectInputStream ois=new ObjectInputStream(client.getInputStream());
Message CNE=(Message)ois.readObject();
CNE.display();
ois.close();
}
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}
First of all, don't close the streams in every run().
Secondly, check whether port for server which you are using is free.
This program makes your pc both host and server.
import java.io.IOException;
import java.net.*;
public class ClientServer {
static byte[] buffer = new byte[100];
private static void runClient() throws IOException {
byte buffer[] = new byte[100];
InetAddress address = InetAddress.getLocalHost();
DatagramSocket ds=new DatagramSocket();
int pos = 0;
while (pos<buffer.length) {
int c = System.in.read();
buffer[pos++]=(byte)c;
if ((char)c =='\n')
break;
}
System.out.println("Sending " + pos + " bytes");
ds.send(new DatagramPacket(buffer, pos, address, 3000));
}
private static void runServer() throws IOException {
InetAddress address = InetAddress.getLocalHost();
DatagramSocket ds = new DatagramSocket(3000, address);
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);
String s=new String(dp.getData(),0,dp.getLength());
System.out.print(s);
}
public static void main(String args[]) throws IOException {
if (args.length == 1) {
runClient();
} else {
runServer();
}
}
}
also follow this link
There could be multiple places where the exception could be thrown. Without a stack trace it is difficult to state so accurately, as to the cause of failure.
But the root cause, is essentially due to the fact that you are closing the InputStream of the socket in your Receiver threads after reading a message, and closing the OutputStream of the socket in your Sender threads after sending a message. Closing either of these streams will automatically close the socket, so you if attempt to perform any further operation on it, a SocketException will be thrown.
If you need to ensure that your server and client do not shutdown in such an abrupt manner, you'll have to keep reading the InputStream (until you get a special message to shutdown, for instance). At the same time, you'll also have to keep writing to the OutputStream. Two-way communication is definitely possible, and the posted code is capable of the same (if the socket remains open).
If you have to handle multiple clients, you'll need multiple reader and writer threads on the server, each listening on an instance of a Socket returned from ServerSocket.accept(); in simpler words, you need a reader-writer pair listening on a distinct socket on the server for each client. At the moment, multiple clients can connect to the Server, as each incoming connection is provided its own client Socket object on the Server, that is provided to individual reader and writer threads. The main Server thread can continue to receive incoming connections and delegate the actual work to the reader-writer pairs.
chat programms normaly have a server through which all communication goes. The reason is that other wise every client needs to know how to reach every other client. And that doesn't work in the general case.
So you'll have a server, every client registers and talks with the server, which will forward messages to other clients.
Mostly communication is done via HTTP cause this is likely to go through firewalls and proxies. You probably want to read up on long polling if you are planning for anything serious.

Categories