问题
i'm try to create a simple client/server java application;
I have to send a java object throw a socket ...
this is the code:
client:
package performancethinclient;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
public class PerformanceThinClient
{
public static void main(String[] args)
{
startClient("localhost");
}
static void startClient(String HOST)
{
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());
}
}
}
server:
import java.io.*;
import java.net.Socket;
import java.net.ServerSocket;
public class PerformanceServer
{
public static void main(String args[]) throws IOException, InterruptedException
{
StartServer();
}
public static void StartServer()
{
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);
System.out.println("Object Send");
}
}
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());
}
}
}
Rilevazione is a java class that i have create in the same folder .
the code:
package performancethinclient;
import java.io.IOException;
import java.util.*;
public class Rilevazione
{
Date DataOra;
GeneralInfo GeneralInformation ;
CPU ControlProcessingUnit;
RAM RandomAccessMemory;
HDD HardDrive;
Apache apache;
public Rilevazione() throws IOException, InterruptedException
{
DataOra = new Date();
GeneralInformation=new GeneralInfo();
ControlProcessingUnit=new CPU();
RandomAccessMemory=new RAM();
HardDrive=new HDD();
apache=new Apache();
}
public void printit()
{
System.out.println("--------------------------------GENERAL INFORMATION------------------------------------------");
System.out.println("----BIOS");
System.out.println(this.GeneralInformation.BIOS);
}
}
now the Class Rilevazione is present in both application(client and server) in src folder where there are .java files and then Rilevazione.java My problem is about sending object throw socket ... javac compiler does not work , the error occured is in the cast of Rilevazione
Rilevazione rl=(Rilevazione) inFromServer.readObject();
it can't compile when in the folder i type: javac PerformanceThinClient.java Can anyone help me? Thank's
来源:https://stackoverflow.com/questions/40465639/how-to-send-java-object-throw-socket