Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
966 views
in Technique[技术] by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...