我正在开发一个应用程序来向端口502发送请求,并希望从同一端口读取响应。
这是我到目前为止所尝试的
public class Autoamtion extends Activity {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
Log.v("on create", "on create");
final ToggleButton fanOn = (ToggleButton) findViewById(R.id.toggleButton1);
fanOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (fanOn.isChecked()) {
Toast.makeText(Autoamtion.this, "Fan is On",
Toast.LENGTH_SHORT).show();
Log.v("on create", "on create");
try {
echoSocket = new Socket("192.168.1.19", 502);
out = new PrintWriter(echoSocket.getOutputStream(),
true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to:192.168.1.19");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
try {
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.close();
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stdIn.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
echoSocket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else
Toast.makeText(Autoamtion.this, "Fan is Off",
Toast.LENGTH_SHORT).show();
}
});
}
}
我刚刚安装了一个simulator,打开了端口502.我确认在运行此simulator后该端口已打开。
我刚建立了与502端口的连接。我没有得到任何答复。
请引导我完成这个,这样我才能使这个工作。任何建议和帮助将不胜感激 感谢
答案 0 :(得分:0)
你也应该发布stackTrace,检查一下是什么意思。
至少有一件事与您的信息不一致,请参阅port 503
以及您使用echoSocket = new Socket("192.168.1.19", 502);
连接到端口502
的代码中的文字。