Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Java 9 Modularity: Patterns and Practices for Developing Maintainable Applications this week in the Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Serializable problem  RSS feed

 
tony lu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to connect to Server with Socket, and exchange data by ObjectInputStream and ObjectOutputStream. but both sides freeze at " m_input=new ObjectInputStream(m_sock.getInputStream());" , and no exception is thrown.
Is there anyone who knows the reason?
any response is appreciated.
tony


at client side
try{
InetAddress serverAddr=InetAddress.getByName(m_serverName);
m_sock=new Socket(serverAddr.getHostName(),m_serverPort);
m_input=new ObjectInputStream(m_sock.getInputStream());
m_output=new ObjectOutputStream(m_sock.getOutputStream());
m_valid=true;
m_status="Server:"+m_serverName
+" Port:"+m_serverPort+" Connection is successful";
}
catch(IOException e)
{
m_valid=false;
m_status="Server:"+m_serverName
+" Port:"+m_serverPort+" Connection fails";
}

at Server Site
try{
InputStream in=m_sock.getInputStream();
m_input=new ObjectInputStream(in);
OutputStream out=m_sock.getOutputStream();
m_output=new ObjectOutputStream(out);
m_flow=(NetFlow)m_input.readObject();
switch(m_flow.m_command){
case (NetFlow.BOOK):
book();
break;
case (NetFlow.CRITERIA_FIND):
find();
break;
default:
done();
break;
}
m_output.writeObject(m_flow);
m_output.flush();
}catch(IOException e){
error(e);
}
catch(ClassNotFoundException e1)
{
}
finally{
done();
}
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see in the code provided how you set up your ServerSocket. Also it looks like both sides are initially trying to read in an object, but neither side sends one first. So , assuming the ServerSocket is set up correctly, it appears both sides are blocking while they wait to get something on the input stream.
-----------------
Michael Ernest, co-author of:

    The Complete Java 2 Certification Study Guide


    [This message has been edited by Michael Ernest (edited December 19, 2000).]
 
Jerry Pulley
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony,
You have to know the trick for this one. Reverse the two lines so that the <code>ObjectOutputStream</code> is constructed before the <code>ObjectInputStream</code>. The c'tor for the object input stream tries to read the stream header data and blocks until the corresponding object output stream has written it.
Jerry
 
tony lu
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much . it works now, one create output first and the other create input.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!