我有一个应用程序通过端口22与硬件进行通信。该应用程序一直工作到本月。现在,每当应用程序尝试连接到硬件时,我都会收到套接字通信错误,程序最终崩溃,因为套接字无法连接。
W/System.err: java.net.SocketException: Software caused connection abort
我能想到的唯一改变的是我在手机上运行了最新的Android更新。硬件的代码与我过去几个月一直在测试的代码相同,并且应用程序的代码自3月底以来已经完成。最新更新中是否有更改可能导致套接字异常?
套接字通信由服务管理。设置它的代码是:
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
if (CheckConnectionStatus()){
ConnectSocket();
Log.i(TAG, "Connected To ATP");
} else {
Log.i(TAG, "Not Connected To ATP");
}
return START_STICKY;
}
/*******************************************************************************
*
* Check if phone is connected to ATP
*/
public Boolean CheckConnectionStatus(){
//if connected to proper network, change indicators
String ssid = "";
WifiManager wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if(wifiInfo.getSupplicantState() == SupplicantState.COMPLETED){
ssid = wifiInfo.getSSID();
}
String networkSSID = "\"ATP45\"";
return ssid.equals(networkSSID);
}
/***********************************************************************
* Setup the socket connection
*/
public void ConnectSocket(){
Log.i(TAG, "Running ConnectSocket");
socket = new Socket();
Runnable connectSocket = new SetUpSocketConnection();
communicationThread = new CommunicationThread();
new Thread(connectSocket).start();
}
/**********************************************************************
* Thread to open socket communication
*/
private class SetUpSocketConnection implements Runnable{
@Override
public void run(){
try{
//ERROR IS HERE:
socket.connect(new InetSocketAddress(IP_ADDRESS, PORT));
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
socket.setSoTimeout(800);
inputStream = socket.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream), 1000);
Log.d(TAG, "Connected to Socket");
communicationThreadRunning = true;
new Thread(communicationThread).start();
} catch (IOException e){
e.printStackTrace();
}
}
}
编辑:导致错误的行是首次连接套接字的地方:
socket.connect(new InetSocketAddress(IP_ADDRESS, PORT));
编辑2:完整堆栈跟踪
W/System.err: java.net.SocketException: Software caused connection abort
W/System.err: at java.net.PlainSocketImpl.socketConnect(Native Method)
W/System.err: at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:334)
W/System.err: at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:196)
W/System.err: at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
W/System.err: at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)
W/System.err: at java.net.Socket.connect(Socket.java:586)
W/System.err: at java.net.Socket.connect(Socket.java:535)