Java 线程似乎无法正常运行

发布于 2024-12-04 16:06:04 字数 1582 浏览 1 评论 0原文

这是我第一次在堆栈溢出中问问题,所以请耐心等待。 我会尽量保持简短。

我正在编写一个简单的客户端套接字 Android 应用程序。 我正在连接的服务器有一个名为 Alice 的聊天机器人。 我已成功连接到服务器,并收到消息“Hello from alice”,但随后线程似乎停止了,因为我没有收到更多消息。

这是一些代码:

    @Override
public void run() {

    try 
    {
        while (true)
        {
        String _input = _rd.readLine();
        if (_input != null)
        {
        _field.append("Alice : "+ _input+"\n");
        }
        else
        {
            _field.append("null");
        }
            sleep(50);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
     catch (InterruptedException e) {
        e.printStackTrace();
    }
}

正如您所看到的,即使我没有收到消息,我也尝试通过附加“null”来对我的代码进行故障排除,但这两者都没有显示在我的 EditText 中。

当用户按下按钮时,我从主 Activity 类中调用 .start()

        @Override
        public void onClick(View arg0) {

            setContentView(R.layout.second);
            InitiateSecondFrame();  

            _cs = new ClientSocket();
            String _check = _cs.EstablishConnection(_host.getText().toString(),
                    Integer.parseInt(_port.getText().toString()),
                    (EditText)findViewById(R.id.historyField));

            _cs.start();

            Toast.makeText(getApplicationContext(), _check, 100).show();
            SaveHostPort(_host.getText().toString(), _port.getText().toString());                           
        }
    }); 

ClientSocket 类由 Thread 扩展并实现 Runnable,我也尝试过扩展或实现。 我根本没有收到任何错误消息。 我希望我提供的信息足以让您意识到问题所在。 请记住,我是多线程和套接字方面的新手,所以我可能错过了一些基本的东西。

This is the first time i'm asking a question here at stack overflow so bear with me.
I'll try to keep this brief.

I'm writing a simple client socket android application.
The server I'm connecting to has a chat robot named Alice.
I have managed to connect to the server and I receive the message "Hello from alice" but then the thread seems to stop because I'm not receiving any more messages.

Here is some code:

    @Override
public void run() {

    try 
    {
        while (true)
        {
        String _input = _rd.readLine();
        if (_input != null)
        {
        _field.append("Alice : "+ _input+"\n");
        }
        else
        {
            _field.append("null");
        }
            sleep(50);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
     catch (InterruptedException e) {
        e.printStackTrace();
    }
}

As you can see I have attempted to troubleshoot my code by appending "null" even if I don't receive a message, however this is neither shown in my EditText.

I call .start() from my main Activity class when the user presses a button.

        @Override
        public void onClick(View arg0) {

            setContentView(R.layout.second);
            InitiateSecondFrame();  

            _cs = new ClientSocket();
            String _check = _cs.EstablishConnection(_host.getText().toString(),
                    Integer.parseInt(_port.getText().toString()),
                    (EditText)findViewById(R.id.historyField));

            _cs.start();

            Toast.makeText(getApplicationContext(), _check, 100).show();
            SaveHostPort(_host.getText().toString(), _port.getText().toString());                           
        }
    }); 

The ClientSocket class is extended by Thread and implements Runnable, I've also tried just extending or implementing.
I do not get any error message at all.
I hope the information I have given is enough for you to realize what's wrong.
Remember I'm new at multithreading and sockets so I might have missed something fundamental.

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

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

发布评论

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

评论(3

绮筵 2024-12-11 16:06:04

如果有帮助,请按照以下方式启动线程:

Runnable runnable = new MyRunnableClass(); // has the run() method
new Thread(runnable).start();

If it helps, here's how you start a thread:

Runnable runnable = new MyRunnableClass(); // has the run() method
new Thread(runnable).start();
两人的回忆 2024-12-11 16:06:04
_rd.readLine();

是你的问题吗?它将永远阻塞并等待来自远程系统的输入,并且只有当它检测到来自远程系统的换行符时才会返回接收到的内容。

_rd.readLine();

is your problem here. It will block and wait forever for input from the remote system, and will only return the content received when it detects a newline character from the remote system.

爱的十字路口 2024-12-11 16:06:04

线程没有停止。它只是在等待消息的到来。 _rd.readLine() 将阻塞,直到读取某行(以换行符结尾)或直到连接结束。因此,如果 Alice 保持连接打开并且不发送任何内容,则 readLine 将永远阻塞,并且您的“空”消息将永远不会被打印。

The thread is not stopped. It's just waiting for a message to come. _rd.readLine() will block until some line (ending with a newline char) is read, or until the connection ends. So If Alice keeps the connection open and doesn't send anything, readLine will block forever, and your "null" messages won't ever be printed.

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