I want to transfer some data between PC and a mobile phone with WiFi.
This is how I get the WiFi IP address:
String ip = String.format(
"%d.%d.%d.%d",
(wifiInfo.getIpAddress() & 0xff),
(wifiInfo.getIpAddress() >> 8 & 0xff),
(wifiInfo.getIpAddress() >> 16 & 0xff),
(wifiInfo.getIpAddress() >> 24 & 0xff));
new Recive().execute(ip);
This is the code about sending a message to the PC:
Socket socket = null;
String message = "test
";
protected Void doInBackground(String... urls) {
try {
Log.i("ip", urls[0]);
socket = new Socket(urls[0], 2468);
toserver = new DataOutputStream(socket.getOutputStream());
toserver.writeBytes(message);
toserver.flush();
toserver.close();
socket.close();
return null;
} catch (Exception e) {
Log.i("e", e.toString());
return null;
}
}
But an error occurs,
java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused)
Besides, I use a android phone to run the app.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…