为什么readLine()在InputStreamReader(Java)中导致未定义的错误?

问题描述 投票:0回答:1

我正在用Java创建一个简单的IRC客户端。尝试使用包裹在BufferedReader中的InputStreamReader从服务器读取数据流时遇到问题。我发现所有示例都使用readLine()方法捕获数据。但是,使用这种方法时,出现以下错误:

readLine()对于InputStreamReader类型未定义

错误首先出现在我的while循环中。这是代码:

    //Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) {

    //Establish a connection to the host
    try
    {
        socket = new Socket(address, port);

        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream()));

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
    }
    catch(UnknownHostException u)
    {
        System.out.println(u);
    }
    catch(IOException i)
    {
        System.out.println(i);
    }

    //Login to the host
    try
    {
        writer.write("NICK " + nick + "\r\n");
        writer.write("USER " + login + "\r\n");
        writer.flush();
    }
    catch(IOException i)
    {
        System.out.println(i);
    }

    //Read lines from the server to make sure we are connected
    String line = null;
    try
    {
        while ((line = reader.readLine()) != null) {

            if (line.indexOf("004") >= 0) {
                // We are logged in
                break;
            } else if (line.indexOf("433") >= 0) {
                System.out.println("Nickname is already in use");
                return;
            }
        }
    } catch (IOException i) {
        System.out.println(i);
    }

    // Join the specified channel
    writer.write("JOIN " + channel + "\r\n");
    writer.flush();

    // Keep reading lines from the host.
    while ((line = reader.read()) != null) {
        if (line.toLowerCase().startsWith("PING ")) {
            //Respond with PONG tro avoid being disconnected
            writer.write("PONG " + line.substring(5) + "\r\n");
            writer.flush();
        }
        else {
            //Print the line received
            System.out.println(line);
        }
    } //end while
}
java inputstream
1个回答
0
投票

这里的问题是,变量readerwriter在while循环中不可见,而仅在try-catch-block中声明了它们。

如果您像这样更改代码,则至少应该编译(因为我没有服务器实现,所以未测试ID:]

//Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) throws IOException {//EDITED HERE added throws declaration (either do this, or catch the exceptions)


//Client constructor
public IRCClient(String address, int port, String nick, String login, String channel) throws IOException {//EDITED HERE added throws declaration (either do this, or catch the exceptions)

    //Establish a connection to the host
    //EDITED HERE move the declaration here to have the variables visible
    BufferedWriter writer = null;
    BufferedReader reader = null;
    try {
        socket = new Socket(address, port);

        writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

        reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    }
    catch (UnknownHostException u) {
        System.out.println(u);
    }
    catch (IOException i) {
        System.out.println(i);
    }

    //Login to the host
    try {
        writer.write("NICK " + nick + "\r\n");
        writer.write("USER " + login + "\r\n");
        writer.flush();
    }
    catch (IOException i) {
        System.out.println(i);
    }

    //Read lines from the server to make sure we are connected
    String line = null;
    try {
        while ((line = reader.readLine()) != null) {

            if (line.indexOf("004") >= 0) {
                // We are logged in
                break;
            }
            else if (line.indexOf("433") >= 0) {
                System.out.println("Nickname is already in use");
                return;
            }
        }
    }
    catch (IOException i) {
        System.out.println(i);
    }

    // Join the specified channel
    writer.write("JOIN " + channel + "\r\n");
    writer.flush();

    // Keep reading lines from the host.
    while ((line = reader.readLine()) != null) {//EDITED HERE replaced read() with readLine()
        if (line.toLowerCase().startsWith("PING ")) {
            //Respond with PONG tro avoid being disconnected
            writer.write("PONG " + line.substring(5) + "\r\n");
            writer.flush();
        }
        else {
            //Print the line received
            System.out.println(line);
        }
    } //end while
}

我用注释//EDITED HERE标记了更改的部分

© www.soinside.com 2019 - 2024. All rights reserved.