java - 在 java readLine() 中读取响应结束

标签 java android ios sockets bufferedreader

    try {
                    input = new BufferedReader(new InputStreamReader(
                            mSocket.getInputStream()));
                    while (!Thread.currentThread().isInterrupted()) {
                        String messageStr = null;
                        messageStr = input.readLine();
                        if (messageStr != null) {
                            updateMessages(messageStr, false);
                        } else {
                            break;
                        }
                    }
                    input.close();
                } catch (IOException e) {
                    Log.e(CLIENT_TAG, "Server loop error: ", e);
                }

我在一个线程中使用上面的代码来接收来自套接字连接的响应。
在 Android 中它工作正常,因为我使用 out.println() 发送数据,但是当设备连接到 ios 并开始接收数据时,它无法识别结束并且仅在连接时接收关闭。除了 readLine() 之外还有其他方法吗?以及如何在上面的代码中使用。

最佳答案

这样效果会更好。

try 
{
    input = new BufferedReader(new InputStreamReader(mSocket.getInputStream()));
    String messageStr = "";
    while (!Thread.currentThread().isInterrupted() && (messageStr = input.readLine()) != null) 
    {
        updateMessages(messageStr, false);
    }
    input.close();
} catch (Exception e) {
     Log.e(CLIENT_TAG, "Server loop error: ", e);
}

关于java - 在 java readLine() 中读取响应结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38829215/

相关文章:

objective-c - 如何使用单个按钮和单个操作方法显示/隐藏 UIPopoverController

java - MyClass.class.getClassLoader().getResource ("").getPath() 抛出 NullPointerException

java - [com/android/vending/billing/IInAppBillingService$Stub$Proxy.class] 中存在重复的 jar 条目

Java 抽象类类型不匹配

javascript - react native : Customizing TouchableOpacity Animation Speed

ios - 自动布局和线性方程

html - 根据复制量调整文本大小

java - 在按钮单击事件中使用无限循环

android - Firebase 远程配置总是命中服务器

Android:在服务和 Activity 之间传递数据