【问题标题】:Asyncrhonous socket client异步套接字客户端
【发布时间】:2012-06-04 22:44:22
【问题描述】:

我正在开发一个基于线程的异步套接字客户端。当程序调用readLine()时,它会无限期地阻塞并且永远不会返回。

public class ADNClient {

  Socket socket = null;
  DataOutputStream dataOutputStream = null;
  DataInputStream dataInputStream = null;

  Thread listener = new Thread(new Runnable() {
    @Override
    public void run() {
      String line;
      try {
        // Stop here and doesn't progress
        while ((line = dataInputStream.readLine()) != null) {
          //DO something
        }
      } 
      catch (IOException e) {}
    });

    public ADNClient() {
    try {
      socket = new Socket("192.168.1.5", 5000);
      dataOutputStream = new DataOutputStream(socket.getOutputStream());
      dataInputStream = new DataInputStream(socket.getInputStream());
      listener.start();
      //sender.start();
    } catch (Exception e) {
      Log.e("ADN", e.getMessage());
    }
  }

  public void close() {
    listener.stop();
    try {
      socket.close();
    } catch (IOException e) {
      Log.e("ADN", e.getMessage());
    }
  }
}

【问题讨论】:

  • 您向客户端发送什么数据?它是否以换行符终止?

标签: java android sockets asynchronous


【解决方案1】:

好的...我是输入/输出流的菜鸟...我没有发送换行符,接收信息的正确方法是使用

readUTF()

而不是

readLine() 

谢谢格雷格·科普夫!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多