我希望使用WiFi在电脑和手机之间传输一些数据。
以下是获取WiFi IP地址的步骤:
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);
这是发送消息到PC的代码:
Socket socket = null;
String message = "test\r\n";
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;
}
}
但是发生了一个错误,
java.net.ConnectException:连接到/192.168.253.3(端口2468)失败:连接被拒绝(Connection refused)
此外,我使用安卓手机运行该应用程序。