2013-01-08 12 views
0

符號我寫簡單的客戶端 - 服務器應用程序,但我有這個(它簡化例子(一切都很好,當我不使用Java序列化))愚蠢的問題:找不到OIS

ServerSocket serversocket=null; 
    Socket socket=null;  
    String slowo=null; 

    try { 
     serversocket=new ServerSocket(8877); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     socket=serversocket.accept(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    try { 
     ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    slowo=(String)ois.readObject(); 

我編譯器顯示:

Serwer.java:51: cannot find symbol 
symbol : variable ois 
location: class Serwer 
slowo=(String)ois.readObject(); 
        ^
1 error 

任何人都可以幫忙嗎?

我還有一個問題。爲什麼這個程序不發送消息?

Serwer.java:

公共類Serwer {

public static void main(String[] args) { 
    ServerSocket serversocket=null; 
    Socket socket=null; 
    InputStream we=null; 
    OutputStream wy=null; 
    BufferedReader odczyt=null; 
    BufferedReader odczytWe=null; 
    DataOutputStream zapis=null; 
    String slowo=null; 
    String tekst=null; 

    ObjectInputStream ois=null; 
    ObjectOutputStream oos=null; 

    try { 
     serversocket=new ServerSocket(8877); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     socket=serversocket.accept(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     ois = new ObjectInputStream(socket.getInputStream()); 
     } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    try { 
     oos=new ObjectOutputStream(socket.getOutputStream()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    //slowo=(String)ois.readObject(); 

    while(true) { 
     try { 
      slowo=(String) ois.readObject(); 
     } catch (ClassNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 


     if(slowo==null || slowo.equals("end")) { 
      try { 
       socket.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
      System.exit(0); 
     } 
     else if(slowo!=null) { 
      System.out.println(slowo); 
     } 

      odczyt=new BufferedReader(new InputStreamReader(System.in)); 
      try { 
       tekst=odczyt.readLine(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      try { 
       oos.writeObject(tekst); 
       oos.flush(); 
      } catch (IOException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      } 

} 

}

Klient.java:

public class Klient { 
public static void main(String[] args) { 

Socket socket=null; 
InputStream we=null; 
OutputStream wy=null; 
BufferedReader odczyt=null; 
BufferedReader odczytWe=null; 
DataOutputStream zapis=null; 
String slowo=null; 
String tekst=null; 
ObjectInputStream ois=null; 
ObjectOutputStream oos=null; 

try { 
    socket=new Socket("localhost", 8877); 
} catch (UnknownHostException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
try { 
    ois=new ObjectInputStream(socket.getInputStream()); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
try { 
    oos=new ObjectOutputStream(socket.getOutputStream()); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

while(true) { 
    try { 
     slowo=(String) ois.readObject(); 
    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    if(slowo==null || slowo.equals("end")) { 
     try { 
      socket.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     System.exit(0); 
    } 
    else if(slowo!=null) { 
     System.out.println(slowo); 
    } 

     odczyt=new BufferedReader(new InputStreamReader(System.in)); 
     try { 
      tekst=odczyt.readLine(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     try { 
      oos.writeObject(tekst); 
      oos.flush(); 
     } catch (IOException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 

} 

} 

}

回答

6

這是超出範圍由時間喲你得到第51行,因爲你在之前的嘗試中聲明瞭它。

將聲明移到兩者之外,或以不同方式編寫代碼。

我認爲這種風格混亂,難以閱讀。我會這樣寫:

ServerSocket serversocket=null; 
String slowo=""; 
try { 
    serversocket=new ServerSocket(8877); 
    Socket socket = serversocket.accept(); 
    ObjectInputStream ois = new ObjectInputStream(socket.getInputStream()); 
    ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream()); 
    slowo=(String)ois.readObject(); 
} catch (Exception e) { 
    e.printStackTrace(); 
} finally { 
    close(serversocket); 
} 

不要讓一個糟糕的IDE爲你寫壞代碼。

你應該在finally塊中關閉你的套接字。

+0

+1僅用於IDE代碼撰寫評論 –