本文介绍了通过java中的套接字发送对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个java netbeans项目,一个是Server,另一个是Client。我有一个我创建的Message类,我希望将其传递给服务器,并在服务器上完成修改后返回到客户端。我在两个项目中都包含了Message类。我使用 ObjectOutputStream ObjectInputStream 来传递对象。服务器和客户端之间的连接是正常的,当我从 ObjectInputStream 使用 readObject()读取对象时,对象通过但在服务器上方法我输入将其转换为消息类。但是服务器会抛出 ClassNotFoundException 。它找不到Message类。我该如何解决这个问题?



客户代码:

  public void startClient(){
try {
//创建套接字
套接字clientSocket = new Socket(HOST,PORT);
//创建输入&输出流到服务器
ObjectOutputStream outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());

//读取修改
// TODO这里

/ *创建发送消息对象* /
LinkedList<消息> msgList = new LinkedList<>();
消息msg = new Message();
msg.setMessage(Kasun);
msg.setIndex(1);
msg.setAverage(5.5f);
msgList.push(msg);

/ *将消息对象发送到服务器* /
outToServer.writeObject(msgList);

/ *从服务器* /
中回复消息对象LinkedList<消息> inFromServerList = new LinkedList<>();
消息msgFrmServer = null;
inFromServerList =(LinkedList< Message>)inFromServer.readObject();
msgFrmServer = inFromServerList.pop();

/ *打印出已收到的Message * /
System.out.println(Message:+ msgFrmServer.getMessage());
System.out.println(Index:+ msgFrmServer.getIndex());
System.out.println(Average:+ msgFrmServer.getAverage());


clientSocket.close();

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

服务器代码

  public void startServer(){

try {
ServerSocket welcomeSocket = new ServerSocket(PORT);

while(true){
//创建客户端套接字
套接字clientSocket = welcomeSocket.accept();
System.out.println(Socket Extablished ...);
//为客户端创建输入和输出流
ObjectOutputStream outToClient = new ObjectOutputStream(clientSocket.getOutputStream());
ObjectInputStream inFromClient = new ObjectInputStream(clientSocket.getInputStream());

//读取修改
// TODO这里

/ *创建消息对象和反向信息* /
LinkedList<消息> inList = new LinkedList<>();
消息inMsg = null;
inList =(LinkedList< Message>)inFromClient.readObject();
inMsg = inList.pop();

/ *修改消息对象* /
inMsg.setMessage(inMsg.getMessage()。toUpperCase());
inMsg.setIndex(5);
inMsg.setAverage(10.5f);

/ *将修改后的Message对象发回* /
outToClient.writeObject(inMsg);

}

} catch(例外e){
System.err.println(服务器错误:+ e.getMessage());
System.err.println(Localized:+ e.getLocalizedMessage());
System.err.println(Stack Trace:+ e.getStackTrace());
System.err.println(To String:+ e.toString());
}
}

服务器抛出的异常

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

我是否必须使用java RMI?有没有使用RMI的解决方案?

解决方案

确保服务器端的消息是 rusl .online.examination.system.client.utility.Message 。似乎并非如此。


I have 2 java netbeans projects, one is the Server other is the 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. I have included the class Message in both projects. I use ObjectOutputStream and ObjectInputStream to pass the object. 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. But an ClassNotFoundException is thrown at the server. It cant find the Message class. 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? Is there a solution for this without using RMI?

解决方案

Make sure that Message on server side is rusl.online.examination.system.client.utility.Message. It seems not to be the case.

这篇关于通过java中的套接字发送对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 01:38