java.net.ConnectException:无法连接到/192.168.253.3(端口 2468):连接失败:ECONNREFUSED(连接被拒绝) [英] java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused)
问题描述
我想在有 WiFi 的 PC 和手机之间传输一些数据.
I want to transfer some data between PC and a mobile phone with WiFi.
这是我获取 WiFi IP 地址的方式:
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);
这是关于向PC发送消息的代码:
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;
}
}
但是出现错误,
java.net.ConnectException:无法连接到/192.168.253.3(端口 2468):连接失败:ECONNREFUSED(连接被拒绝)
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.
推荐答案
A connect failed: ECONNREFUSED (Connection denied)
很可能意味着在该端口和该 IP 地址上没有侦听任何内容.可能的解释包括:
A connect failed: ECONNREFUSED (Connection refused)
most likely means that there is nothing listening on that port AND that IP address. Possible explanations include:
- 服务已崩溃或尚未(成功!)启动,
- 您的客户端正在尝试使用错误的 IP 地址或端口进行连接,
- 您的客户端正在尝试使用解析为错误 IP 的 DNS 名称进行连接,或者
- 服务器访问被代表服务器/服务拒绝"的防火墙阻止.这不太可能,因为通常的做法(现在)是让防火墙黑洞"所有不需要的连接尝试.
- the service has crashed or hasn't been (successfully!) started,
- your client is trying to connect using the wrong IP address or port,
- your client is trying to connect using a DNS name that resolves to the wrong IP, or
- server access is being blocked by a firewall that is "refusing" on the server/service's behalf. This is pretty unlikely given that normal practice (these days) is for firewalls to "blackhole" all unwanted connection attempts.
请注意,虽然您有一个名为 urls
的数组变量,但它不能包含真正的 URL.Socket
构造函数没有任何形式的真正 URL 的重载.事实上,如果你提供了一个像这样的字符串形式的 URL:
Note that while you have an array variable called urls
, it cannot contain real URLs. There is no overload of the Socket
constructor that takes a real URL in any form. Indeed, if you supplied a URL in string form like this:
new Socket("http://example.com", 42)
结果将是一个不同的例外.同样,如果您尝试连接到无法路由到的网络上的 IP 地址(例如不同的 WiFi 网络"),那么您将收到不同的异常;例如找不到主机"、没有到主机的路由"或没有到网络的路由".
the result would be a different exception. Likewise, if you attempt to connect to an IP address on a network that you can't route to (e.g. "a different WiFi network"), then you will get a different exception; e.g. "host not found", "no route to host" or "no route to network".
这篇关于java.net.ConnectException:无法连接到/192.168.253.3(端口 2468):连接失败:ECONNREFUSED(连接被拒绝)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!