Disconnected Android Socket Connection

I try to open Socket on Android, but every time I try, I get a refusal message all the time, no matter how I try.

The error occurs in the next line of code where I try to create a 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 I get the error:


 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 assume this is a bug with my iPaddress and port? But can someone point me in the right direction, what could be the reason?

+4
source share
5 answers

From your code, I assume that you are trying to open a socket connection from an Android application on the same Android device.

Based on Linux and all, Android may have some kind of firewall or connection rule that prevents your socket from connecting successfully. Check the documentation, it could be an Android configuration problem, not a Java problem.

0
source

A properly encoded Android application that advertises network permission in its manifest can accept connections on unprivileged ports because the device does not have a firewall.

However, many Wi-Fi and most mobile networks place the device behind NAT and an external firewall, so incoming connection attempts can never reach it.

+2
source

I used the ipconfig command at the prompt, and then I used the "IPv4 address" to connect to the current PC.

I successfully connected the Server, and also sent and 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()); 
+2
source

In my case, I needed to redirect ports, look:

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

Or maybe you can also try port forwarding.

+1
source

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

ServerSocket socket = new ServerSocker(8888);

0
source

Source: https://habr.com/ru/post/1303813/


All Articles