0

I made a simple object that I'm trying to send though the OOS but everytime I try to send the class that I created it throws an IOException. But if I try to send objects through that I know are serializable it doesn't throw an error.

UserName userName = new UserName();
_out.write("Object is Made! \n");
serverOutput.writeObject(userName);
serverOutput.flush();

Here's the Username Code:

public class UserName implements Serializable {

private String _userName;

public UserName(String userName) {
    _userName = userName;
}

public UserName(){
    _userName = "John Doe";
}

/**
 * @return the _userName
 */
public String get_userName() {
    return _userName;
}

/**
 * @param _userName the _userName to set
 */
public void set_userName(String userName) {
    _userName = userName;
}

}

If I do this then it doesn't throw the error:

UserName userName = new UserName();
_out.write("Object is Made! \n");
serverOutput.writeObject(new String());
serverOutput.flush();

Do I actually have to implement any methods or am I forgetting something?

Jake B
  • 672
  • 1
  • 9
  • 21
  • What does the IOException say? – FThompson Aug 12 '12 at 00:08
  • How do I check that? Print the StackTrace? I'm running this from a .jar executable – Jake B Aug 12 '12 at 00:14
  • Run via CMD and uncaught exception stacktraces will be printed to the system console. – FThompson Aug 12 '12 at 00:23
  • java.net.SocketException: Connection reset by peer: socket write error at java.net.SocketOutputStream.socketWrite0(Native Method) at java.net.SocketOutputStream.socketWrite(Unknown Source) at java.net.SocketOutputStream.write(Unknown Source) at java.io.ObjectOutputStream$BlockDataOutputStream.drain(Unknown Source) at java.io.ObjectOutputStream$BlockDataOutputStream.setBlockDataMode(Unknown Source) at java.io.ObjectOutputStream.writeNonProxyDesc(Unknown Source) – Jake B Aug 12 '12 at 00:51
  • at java.io.ObjectOutputStream.writeClassDesc(Unknown Source) at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeFatalException(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at Code.Client.main(Client.java:83) – Jake B Aug 12 '12 at 00:51
  • Please tell us what's going on on the server side. What code is doing the reading? If the client is getting "Connection reset by peer" the server may have closed the connection or died unexpectedly. Presumably the server is deserializing the object from the connection. Note that the UserName class must be accessible to the server. – Stuart Marks Aug 12 '12 at 05:36
  • Yes I realized that the program finished execution before the other one could read the object. I fixed it by sleeping client thread for one second after it writes the object. is there someway to block the thread until the server completes so an error like this can be avoided without affecting runtime? – Jake B Aug 14 '12 at 01:49

1 Answers1

0

The receiving end has probably closed the connection. Maybe there's an exception there as well?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Yes I realized that the program finished execution before the other one could read the object. is there someway to block the thread untill it completes? – Jake B Aug 14 '12 at 01:47
  • @ReCoNciLiaTiOn You've misunderstood my answer. It is the receiving end that is probably closed the connection, so you can't write to it. The solution is to fix the implementation of your application protocol, probably by defining it properly first, so that peers don't close connections until they have read everything they are supposed to read. No 'block' required, just correct logic. – user207421 Aug 21 '12 at 05:36