java.net.ConnectException:无法连接到 /192.168.253.3(端口 2468):连接失败:ECONNREFUSED(连接被拒绝)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28167054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:43:09  来源:igfitidea点击:

java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused)

javaandroidnetworkingwifi

提问by Entel

I want to transfer some data between PC and a mobile phone with WiFi.

我想通过WiFi在PC和手机之间传输一些数据。

This is how I get the WiFi IP address:

这是我获取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);

This is the code about sending a message to the PC:

这是关于向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;
    }
}

But an error occurs,

但是出现错误,

java.net.ConnectException: failed to connect to /192.168.253.3 (port 2468): connect failed: ECONNREFUSED (Connection refused)

java.net.ConnectException:无法连接到 /192.168.253.3(端口 2468):连接失败:ECONNREFUSED(连接被拒绝)

Besides, I use a android phone to run the app.

此外,我使用安卓手机来运行该应用程序。

采纳答案by Stephen C

A connect failed: ECONNREFUSED (Connection refused)most likely means that there is nothing listening on that port AND that IP address. Possible explanations include:

Aconnect failed: ECONNREFUSED (Connection refused)最有可能的意思是没有在该端口和该 IP 地址上侦听任何内容。可能的解释包括:

  • 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 unlikelygiven that normal practice (these days) is for firewalls to "blackhole" all unwanted connection attempts.
  • 该服务已崩溃或尚未(成功!)启动,
  • 您的客户端正在尝试使用错误的 IP 地址或端口进行连接,
  • 您的客户端正在尝试使用解析为错误 IP 的 DNS 名称进行连接,或者
  • 服务器访问被代表服务器/服务“拒绝”的防火墙阻止。鉴于正常做法(这些天)是防火墙“黑洞”所有不需要的连接尝试,因此这不太可能


Note that while you have an array variable called urls, it cannot contain real URLs. There is no overload of the Socketconstructor that takes a real URL in any form. Indeed, if you supplied a URL in string form like this:

请注意,虽然您有一个名为 的数组变量urls,但它不能包含真实的 URL。没有Socket采用任何形式的真实 URL的构造函数的重载。事实上,如果你提供了一个像这样的字符串形式的 URL:

 new Socket("http://example.com", 42)

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".

结果将是一个不同的例外。同样,如果您尝试连接到无法路由到的网络上的 IP 地址(例如“不同的 WiFi 网络”),那么您将获得不同的异常;例如“找不到主机”、“没有到主机的路由”或“没有到网络的路由”。

回答by Jaydev

Why this might have happened:

为什么会发生这种情况:

  1. The server couldn't send a response: Ensure that the backend is working properly at IP and port mentioned.
  2. SSL connections are being blocked: Fix this by importing SSL certificates
  1. 服务器无法发送响应:确保后端在提到的 IP 和端口上正常工作。
  2. SSL 连接被阻止:通过导入 SSL 证书解决此问题

Less likely:

比较不可能:

  1. Cookies not being sent
  2. Request timeout: Change request timeout
  1. Cookie 未发送
  2. 请求超时:更改请求超时

回答by Farrukh Najmi

A common mistake during development of an android app running on a Virtual Device on your dev machine is to forget that the virtual device is not the same host as your dev machine. So if your server is running on your dev machine you cannot use a "http://localhost/..." url as that will look for the server endpoint on the virtual device not your dev machine.

在您的开发机器上的虚拟设备上开发运行 android 应用程序期间的一个常见错误是忘记虚拟设备与您的开发机器不是同一台主机。因此,如果您的服务器在您的开发机器上运行,则不能使用“ http://localhost/...” url,因为这将在虚拟设备上查找服务器端点,而不是您的开发机器。

回答by Osama Saeed

I was also getting the same issue I tried multiple IPs like my public IP and localhost default IP 127.0.0.1 in windows and default gateway but same response. but I forget to check by

我也遇到了同样的问题,我在 Windows 和默认网关中尝试了多个 IP,例如我的公共 IP 和 localhost 默认 IP 127.0.0.1,但响应相同。但我忘记检查了

C:> ipconfig

ipconfig cleanly say what is my actual IP address of that adapter with which I have connected like I was connected with Wifi adapter my IP address will show me as:

ipconfig 清楚地说明我连接的那个适配器的实际 IP 地址是什么,就像我连接 Wifi 适配器一样,我的 IP 地址将显示为:

Wireless LAN adapter Wireless Network Connection:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::69fa:9475:431e:fad7%11
   IPv4 Address. . . . . . . . . . . : 192.168.15.92

I hope this will help you.

我希望这能帮到您。

回答by user5091911

first,i used "localhost:port" format met this error.then I changed the address to "ip:port" format and the problem solved.

首先,我使用“localhost:port”格式遇到了这个错误。然后我将地址更改为“ip:port”格式,问题解决了。

回答by Anuj Agrawal

you can covert domain to IP address because every Domain have specific IP address, then you will solve that issue. I hope this will help you.

您可以将域转换为 IP 地址,因为每个域都有特定的 IP 地址,然后您将解决该问题。我希望这能帮到您。

回答by shkolnik 225

I solved the same problem, I used a network connection through a proxy server, when I selected the option not to use proxies for internal and local connections, the problem disappeared

我解决了同样的问题,我通过代理服务器使用网络连接,当我选择不使用代理进行内部和本地连接的选项时,问题消失了

回答by Siddharth

check the name of the database in a file where you established a connection.

在建立连接的文件中检查数据库的名称。