将对象从客户端发送到服务器

时间:2016-01-11 13:16:01

标签: java sockets

我想实现一个简单的服务器,它能够从客户端接收一个对象。从客户端发送的对象是在服务器端接收(强制转换)的对象的子类。

编辑:如果我使用相同的子类“TestMessage”,则在服务器端打印正确的字符串。如果我使用超类“消息”(我想这样做,因为我需要多个消息子类)字符串只是null。那么是否有一个ObjectInputStream的特定属性,如果我转换为超类,它会弄乱我的字段或整个对象?

Payload on client side: Hello Server!
Payload on server side: null

我创建了一个抽象类Message,从中可以派生出几个子类:

public abstract class Message implements Serializable {

    private static Random random = new Random();

    /**
     * A 16-byte string (GUID) uniquely identifying the message on the network. 
     */
    private byte[] guid;

    /**
     * Indicates the type of message.
     */
    private byte messageType;

    /**
     * The actual content of the message.
     */
    private static String payload;

    /**
     * TODO change to protected?
     * @return payload
     */
    public static String getPayload() {
        return payload;
    }

    /**
     * TODO change to protected?
     * @param payload
     */
    public static void setPayload(String payload) {
        payload = payload;
    }

    /**
     * @return guid
     */
     public byte[] getGuid() {
        return guid;
    }

    /**
     * @return messageType
     */
    public byte getMessageType() {
        return messageType;
    }

    /**
     * Constructor for a newly created message
     * @param messageType
     */
    public Message(byte messageType) {
        guid = new byte[16];
        random.nextBytes(guid);

         //TODO encrypt guid?

        this.messageType = messageType;
    }

    /**
     * Constructor for a received message
     * @param guid
     * @param messageType
     */
    public Message(byte[] guid, byte messageType) {
        this(messageType);
        this.guid = guid;
    }
}

现在,我已经派生了一个测试消息类型TestMessage,它从客户端发送到服务器。

public class TestMessage extends Message {

    private static byte TYPE_ID = (byte) 0x00;

    /**
     * @param payload
     */
    public TestMessage(String payload) {
        super(TYPE_ID);

        /**
         * TODO: Better practice to create a protected setter?
         */
        Message.setPayload(payload);
    }
}

现在服务器没什么特别的了。它等待客户端连接并从流中读取Message对象。在这里,我得到一个ClassNotFoundException。我想Server类不知道Message类?为什么这样?客户端发送服务器读取的类的子类是一个问题吗?或者我的Message类的转换是否有问题?

public class TestServer {

    public static void main(String[] args) throws IOException {

        int port = 9999;

        ServerSocket serverSocket = new ServerSocket(port);
        Socket socket = serverSocket.accept();

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

        Message incomingMessage = null;
        incomingMessage = (Message)objectInputStream.readObject();

        System.out.println("Payload: " + Message.getPayload());
    }

}

最后,客户:

public class TestClient {

    public static void main(String[] args) throws IOException {

        String serverAdress = "localhost";
        int port = 9999;

        Socket socket = new Socket(serverAdress, port);

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

        TestMessage testMessage = new TestMessage("Hello Server!");

        objectOutputStream.writeObject(testMessage);
    }
}

提前谢谢你们!我希望有人可以帮助我。

3 个答案:

答案 0 :(得分:3)

在您的应用程序的接收方上,您没有定义对象的类,因此无法对其进行反序列化并构建实例。

该类必须存在于双方(客户端和服务器)。

检查您的类路径以查看该类是否存在。最后添加正确的jar(或.class)。

答案 1 :(得分:2)

您获得null的原因是:

    public static void setPayload(String payload) {
        payload = payload;
    }

注意命名空间。此方法为自身提供参数,然后最终销毁对象。如果要将参数分配给静态成员,则必须使用Message.payload正确地对其进行处理。但在这种情况下使用静态成员并不是一个好主意。

这样会更好,因为你想对一个对象(不是一个类)进行de / serialize:

private String payload;

public String getPayload() {
    return payload;
}
public void setPayload(String payload) {
    this.payload = payload;
}

请务必相应地更改TestClient/TestServer中的方法调用。

最后提示:如果您不关闭TestClient/TestServer中的套接字,则在多次运行这两个类时可能会得到java.net.SocketException个(取决于您的操作系统)。

答案 2 :(得分:1)

服务器端需要Message类实现,以便从客户端发送的对象在服务器上反序列化。通过这个,我的意思是服务器的类路径jvm(TestServer主类)应该包含包含类Message的字节码的jar。