简体   繁体   English

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

[英]Android Socket Connection with AIS Transponder throws IOException

I am trying to read the AIS serial data on an Android device over WiFi by creating a TCP connection with the AIS Transponder. 我正在尝试通过与AIS发送应答器创建TCP连接来通过WiFi在Android设备上读取AIS串行数据。 I am using the EM-Trak B360 AIS transponder and creating a socket on the android device using the following code: 我正在使用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);
  }

The above code works for mobile-to-mobile connection by creating a mobile hotspot. 上面的代码通过创建移动热点来实现移动到移动的连接。 However when connecting the AIS Transponder's Wifi network it throws the I/O Exception: Software Caused Connection Abort. 但是,在连接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)

I got the solution to the problem. 我找到了解决问题的办法。 I used telnetclient library from Apache Commons. 我使用了来自Apache Commons的telnetclient库。 https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/telnet/TelnetClient.html https://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/net/telnet/TelnetClient.html

Now i'm able to communicate with AIS transponder. 现在,我可以与AIS转发器进行通信了。

Thanks!! 谢谢!!

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

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