缓冲读卡器没有从套接字接收数据 [英] buffered reader not receiving data from socket

查看:123
本文介绍了缓冲读卡器没有从套接字接收数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个客户端应用程序,它将通过tcp / ip接收连续的数据流。我遇到的问题是缓冲的读取器对象没有接收到任何数据并且挂在readline方法上。

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.

服务器的工作方式是连接到它,然后发送身份验证信息以接收数据。我的代码的要点在下面

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
}

我创建了一个示例服务器应用程序,它以我认为服务器发送的方式发送数据数据和它连接,接收和处理数据。我可以在我的应用程序中正常连接到服务器。我也可以telnet到服务器并写入身份验证字符串并使用telnet接收大量数据。但是,我的应用程序只是挂在readLine与服务器,我不明白为什么。

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 atleast)看起来像是以下的连续流:

The data coming in (through telnet atleast) looks like a continuous stream of the following:

 data;data;data;data;data
 data;data;data;data;data

为什么我的应用程序挂在readline上,我是不是正确输出了身份验证行?我没有收到任何错误...

Why is my app hanging at readline, am I not outputting the authentication line correctly? 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();
}


推荐答案

首先你应该打电话给 BufferedReader.ready()在调用 readLine()之前,作为 ready()会告诉你是否可以阅读。

Firstly you should call BufferedReader.ready() before calling readLine(), as the ready() will tell you if it's ok to read.

PrintWriter 不会抛出I / O异常如果没有你的知识,写作可能会失败,这就是没有什么可读的原因。使用 PrintWriter.checkError()查看写入过程中是否有任何错误。

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.

你应该设置在您向管道写下任何内容之前, Socket 上的输入和输出流。如果您的读者在另一端尝试写入时没有准备好,您将在服务器中获得一个损坏的管道,它将不再发送任何数据。在您编写或阅读任何内容之前,Telnet会设置读写。

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.

您可以使用 Wireshark 告诉服务器是否实际发送数据。

You can make use of Wireshark to tell if the server is actually sending data.

这篇关于缓冲读卡器没有从套接字接收数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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