3

我正在编写一个使用对象输出和输入流的应用程序。但是我有一个问题,因为我无法正确发送我的对象。我把它写到流中,服务器给了我一个未找到的类异常,即使客户端和服务器都有完全相同的这个类的副本(唯一的区别是包名)和相同的序列号。这是我的课:

import java.io.Serializable;

public class Message implements Serializable {

/**
 * 
 */
private 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 Message() {

}

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

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

public Message(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
}

那是我要发送的对象的类。这是客户端代码:

(它只是为了测试而编写的)

public static void main(String[] args) {
        try {
            Socket s = new Socket("localhost", 5656);
            ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

            Message m = new Message("user3", "123", 100, "2011-06-11 22:22:22",
                "2011-06-11 22:22:22", "test");
        oos.writeObject(m);
        oos.flush();


        ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
        Message n = (Message) ois.readObject();
        System.out.print(n.toString());

        oos.close();
        ois.close();

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

这是服务器代码的一部分(是的,它是多线程的):

package systemZarzadzaniaReporterami.serwer;

import java.beans.ExceptionListener;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.sql.SQLException;

public class ClientHandler extends Thread {

private ExceptionListener exceptionListener;

private Socket socket;
private String ip;

public ClientHandler(ExceptionListener exceptionListener, Socket socket) {
    this.exceptionListener = exceptionListener;
    this.socket = socket;
    ip = socket.getInetAddress().getHostAddress();
}

public void run() {
    Message in = null;
    ObjectInputStream inputStream = null;
    ObjectOutputStream outputStream = null;

    try {
    //  ClientListener.CONNECTION_COUNTER++;

        inputStream = new ObjectInputStream(socket.getInputStream());

        in = (Message) inputStream.readObject();
        MessageProcessor messageProcessor = new MessageProcessor();

        System.out.print(in.getUsername());

        outputStream = new ObjectOutputStream(socket.getOutputStream());

        Message out = messageProcessor.process(in, ip);
                System.out.print(in.getCode().toString());      
        outputStream.writeObject(out);
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
        exceptionListener.exceptionThrown(new ServerException(Codes.ERR_CONNECTION_ERROR));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
        exceptionListener.exceptionThrown(new ServerException(Codes.ERR_MYSQL_ERROR));
    } finally {
        if (inputStream != null)
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                exceptionListener.exceptionThrown(new ServerException(Codes.ERR_CONNECTION_ERROR));
            }
        if (outputStream != null)
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
                exceptionListener.exceptionThrown(new ServerException(Codes.ERR_CONNECTION_ERROR));
            }
        if (socket != null)
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
                exceptionListener.exceptionThrown(new ServerException(Codes.ERR_CONNECTION_ERROR));
            }
    //  ClientListener.CONNECTION_COUNTER--;
    }
}

}

我现在很困惑。因为我不知道为什么会发生这样的事情。

我认为最重要的是,当我用字符串替换 Message 时,以下代码表现良好。

4

2 回答 2

8

唯一的区别是包名

那是行不通的。两边的类名(包括包名)必须相同。

于 2011-11-12T10:06:03.747 回答
3

唯一的区别是包名

那么他们就不是同一个班级了。确保两边的课程完全相同。这意味着相同的包装。

于 2011-11-12T10:06:49.290 回答