简体   繁体   English

通过java中的套接字发送对象

[英]Sending an object through a socket in java

I have 2 java netbeans projects, one is the Server other is the Client. 我有2个java netbeans项目,一个是Server,另一个是Client。 I have a Message class that I have created which I want to pass to the server and back the other way to the client after modification done at the server. 我有一个我创建的Message类,我希望将其传递给服务器,并在服务器上完成修改后返回到客户端。 I have included the class Message in both projects. 我在两个项目中都包含了Message类。 I use ObjectOutputStream and ObjectInputStream to pass the object. 我使用ObjectOutputStreamObjectInputStream来传递对象。 The connection between the server and the client is ok and the object passes through but at the server when I read the object from the ObjectInputStream using readObject() method I type cast it to the Message class. 服务器和客户端之间的连接是正常的,对象通过但在服务器上,当我使用readObject()方法从ObjectInputStream读取对象时,我将其类型转换为Message类。 But an ClassNotFoundException is thrown at the server. 但是在服务器上抛出了ClassNotFoundException It cant find the Message class. 它找不到Message类。 How can I resolve this? 我该如何解决这个问题?

Code for the client: 客户代码:

public void startClient() {
    try {
        // Create the socket
        Socket clientSocket = new Socket(HOST, PORT);
        // Create the input & output streams to the server
        ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
        ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());

        // Read modify
        // TODO here

        /* Create The Message Object to send */
        LinkedList<Message> msgList = new LinkedList<>();
        Message msg = new Message();
        msg.setMessage("Kasun");
        msg.setIndex(1);
        msg.setAverage(5.5f);
        msgList.push(msg);

        /* Send the Message Object to the server */
        outToServer.writeObject(msgList);            

        /* Retrive the Message Object from server */
        LinkedList<Message> inFromServerList = new LinkedList<>();
        Message msgFrmServer = null;
        inFromServerList = (LinkedList<Message>)inFromServer.readObject();
        msgFrmServer = inFromServerList.pop();

        /* Print out the recived Message */
        System.out.println("Message: " + msgFrmServer.getMessage());
        System.out.println("Index: " + msgFrmServer.getIndex());
        System.out.println("Average: " + msgFrmServer.getAverage());


        clientSocket.close();

    } catch (Exception e) {
        System.err.println("Client Error: " + e.getMessage());
        System.err.println("Localized: " + e.getLocalizedMessage());
        System.err.println("Stack Trace: " + e.getStackTrace());
    }
}

Code for the Server 服务器代码

public void startServer() {

    try {
        ServerSocket welcomeSocket = new ServerSocket(PORT);

        while (true) {    
            // Create the Client Socket
            Socket clientSocket = welcomeSocket.accept();
            System.out.println("Socket Extablished...");
            // Create input and output streams to client
            ObjectOutputStream outToClient = new ObjectOutputStream(clientSocket.getOutputStream());
            ObjectInputStream inFromClient = new ObjectInputStream(clientSocket.getInputStream());

            // Read modify
            // TODO here

            /* Create Message object and retrive information */
            LinkedList<Message> inList = new LinkedList<>();
            Message inMsg = null;
            inList = (LinkedList<Message>)inFromClient.readObject();
            inMsg = inList.pop();

            /* Modify the message object */
            inMsg.setMessage(inMsg.getMessage().toUpperCase());
            inMsg.setIndex(5);
            inMsg.setAverage(10.5f);

            /* Send the modified Message object back */
            outToClient.writeObject(inMsg);        

        }

    } catch (Exception e) {
        System.err.println("Server Error: " + e.getMessage());
        System.err.println("Localized: " + e.getLocalizedMessage());
        System.err.println("Stack Trace: " + e.getStackTrace());
        System.err.println("To String: " + e.toString());
    }
}

The exception thrown at the "server" 在“服务器”抛出的异常

java.lang.ClassNotFoundException: rusl.online.examination.system.client.utility.Message

Would I have to use java RMI? 我必须使用java RMI吗? Is there a solution for this without using RMI? 有没有使用RMI的解决方案?

Make sure that Message on server side is rusl.online.examination.system.client.utility.Message . 确保服务器端的消息是rusl.online.examination.system.client.utility.Message It seems not to be the case. 似乎并非如此。

If you are sending objects of a class over the socket. 如果要通过套接字发送类的对象。 In order to reproduce the object at the other end, you have to cast the object. 为了在另一端再现对象,您必须转换对象。 For that you will require the class definition on both ends. 为此,您将需要两端的类定义。

Make sure you properly import the same Message class for server program as well, which you used to create the message. 确保为服务器程序正确导入相同的Message类,用于创建消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM