34

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\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)

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

5377037
  • 9,493
  • 12
  • 43
  • 73
Entel
  • 549
  • 2
  • 7
  • 12

9 Answers9

21

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

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

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)

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

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096
10

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.

Farrukh Najmi
  • 4,418
  • 2
  • 29
  • 45
7

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

Less likely:

  1. Cookies not being sent
  2. Request timeout: Change request timeout
soshial
  • 3,252
  • 4
  • 27
  • 36
Jaydev
  • 1,545
  • 18
  • 36
  • 1
    It is highly unlikely that scenarios 3 and 4 would cause "Connection Refused". 1 is plausible. 2 is plausible ... depending on what the webserver does if you attempt to enable SSL support without configuring any server certs. – Stephen C Sep 25 '18 at 12:38
1

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

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:

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.

Osama Saeed
  • 109
  • 7
0

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

user5091911
  • 123
  • 1
  • 2
0

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

Yunnosch
  • 21,438
  • 7
  • 35
  • 44
0

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

0

My problem was solved after turning Off Windows Firewall Defender in public network as I was connected with that network.

Tanvi Agarwal
  • 33
  • 1
  • 7
-5

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.