Search code examples
javaobjectserversend

Java send Personalized Object by socket


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


Solution

  • 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