I am trying to send a simple text from my android app to a server running on my laptop but even though I went through all the stuff I found on the internet I couldn't get it working. So this is the code on my android app:
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);
}
}
On my laptop I run netcat listening on the port. The ip is my laptop's IP on icanhazip.com. Both my laptop and my phone are connected to a wi-fi network.
What am I doing wrong?