2013-03-06 140 views
4

我正在將客戶端的對象發送給服務器,以便在服務器端修改該對象並將其重新發送給客戶端。發送對象表單客戶端到服務器是正常的,但它正常工作,但是當我發送對象時它給出異常Socket被關閉。這是代碼。 IntString和ParentObj是我發送對象的類。java.net.SocketException套接字已關閉

客戶端1類:

import java.net.*; 
import java.io.*; 

public class Client1 { 
    public static void main(String args[]) { 
     int arr[] = new int[10]; 
     int length = 6, i, counter_1; 
     ParentObj obj1; 
     for (i = 0; i < length; i++) { 
      arr[i] = i + 10; 
     } 
     try { 
      Socket s = new Socket("localhost", 6789); 
      IntString obj = new IntString(arr, length); 
      /* 
      * OutputStream os = s.getOutputStream(); 
      * ObjectOutputStream oos = new ObjectOutputStream(os); 
      * 
      * oos.writeObject(obj); 
      * oos.close(); 
      * os.close(); 
      */ 
      Send_recv snd = new Send_recv(s); 
      snd.sendObj((ParentObj) obj); 

      if (s.isClosed()) { 
       System.out.println("Closed"); 
       // s.connect(null); 
      } 
      obj1 = snd.recObj(); 
      obj = (IntString) obj1; 
      if (obj != null) { 
       for (counter_1 = 0; counter_1 < obj.length_of_row; counter_1++) { 
        System.out.println(obj.row[counter_1]); 
        obj.row[counter_1]++; 
       } 
      } 

      // s.close(); 

     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 
} 

服務器類

import java.net.*; 
import java.io.*; 

public class Server1 { 
    public static void main(String args[]) { 
     IntString obj; 
     ParentObj obj1; 
     int port = 6789, counter_1; 
     try { 
      ServerSocket ss = new ServerSocket(port); 
      Socket s = ss.accept(); 
      Send_recv rev = new Send_recv(s); 
      /* 
      * InputStream is = s.getInputStream(); 
      * ObjectInputStream ois = new ObjectInputStream(is); 
      * IntString obj = (IntString)ois.readObject(); 
      */ 
      obj1 = rev.recObj(); 
      obj = (IntString) obj1; 
      System.out.println(s.getInetAddress()); 
      System.out.println(s.getLocalAddress()); 
      if (obj != null) { 
       for (counter_1 = 0; counter_1 < obj.length_of_row; counter_1++) { 
        System.out.println(obj.row[counter_1]); 
        obj.row[counter_1]++; 
       } 
      } 
      if (ss.isClosed()) { 
       System.out.println("Closed ss"); 

      } 
      if (s.isClosed()) { 
       System.out.println("Closed in Server"); 

      } 
      Send_recv snd = new Send_recv(s); 
      snd.sendObj((ParentObj) obj); 

     } catch (Exception e) { 
      System.out.println(e + "In Server"); 
     } 

    } 
} 

Send_recv類用於發送和接收對象。

import java.io.*; 
import java.net.*; 

public class Send_recv { 
    Socket s; 
    IntString obj1; 

    public Send_recv(Socket s) { 
     this.s = s; 
    } 

    public void sendObj(ParentObj obj) { 
     try { 
      OutputStream os = s.getOutputStream(); 
      ObjectOutputStream oos = new ObjectOutputStream(os); 
      oos.writeObject(obj); 
      oos.close(); 
      os.close(); 
     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

    public ParentObj recObj() { 

     try { 
      InputStream is = s.getInputStream(); 
      ObjectInputStream ois = new ObjectInputStream(is); 
      obj1 = (IntString) ois.readObject(); 
      ois.close(); 
      is.close(); 

     } catch (Exception e) { 
      System.out.println(e); 
     } 
     return (obj1); 
    } 
} 

回答

14
java.net.SocketException socket is closed 

此異常意味着關閉套接字,然後繼續嘗試使用它。

os.close(); 

然後你在這裏關閉它。關閉Socket的輸入或輸出流將關閉另一個流,並關閉Socket.

+1

謝謝您的幫助。 – user2138414 2013-03-06 08:19:53

相關問題