简体   繁体   English

缓冲的读取器未从套接字接收数据

[英]buffered reader not receiving data from socket

I am writing a client application that will receive a continuous flow of data through tcp/ip. 我正在编写一个客户端应用程序,它将通过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. 我遇到的问题是缓冲的读取器对象未接收到任何数据,并且挂在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. 我还可以通过telnet远程登录服务器并写入身份验证字符串,并接收大量数据。 However my application just hangs at readLine with the server and I'm out of idea's why. 但是我的应用程序只是挂在服务器的readLine上,我不知道为什么。

The data coming in (through telnet atleast) looks like a continuous stream of the following: (通过telnet atleast传入的)数据看起来像是以下内容的连续流:

 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? 为什么我的应用程序挂在readline上,我没有正确输出验证行? I'm not receiving any errors... 我没有收到任何错误...

EDIT My sample server code (which is working correctly)...again this is only mimicking the way I think the 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();
}

Firstly you should call BufferedReader.ready() before calling readLine() , as the ready() will tell you if it's ok to read. 首先,您应该在调用readLine() BufferedReader.ready()之前调用BufferedReader.ready() ,因为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. PrintWriter不会引发I / O异常,因此在您不知情的情况下写入可能会失败,这就是为什么没有要读取的内容。 Use PrintWriter.checkError() to see if anything as gone wrong during the write. 使用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. 在将任何内容写到管道之前,您应该同时在Socket上设置输入和输出流。 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. Telnet会在您写入或读取任何内容之前设置读写权限。

You can make use of Wireshark to tell if the server is actually sending data. 您可以使用Wireshark来判断服务器是否正在实际发送数据。

BufferdReader.readLine() reads lines, ie sequences of characters ended with \\r or \\r\\n . BufferdReader.readLine()读取行,即以\\r\\r\\n结尾的字符序列。 I guess that your server writes its output into one single line. 我猜您的服务器将其输出写入一行。 Your telnet output proves this assumption. 您的telnet输出证明了这一假设。 Just use PrintWriter.println() at server side. 只需在服务器端使用PrintWriter.println()

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);

       }

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

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