Android Socket connection refused

天大地大妈咪最大 提交于 2021-01-21 08:42:45

问题


I am trying to open a Socket on Android but everytime I try I keep getting connection refused error no matter what way I try.

The error happens on the following line of code where I try to create the connection.


Socket socket = new Socket(getLocalIpAddressString(), 8008);

The following is the method I use to get the local ip address:


public static String getLocalIpAddressString() {
 try {
     for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
         NetworkInterface intf = en.nextElement();
         for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
             InetAddress inetAddress = enumIpAddr.nextElement();
             if (!inetAddress.isLoopbackAddress()) {
                 return inetAddress.getHostAddress().toString();
             }
         }
     }
 } catch (SocketException ex) {
     Log.e("IPADDRESS", ex.toString());
 }
 return null;

}


And the error I get is:


WARN/System.err(3574): java.net.ConnectException: /192.168.2.163:8008 - Connection refused
WARN/System.err(3574):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:237)
WARN/System.err(3574):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:199)
WARN/System.err(3574):     at java.net.Socket.startupSocket(Socket.java:734)
WARN/System.err(3574):     at java.net.Socket.<init>(Socket.java:157)
WARN/System.err(3574):     at com.networks.phone.engine.SIPEngine.rtpTest(SIPEngine.java:1444)

I presume its an error with my Ipaddress and or port? But can anyone point me in the right direction as to what might be the cause?


回答1:


From your code, I assume you're trying to open a socket connection from an Android app to the same Android device.

Being based on Linux and all, maybe Android has some kind of firewall or connection rules that prevent your socket to connect successfully. Check the documentation, it may be an Android configuration problem and not a Java problem.




回答2:


A properly coded android application which declares network permission in its manifest can accept connections on unpriveleged ports as there is no firewall on the device.

However, many wifi and most mobile networks will place the device behind NAT and an external firewall, so inbound connection attempts may never reach it.




回答3:


I have used the ipconfig command in prompt and then i used the "IPv4 Address" for connecting to current PC.

I have connected the Server successfully as well as sent & received bytes.

Android Client Side Code:

s = new Socket("192.168.42.85",12345);

PC Server Side Code:

socket = ss.accept();
InetAddress ia = socket.getInetAddress();
System.out.println("Client Address: " + ia.getHostAddress());



回答4:


In my case, I needed to redirect the ports, have a look:

http://developer.android.com/guide/developing/tools/emulator.html#redirections

Or maybe you can try forwarding the ports too.




回答5:


I had the same problem. I had to use ServerSocket instead of Socket:

ServerSocket socket = new ServerSocker(8888);



来源:https://stackoverflow.com/questions/2425774/android-socket-connection-refused

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!