Reputation: 33
I'm writing a server/client program. The client sends "Request"s (which are objects designed for this purpose) to the server and server decodes them using an ObjectInputStream. All the "Request" objects are of the same class and just differ in data fields.
Everything usually works; but in some particular states (maybe when the Request object is a bit larger, not however more than 200 kb!) the readObject() on the serverside just blocks with no exception.
Any idea?!
The server code:
public class ConnectionThread extends Thread {
Socket con;
Request request;
public ConnectionThread(Socket s) {
con = s;
try {
ObjectInputStream in = new ObjectInputStream(con.getInputStream());
// Works till here; the object "in" is initialized.
request = (Request) in.readObject();
// This line is not reached, in particular cases.
} catch (ClassNotFoundException ex) {
Logger.getLogger(ConnectionThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ConnectionThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
...
}
The client code:
public static void saveStoreDataForTable(DataTable tb) {
try {
Socket s = new Socket("localhost", portNo);
ObjectOutputStream out = new ObjectOutputStream(s.getOutputStream());
out.writeObject(new Request("saveStoreData",
new Object[]
{tb.getEdited(), tb.getRemoved(), tb.getNewTables(), tb.getAlterations()}));
out.flush();
// These lines work. But I can't get servers respone; again, in some particular cases.
...
}
Upvotes: 0
Views: 1225
Reputation: 310876
You should move that I/O from the constructor to the start() method. At the moment you're doing the I/O in the thread that constructs this thread, which is almost certsinly the wrong thread.
Upvotes: 1