Hello,
I am writng a simple networking program with a single server and multiple clients. I would like to send and receive objects between the clients and the server and hence I use the ObjectOutputStream/writeObject(Object) and ObjectInputStream/readObject() combination.
First, my server program:
import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.ArrayList;
public class ChatServer implements Runnable {
private Socket socket = null;
private ServerSocket server = null;
private Thread thread = null;
private int ID = 0;
private ArrayList<ObjectOutputStream> list = new ArrayList<ObjectOutputStream>();
private ChatServerThread t;
public ChatServer(int port) {
try {
System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
System.out.println("Waiting for a client ...");
}
catch (IOException ioe) {
System.out.println(ioe);
}
thread = new Thread(this);
thread.start();
}
public void run()
{
while (true) {
try {
socket = server.accept();
ID++;
System.out.println("Client:" + ID + " Accepted " + socket);
t= new ChatServerThread(socket, ID);
t.start();
}
catch (IOException ioe) {
//System.out.println(ioe);
}
}
}
public static void main(String args[]) {
ChatServer server = new ChatServer(2000);
}
private class ChatServerThread extends Thread implements Serializable {
private Socket socket = null;
private ObjectOutputStream objectoutputstream = null;
private Thread t;
private int id;
private String line;
private ObjectInputStream objectinputstream = null;
public ChatServerThread(Socket s, int ID) {
super("Server");
socket = s;
id = ID;
try {
System.out.println ("Start " + id);
objectinputstream= new ObjectInputStream(socket.getInputStream());
System.out.println ("End " + id);
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
try {
objectoutputstream = new ObjectOutputStream(socket.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
list.add(objectoutputstream);
//writer.println(id);
System.out.println ("Inside Thread#" + id + " 's Constructor in Server");
t = new Thread(this);
t.start();
}
public void run() {
while (true)
{
try
{
Object obj = objectinputstream.readObject();
if (obj instanceof String)
{
objectoutputstream.writeObject("Server Echo " + obj);
objectoutputstream.reset();
}
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Connected.");
}
}
}
}
The issue I am having is the following:
The Server is started and the first client connects fine with the server. When the server passes the socket and ID to the ChatServerThread objects's constructor, the server seems to get stuck on the following line fragment within it:
try {
System.out.println ("Start " + id);
objectinputstream= new ObjectInputStream(socket.getInputStream());
System.out.println ("End " + id);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
At this point in the program, the console for the Server shows the following:
Binding to port 2000, please wait ...
Server started: ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=2000]
Waiting for a client ...
Client:1 Accepted Socket[addr=/127.0.0.1,port=56989,localport=2000]
Start 1
So, since I do not see the "End 1", the line in the ChatServer thread that is causing an issue is :
objectinputstream= new ObjectInputStream(socket.getInputStream());
Because of this error, the server does not accept any more client requests as this line is unresolved.
I would be grateful for any help.
Thank you!