简体   繁体   English

从socket bufferreader永远读取android

[英]Read from socket bufferreader forever android

I am trying to make a simple chat server/client application. 我正在尝试制作一个简单的聊天服务器/客户端应用程序。 My server is made in python, using twisted. 我的服务器是用python制作的,使用twisted。 Everything works good with it, tested it with telnet. 一切都很好用,用telnet测试。

Basically the server sends to all it's clients every message it's getting. 基本上,服务器会向其所有客户端发送它收到的每条消息。 I need a way to read forever from the socket, so that I can get all messages. 我需要一种永远从套接字读取的方法,以便我可以获取所有消息。 I don't think it's the best approach, but I tried to make it using a thread, that reads forever (for now). 我不认为这是最好的方法,但我试图使用一个线程,它永远读取(现在)。 After the client is connected to the server, it will start a thread, that will check forever for updates. 客户端连接到服务器后,它将启动一个线程,该线程将永远检查更新。

Here is my CheckUpdates class that implements Runnable: 这是我的CheckUpdates类 ,它实现了Runnable:

class CheckUpdates implements  Runnable{
    @Override
    public void run() {
       Log.d("ME", "CHECKING FOR UPDATES");
        while(true)
        {
            // reader.lines().forEach(System.out.println);
            String line;
            Log.d("ME","IN WHILE");
            try {
                while ((line = reader.readLine()) != null) {
                    Log.d("ME", "READING LINE");
                    Looper.prepare();
                    Toast.makeText(MainActivity.this, line, Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }
            } catch (IOException e) {
                Log.d("ME", "EXCEPTION IN WHILE: " + e.getMessage().toString());
            }
            Log.d("ME", "END OF WHILE CHECK UPDATES");
        }
    }
}

I am starting the thread right after I'm connected to the server. 我在连接到服务器后立即启动线程。 Basically, I am getting the first message (first toast), and right after that, nothing more comes in, not even the END OF WHILE CHECK UPDATES logcat message. 基本上,我正在收到第一条消息(第一个吐司),在此之后,没有更多内容,甚至是END OF WHILE CHECK UPDATES logcat消息。

Could it get stuck forever in the while that checks for a new line from socket ? 它是否会在从套接字检查新行的同时永远陷入困境? Also, if there is a more elegant way of doing this, would really appreciate if somebody could point me in the right direction. 此外,如果有一种更优雅的方式,如果有人能指出我正确的方向,真的很感激。

Could it get stuck forever in the while that checks for a new line from socket ? 它是否会在从套接字检查新行的同时永远陷入困境?

No, but it could get stuck forever in the outer while (true) loop, which is completely pointless. 不,但它可能永远陷入外部while (true)循环,这是完全没有意义的。 Remove it. 去掉它。 At the moment you're looping forever at end of stream. 目前你正在循环结束时永远循环。

Could it get stuck forever in the while that checks for a new line from socket? 它是否会在从套接字检查新行的同时永远陷入困境?

Yes, if there's no more data. 是的,如果没有更多的数据。 But I suspect that's not the problem. 但我怀疑这不是问题所在。 I suspect the problem is this: 我怀疑问题是这样的:

Looper.loop();

As far as I can see, you don't need to use Looper at all here. 据我所知,你根本不需要使用Looper Assuming you're really on a dedicated thread, what messages do you expect to process? 假设您真的在专用线程上,您希望处理哪些消息? With nothing calling Looper.quit() , the Looper.loop() call will block forever, I suspect. 没有调用Looper.quit() ,我怀疑Looper.loop()调用会永远阻塞。

Just get rid of the two Looper calls and I suspect you'll be able to see all the messages. 只是摆脱两个Looper电话,我怀疑你将能够看到所有的消息。

EDIT: However, you'll need to show the toast on the UI thread - see this SO question for details of that, using Activity.runOnUiThread . 编辑:但是,您需要在UI线程上显示吐司 - 使用Activity.runOnUiThread查看此SO问题以获取详细信息。 I'm not sure that showing a toast on every message is an ideal UI, mind you... 我不确定在每条消息上祝酒是一个理想的用户界面,请注意......

You'll also want to get rid of the while (true) - you're already looping on the inner loop, so that's fine. 你也想摆脱while (true) - 你已经在内循环上循环,所以没关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM