Send object over socket
<p>I'm trying to send a custome object through socket in java. I know that i need to put class that has object i need to send in same package, have the same serialVersionUID and implement Serializable. I have already did it but i still can't send object over socket. Where did i wrong ?<br>
Here is client code:<br>
Class user in client</p>

<pre><code>package data;
import java.io.Serializable;

public class User implements Serializable {

    /**
     * 
     */
    public static final long serialVersionUID = 1L;
    private String username = null;
    private String hashedPassword = null;
    private Integer code = null;
    private String from = null;
    private String to = null;
    private Object data = null;

    public User() {

    }

    public User(Integer code) {
        this.code = code;
    }

    public User(Integer code, Object data) {
        this.code = code;
        this.data = data;
    }

    public User(String username, String hashedPassword, Integer code, String from, String to, Object data) {
        this.username = username;
        this.hashedPassword = hashedPassword;
        this.code = code;
        this.from = from;
        this.to = to;
        this.data = data;
    }

    public Integer getCode() {
        return code;
    }
    // other getters and setters
}
</code></pre>

<p>Code to send object of user class:</p>

<pre><code>package tankclient;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import data.*;

public class Main {
    public static void main(String args[]) {
        Socket socket = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        boolean isConnected = false;
        String ipServer = "localhost";
        int portServer = 4444;
        try {
            // Print("connecting to server...");
            socket = new Socket(ipServer, portServer);
            isConnected = true;
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
            oos.writeObject(new User());
            // Print("connect server success");
        } catch (Exception ex) {
            isConnected = false;
            ex.printStackTrace();
        }
        // DataTransfer transfer = new DataTransfer();
        // //transfer.SendDataRequest(new DataRequest(3));
        // transfer.TestSend();
    }
}
</code></pre>

<p>Here is the code of server:<br>
Code of user class: Exactly the same as user of client ( i copy from client and paste them into server )<br>
Code of server to recieve user object:</p>

<pre><code>package tankserver;

import data.*;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Main {

    public static void main(String args[]) {
        ServerSocket serverSocket = null;
        Socket socket = null;
        ObjectOutputStream oos = null;
        ObjectInputStream ois = null;
        boolean isConnected = false;
        int portServer = 4444;
        try {
            System.out.println("waiting for client to connect ...");
            serverSocket = new ServerSocket(portServer);
            socket = serverSocket.accept();
            oos = new ObjectOutputStream(socket.getOutputStream());
            ois = new ObjectInputStream(socket.getInputStream());
            isConnected = true;
            System.out.println("connected");
            User obj = (User) ois.readObject();
            System.out.println(obj.toString());
        } catch (Exception ex) {
            isConnected = false;
            ex.printStackTrace();
        }
        //DataTransfer transfer = new DataTransfer();

        //transfer.GetDataRequest();
        //transfer.TestRecieve();
    }

}
</code></pre>

<p>here is stacktrace i recieve ( in the server side ):</p>

<blockquote>
  <p>java.net.SocketException: Connection reset    at
  java.net.SocketInputStream.read(SocketInputStream.java:209)   at
  java.net.SocketInputStream.read(SocketInputStream.java:141)   at
  java.net.SocketInputStream.read(SocketInputStream.java:223)   at
  java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2296)
    at
  java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(ObjectInputStream.java:2476)
    at
  java.io.ObjectInputStream$BlockDataInputStream.refill(ObjectInputStream.java:2546)
    at
  java.io.ObjectInputStream$BlockDataInputStream.skipBlockData(ObjectInputStream.java:2448)
    at
  java.io.ObjectInputStream.skipCustomData(ObjectInputStream.java:1944)
    at
  java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1621)
    at
  java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518)
    at
  java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371)
    at tankserver.Main.main(Main.java:31) BUILD SUCCESSFUL (total time: 8
  seconds)</p>
</blockquote>

<p>when i try to debug then follow situation happen: when i put break point in client (server run normaly) and run step by step then it work but when i put break point in server ( client run normaly ) and run step by step then it throw exception in line 28 ois = new ObjectInputStream(socket.getInputStream());</p>