下面的代码挂起不会超出下面的消息,而是挂在socket.accept()上。 它是android应用程序的一部分,并且已将INTERNET权限添加到清单中。尝试将accept放入while(true)循环中...但是那没用:
09-23 15:55:43.737 24079-25436:尝试打开端口5556 ... 09-23 15:55:43.739 24079-25436:>>>开放端口:5556
private Socket socket = null;
private ServerSocket server = null;
private static final String TAG = "MyActivity";
public boolean openPort(String host , int port){
try {
server = new ServerSocket(port);
} catch (IOException e) {
Log.e(TAG , "Error initializing serverSocket: " , e);
}
Log.e(TAG , ">>> Opening Port : " + port);
boolean bmsg = false;
try {
socket = server.accept();
Log.e(TAG , ">>> Opened Port : " + port);
return true;
} catch(IOException e){
Log.e(TAG, "Error opening port: ", e);
return bmsg;
} catch(SecurityException s){
Log.e(TAG, "sError opening port: ", s);
return bmsg;
} catch(IllegalBlockingModeException b){
Log.e(TAG, "bError opening port: ", b);
return bmsg;
}
}
在单击按钮时从异步任务下面调用上述类:
private class openPort extends AsyncTask{
@Override
protected Object doInBackground(Object[] objects) {
Log.i(TAG , "Trying to open port 5556...");
//host is localhost
//port is 5556
OpenClosePort ocp = new OpenClosePort();
ocp.openPort(host , port);
return null;
}
}