在套接字上设置超时时出现 NoSuchElementException

发布于 2024-11-07 06:44:57 字数 1761 浏览 4 评论 0原文

我想在客户端读取时设置超时。该例程应该抛出 InterruptedIOException,但它却在 System.out.println("echo: " + _in.nextLine()); 上抛出 NoSuchElementException 我做错了什么?

这是我的方法

public void startUserInput()
{
    try {
        _out = new PrintWriter(_echoSocket.getOutputStream(), true);
        _in  = new Scanner(new InputStreamReader(_echoSocket.getInputStream()));

        Scanner stdIn = new Scanner(new InputStreamReader(System.in));
        System.out.print("Input: ");
        while (stdIn.hasNextLine()) {
            _out.println(stdIn.nextLine());
            System.out.println("echo: " + _in.nextLine());
            System.out.print("Input: ");
        }
        stdIn.close();

    }catch (InterruptedIOException exception){
        System.err.println("The server is not responding " + _serverHostname);

    }
    catch (IOException e) {
        System.out.println("error"  + e.getLocalizedMessage());
    }}

,这是我的联系,

public boolean establishConnection()
{
    System.out.println ("Connecting to the host " +
            this.getServerHostname() + " au port " + this.getServerPort());

    try {
        _echoSocket = new Socket();
        _echoSocket = new Socket(this.getServerHostname(), this.getServerPort());
        _echoSocket.setSoTimeout(10000);
        System.out.println(_echoSocket.getOutputStream());
        return _echoSocket.isConnected();

    } catch (UnknownHostException e) {
        System.err.println("Unknown host: " + this.getServerHostname());
        return false;



    } catch (IOException e) {
        System.err.println("Error while connecting to the server : " + 
                this.getServerHostname() + ":" + this.getServerPort());
        return false;
    }
}

谢谢

I wanted to set a timeout when a client read. the routine supposed to throw an InterruptedIOException but instead it throws NoSuchElementException on System.out.println("echo: " + _in.nextLine()); what am I doing wrong ?

this is my methode

public void startUserInput()
{
    try {
        _out = new PrintWriter(_echoSocket.getOutputStream(), true);
        _in  = new Scanner(new InputStreamReader(_echoSocket.getInputStream()));

        Scanner stdIn = new Scanner(new InputStreamReader(System.in));
        System.out.print("Input: ");
        while (stdIn.hasNextLine()) {
            _out.println(stdIn.nextLine());
            System.out.println("echo: " + _in.nextLine());
            System.out.print("Input: ");
        }
        stdIn.close();

    }catch (InterruptedIOException exception){
        System.err.println("The server is not responding " + _serverHostname);

    }
    catch (IOException e) {
        System.out.println("error"  + e.getLocalizedMessage());
    }}

and this is my connection

public boolean establishConnection()
{
    System.out.println ("Connecting to the host " +
            this.getServerHostname() + " au port " + this.getServerPort());

    try {
        _echoSocket = new Socket();
        _echoSocket = new Socket(this.getServerHostname(), this.getServerPort());
        _echoSocket.setSoTimeout(10000);
        System.out.println(_echoSocket.getOutputStream());
        return _echoSocket.isConnected();

    } catch (UnknownHostException e) {
        System.err.println("Unknown host: " + this.getServerHostname());
        return false;



    } catch (IOException e) {
        System.err.println("Error while connecting to the server : " + 
                this.getServerHostname() + ":" + this.getServerPort());
        return false;
    }
}

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

胡渣熟男 2024-11-14 06:44:57

原因是当您调用 _in.nextLine() 时,没有从 Scanner 对象 _in 中读取任何行。

您在 while 循环中所做的是检查 stdIn.hasNextLine() ,但没有检查 _in 是否具有可以读取的 nextLine() 。

有关异常的详细信息,您可以查看:

http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html#nextLine()

希望有帮助:) 干杯!

The reason is that when you invoked _in.nextLine() there is no line to be read in from the from the Scanner object _in.

What you did in the while loop was to check for stdIn.hasNextLine() but you did not check if _in has a nextLine() that can be read.

For details on the exception, you can check out:

http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html#nextLine()

hope it helps :) Cheers!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文