• Post Reply Bookmark Topic Watch Topic
  • New Topic

exeption when using ObjectInputSream

 
arona kosari
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I'm writing Messenger project that must add new user, login and send message to client.But these commands at first must go to server and then server must handle them and return back to client.
I have a class that has information about user.for example: ID, passsword,fullname,message and so on.I want to send this object to server.I write this with ObjectInputStream and ObjectOutputSream and it is writen and read by writeObject and readObject.
the problem:when I click on for example login button, it hangs, so I click on Terminate Button(Red button on top of console)and it gives me some exeption like this:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Server.main(Server.java:25)

the line which couse exeption(Server.java:25) is:
ObjectInputStream objReader = new ObjectInputStream(client.getInputStream());

If you have suggestion please help me as soon as possible.
Thanking you in anticipation.
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, arona.
It sounds like your application is functioning correctly. Your client terminates the connection and your server throws an exception indicating the connection was reset. The question is why the object you are sending isn't being read, and for that we need to look at your code. Are you flushing the socket output stream on the client after you write the object? There's plenty of buffering between your client and the network that needs to be cleared out before your packets reach the server.
 
arona kosari
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
Welcome to the JavaRanch, arona.
It sounds like your application is functioning correctly. Your client terminates the connection and your server throws an exception indicating the connection was reset. The question is why the object you are sending isn't being read, and for that we need to look at your code. Are you flushing the socket output stream on the client after you write the object? There's plenty of buffering between your client and the network that needs to be cleared out before your packets reach the server.
 
Arash
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Ess:
Welcome to the JavaRanch, arona.
It sounds like your application is functioning correctly. Your client terminates the connection and your server throws an exception indicating the connection was reset. The question is why the object you are sending isn't being read, and for that we need to look at your code. Are you flushing the socket output stream on the client after you write the object? There's plenty of buffering between your client and the network that needs to be cleared out before your packets reach the server.


*What he said could solve your problem, but Do not forget to serialize your classes; You are using ObjectOutputStream so your classes SHOULD be serialized, or you`ll get another exceptions.
 
arona kosari
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your guidence.
I found my problem.I serialize some of classes that i think it is needed.But it seems that we must Serialize ALL the classes.So I made progress.
But I faced to new exeption- socket closed- and since the solution was intesting for me I want to tell you too.
solution:It was becouse I close ObjectOutputSream object(objwriter in my program) in one of methods I wrote in client class.
I thought since it is created again with clientSocket that is object of Socket class while it isn't closed in program at all, it does not matter to close objwriter.but it seems when I close it, that socket and their connection to server is closed,so that exepction was made.
here is what I did:

try {

//in this line objwriter is created again with socket object that isn't closed.
objWriter = new ObjectOutputStream(clientSocket.getOutputStream());

// accept a message from client console
objWriter.writeObject(command);

// send to server
objWriter.flush();

//objwriter.close(); this line was my problem

} catch (UnknownHostException e) {

e.printStackTrace();
} catch (IOException e) {

e.printStackTrace();
}

Thanks again for your attention.
Arona
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!