我正在尝试从我的Android应用程序向我的笔记本电脑上运行的服务器发送一个简单的文本,但即使我查看了我在互联网上发现的所有内容,我也无法使其正常工作。所以这是我的Android应用程序上的代码:
public class Client extends AsyncTask<Void, Void, Void> {
String dstAddress = "89.101.42.238";
int dstPort;
String response = "";
TextView status;
Client(String addr, int port, TextView textResponse) {
dstAddress = addr;
dstPort = port;
this.status = textResponse;
}
@Override
protected Void doInBackground(Void... arg0) {
Socket socket = null;
try {
socket = new Socket("89.101.42.238", 5000);
PrintStream printer = new PrintStream(socket.getOutputStream(), true);
//BufferedWriter cout = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
printer.write("Salut!".getBytes());
printer.write("Please work!!!!!!".getBytes());
//status.setText("Finished");
} catch (UnknownHostException e) {
e.printStackTrace();
response = "UnknownHostException: " + e.toString();
} catch (IOException e) {
e.printStackTrace();
response = "IOException: " + e.toString();
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
}
}
在我的笔记本电脑上,我在端口上运行netcat监听。 ip是icanhazip.com上我的笔记本电脑的IP。我的笔记本电脑和手机都连接到了Wi-Fi网络。
我做错了什么?