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

时间:2016-11-07 09:26:09

标签: java object server send

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

 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编译器给我一个错误:

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

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

ps:文件strutture: src> performancethinclient-> - files.class

1 个答案:

答案 0 :(得分:0)

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

javac -cp . *.java

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

java -cp . performancethinclient.PerformanceThinClient