Java通过套接字发送个性化对象

Java通过套接字发送个性化对象

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

问题描述

我的问题是关于java发送个性化对象,我创建了一个类 Rilevazione,然后我想将一个对象 Rilevazione 发送给读取该对象并打印它的客户端.这里是服务器:

my problem is about java send personalized object,i create a class Rilevazione, than i want to send a object Rilevazione to a client that read the object and print it.here the server:

 try
        {
            ServerSocket welcomeSocket = new ServerSocket(50000);

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

                Rilevazione rl=new Rilevazione();
                outToClient.writeObject(rl);

            }

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

这里是客户:

try
    {

            Socket clientSocket = new Socket(HOST, 50000);
            // Create the input & output streams to the server
            ObjectInputStream inFromServer = new ObjectInputStream(clientSocket.getInputStream());

            Rilevazione rl=(Rilevazione)inFromServer.readObject();
            inFromServer.close();
            clientSocket.close();

            rl.printit();




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

java 编译器给我一个错误:

the java compiler give me an error:

error: cannot find symbol
            Rilevazione rl=(Rilevazione)inFromServer.readObject();
                            ^

为什么?谁能帮我?对象是否必须序列化而不是反序列化?

why?can anyone help me?does the object must be serialized and than deserialized?

ps:文件结构:src >performancethinclient->-files.class

ps: the file strutture :src >performancethinclient->- files.class

推荐答案

正如你所说,如果所有类都在同一个包中,你可以使用以下命令进行编译:

As you told me, if the classes all are in the same package you can compile with the following command:

javac -cp . *.java

要运行您的应用程序,请转到根文件夹并运行以下命令:

To run your application go to the root folder and run the following:

java -cp . performancethinclient.PerformanceThinClient

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

08-06 02:56