2012-01-31 127 views
0

我已經創建了一個與telnet協同工作的聊天服務器。現在,我正在嘗試寫我自己的客戶。我需要能夠從用戶和端口號獲取IP地址。我試圖通過ChatClient()傳遞這些變量。然而,當我編譯下面的代碼,我收到以下錯誤信息:Java聊天客戶端連接服務器

ChatClient.java:24: cannot find symbol 
symbol : variable ip 
location: class ChatClient 
      new ChatClient(ip,port); 
         ^
ChatClient.java:24: cannot find symbol 
symbol : variable port 
location: class ChatClient 
      new ChatClient(ip,port); 
         ^
2 errors 

ChatClient.java

public class ChatClient { 
    PrintWriter output; 
    BufferedReader input; 
    Socket client; 

    public ChatClient(int ip, int port) throws Exception { 
     String line; 
     input = new BufferedReader(new InputStreamReader(client.getInputStream())) ; 
     output = new PrintWriter(client.getOutputStream(),true); 
     output.println("Enter an ip address: "); 
     line = input.readLine(); 
     output.println("Enter a port number: "); 
     line = input.readLine(); 
    } 

    public static void main(String ... args) { 
     try { 
      new ChatClient(ip,port); 
     } catch(Exception ex) { 
      out.println("Error --> " + ex.getMessage()); 
     } 

    } // end of main   

} 
+0

做一個清理並重新編譯 – Adrian 2012-01-31 16:37:09

+0

在main方法的範圍內沒有ip或port變量。 – qrtt1 2012-01-31 16:37:19

回答

1

yatskevich提供了編譯錯誤的答案,但是你的代碼有其他問題。

你想讓ChatClient構造函數使用ip和port來做什麼?它當前通過Socket的OutputStream發送提示,然後通過其InputStream等待來自Socket的輸入。然後它忽略輸入。

public ChatClient() throws Exception { 
    String line; 
    int ip, port; 
    input = new BufferedReader(new InputStreamReader(client.getInputStream())) ; 
    output = new PrintWriter(client.getOutputStream(),true); 

    output.println("Enter an ip address: "); 
    line = input.readLine(); 
    if (line == null) { 
     //EOF - connection closed 
     throw new EOFException("EOF encountered before ip address input."); 
    } 
    try { 
     ip = Integer.parseInteger(line); 
    } catch (NumberFormatException nan) { 
     //Invalid input 
     // log the error and throw the exception or use a default value. 
    } 

    output.println("Enter a port number: "); 
    line = input.readLine(); 
    if (line == null) { 
     //EOF - connection closed 
     throw new EOFException("EOF encountered before port input."); 
    } 
    try { 
     port = Integer.parseInteger(line); 
    } catch (NumberFormatException nan) { 
     //Invalid input 
     // log the error and throw the exception or use a default value. 
    } 
} 

現在您已經從Socket讀取了端口和IP地址。

Socket從哪裏來?我懷疑你真正想要的是:

  1. 從命令行參數中獲取IP地址和端口。
  2. 打開一個套接字到指定的地址和端口。
  3. 用新創建的Socket實例創建一個Client實例。
2

new ChatClient(ip,port)需要聲明在你的代碼int ipint port變量之前:

public static void main(String... args) { 
    try { 
     int ip = 0; 
     int port = 8080; 
     new ChatClient(ip,port); 
    } catch(Exception ex) { 
     ... 
    } 
} 

順便說一句,如果你要讀取IP地址和por t從控制檯中,您可以從ChatClient的構造函數中刪除int ip, int port參數。

+0

現在它編譯,但它不打印到控制檯。從服務器端獲取IP地址和端口號並將其傳遞給客戶端會更好嗎? – user1179027 2012-01-31 17:06:51

+0

這是一個不同的問題,但看看這個簡短的教程:http://www.java-tips.org/java-se-tips/java.util/how-to-read-input-from-console.html – yatskevich 2012-01-31 18:23:24