如何发送java对象抛出socket

时间:2016-11-07 12:50:13

标签: object objectinputstream

我尝试创建一个简单的客户端/服务器java应用程序; 我要发送一个java对象抛出一个socket ... 这是代码:
客户端:

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

服务器:

    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是我在同一个文件夹中创建的java类。
代码:

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

    }

}

现在Class Rilevazione存在于src文件夹中的应用程序(客户端和服务器)中,其中有.java文件,然后是Rilevazione.java 我的问题是关于发送对象throw socket ... javac编译器不起作用,发生的错误是在Rilevazione

的演员阵容中
Rilevazione rl=(Rilevazione) inFromServer.readObject();

在i键入的文件夹中无法编译:javac PerformanceThinClient.java 谁能帮我? 感谢' S

0 个答案:

没有答案