0

I want to send object (custom class) via socket from Client to Server. This is my code:

Server:

private class SocketServerThread extends Thread {

    @Override
    public void run() {
        try {
            serverSocket = new ServerSocket(socketServerPORT);

            while (true) {
                clientSocket = serverSocket.accept();
                ObjectInputStream inObject = new ObjectInputStream(
                        clientSocket.getInputStream());
                try {
                    Te xxx = (Te) inObject.readObject();

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


                //DataInputStream dataInputStream = new DataInputStream(
                        //clientSocket.getInputStream());
                //messageFromClient=dataInputStream.readUTF();

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //activity.msgFromC.setText(messageFromClient);
            }
        });
    }
}

Client:

public Client(String aIPaddres, int aPort, Te u) {
    AddressIP = aIPaddres;
    Port = aPort;
    sendUser = u;
}
protected Void doInBackground(Void... arg0) {

    Socket clientSocket = null;

    try {

        clientSocket = new Socket(AddressIP, Port);
        ObjectOutputStream outObject = new ObjectOutputStream(
                clientSocket.getOutputStream());
        outObject.writeObject(sendUser);
        //DataOutputStream daneWyjsciowe = new DataOutputStream(
                //clientSocket.getOutputStream());
        //daneWyjsciowe.writeUTF("Czesc!" );

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        //response = "UnknownHostException: " + e.toString();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        //response = "IOException: " + e.toString();
    } finally {
        if (clientSocket != null) {
            try {
                clientSocket.close();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return null;

}

Custom class (Te.class):

public class Te implements Serializable {
public String message;
public Te(String m){
    message=m;
}

}

When I passing simple data i.e. String there is no problem. But now I want to pass object, but there is always ClassNotFoundException at server. I read lot of stacks but I didnt find answer. Could U help me?

Magnus2005
  • 97
  • 3
  • 13
  • Do the server and client contain a copy of the exact same `Te` class? – Vince May 28 '16 at 20:44
  • Yes. In both there is Te.class – Magnus2005 May 28 '16 at 20:56
  • Make sure they are the exact same copy of the class, not just a class with the same name. If server's `Te` has a constructor with `String` parameter, the client's `Te` needs one too. Also make sure both copies of the class define a `serialVersionID` – Vince May 28 '16 at 20:59
  • Like I said in both there is the same class. Should this code work if in both is the same class? Or is there other method to send object? – Magnus2005 May 28 '16 at 21:08
  • could it be becsuse of package name? I am code in android studio for android. – Magnus2005 May 29 '16 at 08:53
  • have a look at this one http://stackoverflow.com/questions/26150491/java-sockets-sending-objects-and-distributing-the-object-to-all-clients-that-a – TechDog Dec 07 '16 at 15:59

0 Answers0