簡體   English   中英

SocketException:序列化時連接重置

[英]SocketException: Connection reset when serializing

我有一個基本的 TCP 連接,我通過序列化將 object 從客戶端發送到服務器。 但是在執行這個簡單的任務時我遇到了一個錯誤,下面是代碼:

客戶:

public client() throws IOException{
    Socket socket = new Socket("127.0.0.1", 4390);
    System.out.println("Client connected with server");
    
    Student student = new Student(1, "jemoi", "lerry");
    
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    oos.writeObject(student);
}

服務器:

public server() throws IOException, ClassNotFoundException{
    ServerSocket serverSocket = new ServerSocket(4390);
    
    System.out.println("Server initialized successfully");
    
    Socket socket = serverSocket.accept();
    
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
    Student student = (Student)ois.readObject();
    
    System.out.println("Object send from client: " + student.getFirstName());
}
    

錯誤:

Exception in thread "main" java.net.SocketException: Connection reset

錯誤發生在以下行:

Student student = (Student)ois.readObject();

找到了解決方案。 我忘記在客戶端設置來自 ObjectOutputStream 的 close()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM