2012-01-31 111 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