3

オブジェクトの出力ストリームと入力ストリームを利用するアプリを作成しています。ただし、オブジェクトを適切に送信できないため、問題があります。ストリームに書き込むと、クライアントとサーバーの両方が同じシリアル ID を持つこのクラスのまったく同じコピー (唯一の違いはパッケージ名) を持っていても、サーバーはクラスが見つからないという例外を返します。これが私のクラスです:

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--;
    }
}

}

私は今かなり混乱しています。どうしてそんなことが起きているのかわからないからです。

メッセージを文字列に置き換えると、次のコードがうまくいくという私の意見で最も重要なことは何ですか。

4

2 に答える 2

8

唯一の違いはパッケージ名です

それはうまくいきません。クラス名 (パッケージ名を含む) は両側で同じでなければなりません。

于 2011-11-12T10:06:03.747 に答える
3

唯一の違いはパッケージ名です

その場合、それらは同じクラスではありません。両側にまったく同じクラスがあることを確認してください。つまり、同じパッケージです。

于 2011-11-12T10:06:49.290 に答える