2015-07-12 160 views
-2

我正在開發一個客戶端服務器程序和我的程序給我Socket對象上的一些錯誤不能由編譯器檢測,我已經重新檢查它並沒有任何拼錯錯誤或丟失分號,但它仍然告訴我error.Beside,我也與一些在線socket編程教程比較我的計劃,我發現我的方式是正確的。我很新到Java Socket編程Java Socket技術方案和符號不能找到DataInputStream類類

try 
    { 
    JOptionPane.showMessageDialog(null,"Server : Configuring the port"+port); 
    ServerSocket server = new ServerSocket(port); 
    JOptionPane.showMessageDialog(null,"Server : Waiting for client's connection");; 
    while(true) 
    { 
     Socket socket = new Socket(); 
     socket = server.accept(); 
    } 
    } 
catch(IOException e) 
    { 
    JOptionPane.showMessageDialog(null,"Server : Nothing Found"); 
    } 
    try 
    { 

     DataInputStream input = new DataInputStream(socket.getInputStream()); // get the input from client 
     DataOutputStream output = new DataOutputStream(socket.getOutputStream()); // Send the output to client 
     String word = "Connection Establish"; 
     while(true) 
     { 
      word = output.readLine(); // read the message 
      output.println(word); // display the message to client 
     } 
    } 
    catch(IOException e) 
    { 
     JOptionPane.showMessageDialog(null,"Server : Can't Connect To Server, Please Try Again"); 
    } 

} 

這是我的一些程序的部分,錯誤顯示在DataInputStream類類,因爲它不能找到符號插座
http://codepad.org/ISGe9eTx將提供完整的程序

+1

所以,你會得到一個錯誤。它是什麼?發佈完整且準確的錯誤消息。告訴我們它指的是哪一行。不讀取錯誤信息不會幫助您(和我們)解決問題。 –

+0

錯誤發生在所有DataInputStream類類給我Socket對象未找到 –

+0

錯誤發生線35/36和40/41線。 SRY不提供明確的指示 –

回答

1

您需要聲明sockettry塊之外:

Socket socket = null; 
try{ 
    ... 
    while(true) { 
    socket = server.accept(); 
    } 
} 
... 
+0

問題仍然看起來是一樣的...... –

+0

@ShinjiKagawa什麼問題會是這樣?你沒有足夠詳細地描述任何人知道它是什麼,但這個答案肯定會糾正你的代碼中的錯誤。 – EJP

+0

謝謝大家,我解決了所有的錯誤信息ady –