我想检查某个 IP 端口是否在 Java 中为 android 应用程序打开。
这是调用的代码:
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RetreiveFeedTask Check=new RetreiveFeedTask();
try{
Work=(Check.execute(Exec, ServerIP, ServerPort)).get();
ALL.setText(Work);//Display what is returned
}
catch(Exception e)
{e.printStackTrace(); }
if(Work.equal("xyz")
{
//Do Stuff
}
else
{
//Do Other Stuff
}
}
});
异步代码:
class RetreiveFeedTask extends AsyncTask<String , String , String > {
//private Exception e;
String GotBack="3";
@Override
protected String doInBackground(String... params) {
try{
Socket s= new Socket(params[1], Integer.parseInt(params[2]));
s.close();
}
catch(Exception e){e.printStackTrace(); return "3";}
try{
Socket socket = new Socket(params[1], Integer.parseInt(params[2]));
PrintWriter output = new PrintWriter(socket.getOutputStream());
BufferedReader input=new BufferedReader(new InputStreamReader(socket.getInputStream()));
output.println(params[0]);
output.flush();
String read="";
while((read=input.readLine())!=null)
{
GotBack=read;
}
socket.close();
}
catch(SocketException e){e.printStackTrace(); }
catch(Exception e){e.printStackTrace();}
return GotBack;
}
}
目前,当我按下按钮时,它会冻结...
我检查 {ServerIP1-ServerPort1} 是否接受连接,如果没有,我需要知道。