java 缓冲读取器未从套接字接收数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6359896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
buffered reader not receiving data from socket
提问by Ryan
I am writing a client application that will receive a continuous flow of data through tcp/ip. The problem I'm having is that the buffered reader object isn't receiving any data and is hanging at the readline method.
我正在编写一个客户端应用程序,它将通过 tcp/ip 接收连续的数据流。我遇到的问题是缓冲的读取器对象没有接收任何数据,而是挂在 readline 方法上。
The way the server works is that you connect to it, and then send authentication information in order to receive data. The gist of my code is below
服务器的工作方式是您连接到它,然后发送身份验证信息以接收数据。我的代码的要点如下
socket = new Socket(strHost, port);
authenticate();
inStream = new BufferedReader(new InputStreamReader(socket.getInputStream()));
process(inStream);
authenticate()
{
PrintWriter pwriter = new PrintWriter(socket.getOutputStream(), true);
pwriter.println(authString);
}
process(BufferedReader bufferedReader)
{
while((line = bufferedReader.readLine()) != null)
dostuff
}
I created a sample server application that sends data the way (I think) the server is sending data and it connects, and receives and processes the data fine. I can connect to the server fine in my application. I can also telnet to the server and write the authentication string and receive a flood of data using telnet. However my application just hangs at readLine with the server and I'm out of idea's why.
我创建了一个示例服务器应用程序,它以(我认为)服务器发送数据的方式发送数据,它连接、接收和处理数据。我可以在我的应用程序中正常连接到服务器。我还可以 telnet 到服务器并写入身份验证字符串并使用 telnet 接收大量数据。然而,我的应用程序只是挂在服务器的 readLine 上,我不知道为什么。
The data coming in (through telnet atleast) looks like a continuous stream of the following:
传入的数据(至少通过 telnet)看起来像以下连续流:
data;data;data;data;data
data;data;data;data;data
Why is my app hanging at readline, am I not outputting the authentication line correctly? I'm not receiving any errors...
为什么我的应用程序挂在 readline 上,我是否没有正确输出身份验证行?我没有收到任何错误...
EDITMy sample server code (which is working correctly)...again this is only mimicking the way I thinkthe real server is running but I can connect to both in my application just not receive data from the real server.
编辑我的示例服务器代码(工作正常)...同样,这只是模仿我认为真实服务器运行的方式,但我可以在我的应用程序中连接到两者,只是不接收来自真实服务器的数据。
public static void main(String[] args) throws IOException
{
ServerSocket serverSocket = null;
try
{
serverSocket = new ServerSocket(1987);
}
catch (IOException e)
{
System.out.println("Couldn't listen on port: 1987");
System.exit(-1);
}
Socket clientSocket = null;
try
{
clientSocket = serverSocket.accept();
}
catch (IOException e) {
System.out.println("Accept failed: 1987");
System.exit(-1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String something;
while ((something = in.readLine()) != null)
{
while(true)
{
out.println(message);
}
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
回答by David Newcomb
Firstly you should call BufferedReader.ready()
before calling readLine()
, as the ready()
will tell you if it's ok to read.
首先你应该在打电话BufferedReader.ready()
之前先打电话readLine()
,因为它ready()
会告诉你是否可以阅读。
PrintWriter
doesn't throw I/O Exception so the write may have failed without your knowledge which is why there is nothing to read. Use PrintWriter.checkError()
to see if anything as gone wrong during the write.
PrintWriter
不会抛出 I/O 异常,因此写入可能在您不知情的情况下失败,这就是为什么没有任何内容可读。使用PrintWriter.checkError()
是否有任何与写时出了错。
You ought to set up the input and output streams on the Socket
at the same time before you write anything down the pipe. If your reader is not ready when the other end tries to write you will get a broken pipe in the server and it won't send any more data. Telnet sets up read and write before you have written or read anything.
Socket
在将任何内容写入管道之前,您应该同时设置输入和输出流。如果您的阅读器在另一端尝试写入时尚未准备好,您将在服务器中发现一个损坏的管道,并且它不会再发送任何数据。在您写入或读取任何内容之前,Telnet 设置读取和写入。
You can make use of Wiresharkto tell if the server is actually sending data.
您可以使用Wireshark来判断服务器是否真的在发送数据。
回答by AlexR
BufferdReader.readLine()
reads lines, i.e. sequences of characters ended with \r
or \r\n
. I guess that your server writes its output into one single line. Your telnet output proves this assumption. Just use PrintWriter.println()
at server side.
BufferdReader.readLine()
读取行,即以\r
或结尾的字符序列\r\n
。我猜您的服务器将其输出写入一行。您的 telnet 输出证明了这一假设。只需PrintWriter.println()
在服务器端使用。
回答by Bahgat Mashaly
this work with me with socket without flush
这与我一起使用没有冲洗的插座
void start_listen()
{
String result1="";
char[] incoming = new char[1024];
while (!s.isClosed())
{
try {
int lenght = input.read(incoming);
result1 = String.copyValueOf(incoming,0,lenght);
}
catch (IOException e)
{
e.printStackTrace();
}
Log.d("ddddddddddd",result1);
}