具有AIS发送应答器的Android套接字连接抛出IOException

时间:2018-08-20 15:10:20

标签: java android sockets ais

我正在尝试通过与AIS发送应答器建立TCP连接来通过WiFi在Android设备上读取AIS串行数据。我正在使用EM-Trak B360 AIS转发器,并使用以下代码在android设备上创建套接字:

public class Client extends AsyncTask<Void, Void, Void> {

  String dstAddress;
  int dstPort;
  String response = "";
  TextView textResponse;

  Client(String addr, int port, TextView textResponse) {
    dstAddress = addr;
    dstPort = port;
    this.textResponse = textResponse;
  }

  @Override
  protected Void doInBackground(Void... arg0) {

    Socket socket = null;

    try {
      socket = new Socket(dstAddress, dstPort);

      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024);
      byte[] buffer = new byte[1024];

      int bytesRead;
      InputStream inputStream = socket.getInputStream();

      /*
       * notice: inputStream.read() will block if no data return
       */
      while ((bytesRead = inputStream.read(buffer)) != -1) {
        byteArrayOutputStream.write(buffer, 0, bytesRead);
        response += byteArrayOutputStream.toString("UTF-8");
      }

    } catch (UnknownHostException e) {
      e.printStackTrace();
      response = "UnknownHostException: " + e.toString();
    } catch (IOException e) {
      e.printStackTrace();
      response = "IOException: " + e.toString();
    } finally {
      if (socket != null) {
        try {
          socket.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return response;
  }

  @Override
  protected void onPostExecute(Void result) {
    textResponse.setText(response);
    super.onPostExecute(result);
  }

上面的代码通过创建移动热点来实现移动到移动的连接。但是,当连接AIS发送应答器的Wifi网络时,会引发I / O异常:软件导致连接中断。

08-20 17:04:39.089 13505-13534/com.hfad.slave W/System.err: java.net.SocketException: Software caused connection abort
08-20 17:04:39.090 13505-13534/com.hfad.slave W/System.err:
  at java.net.PlainSocketImpl.socketConnect(Native Method)
  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
  at java.net.Socket.connect(Socket.java:586)
  at java.net.Socket.connect(Socket.java:535)
  at java.net.Socket.<init>(Socket.java:427)
  at java.net.Socket.<init>(Socket.java:210)
  at com.hfad.slave.Client.doInBackground(Client.java:47)
  at com.hfad.slave.Client.doInBackground(Client.java:16)
  at android.os.AsyncTask$2.call(AsyncTask.java:304)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
  at java.lang.Thread.run(Thread.java:762)

1 个答案:

答案 0 :(得分:0)

我找到了解决问题的办法。我使用了来自Apache Commons的telnetclient库。 https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/telnet/TelnetClient.html

现在我可以与AIS转发器进行通信了。

谢谢!!