Java BufferedReader读取表单套接字不返回null [英] Java BufferedReader reading form socket doesn't return null

查看:116
本文介绍了Java BufferedReader读取表单套接字不返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在用Java编写一个应用程序,该应用程序将打开一个套接字,并应通过该套接字收发数据。

I'm currently writing an app in Java that opens a socket and should receive and send data over it.

据我了解,一旦缓冲区为空,BufferedReader.readLine()返回null。但是,我的代码并没有退出从BufferedReader读取行的循环。
的想法是,我收到一首歌曲列表,然后将值发送到开始播放歌曲的服务器。

It is my understanding that BufferedReader.readLine() returns null once the buffer is empty. However, my code doesn't exit from the loop that reads the lines from the BufferedReader. The idea is thata i receive a list of songs and then send a value to the server where a song starts playing.

这是代码:

package me.frankvanbever.MediaServerClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

        public class MediaServerClient {

            /**
             * @param args
             */
            public static void main(String[] args) {

                    Socket server;
                    try {
                        server = new Socket( "10.42.0.41" , 2626 );

                        InputStream in = server.getInputStream();
                        OutputStream out = server.getOutputStream();

                        BufferedReader bin = new BufferedReader( new InputStreamReader( in ) , 4096);

                        String inputline;
                        while( (inputline = bin.readLine()) != null){

                            System.out.println(inputline);

                        }

                        System.out.println("exited loop");
                        out.write('1');
                    } catch (UnknownHostException e) {
                    } catch (IOException e) {
                    }


            }

    }

这是正常行为吗?服务器是用python编写的,我可以更改源代码。

Is this normal behaviour? The server is written in python and I can change the source code.

推荐答案


这是我的理解一旦缓冲区为空,则BufferedReader.readLine()返回null。

It is my understanding that BufferedReader.readLine() returns null once the buffer is empty.

否。一旦基本收益被关闭,它将返回。如果您要连接的服务器没有关闭连接,则 BufferedReader 只会挂起,等待下一行文本。

No. It will return null once the underlying return has been closed. If the server you're connecting to doesn't close the connection, BufferedReader will just hang, waiting for the next line of text.

别忘了TCP是面向流的( BufferedReader 也是如此)。除非您将其放入协议中,否则不会显示完整的消息。这就是协议通常包含消息终止符之类的原因,或者它们在发送消息之前指定消息中有多少数据。

Don't forget that TCP is stream-oriented (as is BufferedReader). There's no indication of "a complete message" unless you put it in your protocol. That's why protocols often include things like message terminators, or they specify how much data is in a message before sending it.

这篇关于Java BufferedReader读取表单套接字不返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆