Java send Personalized Object by socket

给你一囗甜甜゛ 提交于 2019-12-12 03:53:38

问题


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

Here the client:

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

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: the file strutture : src >performancethinclient->- files.class


回答1:


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


来源:https://stackoverflow.com/questions/40461714/java-send-personalized-object-by-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!