[英]Java - socket.isConnected returns true to ANY ip address using port 80
[英]Socket AsyncTask does not log the socket.isConnected value in the code
我有以下代碼,以便在android中執行異步任務以連接到套接字。
調用asynctask是
new SocketAsyncTask().execute("abc");
導入android.os.AsyncTask; 導入android.util.Log;
導入java.io.IOException; 導入java.net.Socket;
公共類SocketAsyncTask擴展了AsyncTask {
private static String connectionURL ="MY_URL" ;
private static int port = 3000;
@Override
protected Void doInBackground(String... uri) {
try {
String sentence;
String modifiedSentence;
Socket clientSocket = new Socket(connectionURL, port);
Log.d("In socket",clientSocket.isConnected() + "");
if (clientSocket.isConnected()) {
Log.d("Client connection Successfull!!", "true");
//System.out.print("Connection Successfull!!");
} else {
//System.out.print("Connection Not Successfull!!");
Log.d("Client connection not Successfull!!", "true");
}
/*DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);*/
clientSocket.close();
}catch (IOException exp) {
}
return null;
}
}
輸出確實會打印“ In socket”字符串,但是不會記錄“ Client connection successl”或“ Client connection not successl”。 我嘗試調試它,但是它基本上做了一些奇怪的事情。 它在logcat中打印“ In socket”,但似乎沒有在該斷點處停止。
首先,記錄isConnected()
沒有意義。 如果構造函數未引發異常,則說明套接字已連接。 在測試時, isConnected()
測試不可能為假。
這里的問題是您忽略了應該記錄的異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.