Java Android Socket 连接被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2425774/
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
Android Socket connection refused
提问by Donal Rafferty
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.
我正在尝试在 Android 上打开一个 Socket,但每次尝试时,无论我尝试什么方式,我都会收到连接被拒绝的错误。
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:
下面是我用来获取本地ip地址的方法:
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?
我认为我的 Ipaddress 和或端口有问题?但是任何人都可以指出我可能是什么原因的正确方向吗?
采纳答案by Olivier Croisier
From your code, I assume you're trying to open a socket connection from an Android app to the same Android device.
根据您的代码,我假设您正在尝试打开从 Android 应用程序到同一 Android 设备的套接字连接。
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.
基于 Linux 和所有,也许 Android 有某种防火墙或连接规则,阻止您的套接字成功连接。检查文档,这可能是 Android 配置问题而不是 Java 问题。
回答by Meriadoc
In my case, I needed to redirect the ports, have a look:
就我而言,我需要重定向端口,看看:
http://developer.android.com/guide/developing/tools/emulator.html#redirections
http://developer.android.com/guide/developing/tools/emulator.html#redirections
Or maybe you can try forwarding the ports too.
或者您也可以尝试转发端口。
回答by Chris Stratton
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.
在其清单中声明网络权限的正确编码的 android 应用程序可以接受非特权端口上的连接,因为设备上没有防火墙。
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.
但是,许多 wifi 和大多数移动网络会将设备置于 NAT 和外部防火墙之后,因此入站连接尝试可能永远无法到达。
回答by Ganesh Bhosale
I have used the ipconfig command in prompt and then i used the "IPv4 Address" for connecting to current PC.
我在提示符下使用了 ipconfig 命令,然后我使用了“IPv4 地址”来连接到当前的 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:
PC服务器端代码:
socket = ss.accept();
InetAddress ia = socket.getInetAddress();
System.out.println("Client Address: " + ia.getHostAddress());
回答by Silas
I had the same problem. I had to use ServerSocket instead of Socket:
我有同样的问题。我不得不使用 ServerSocket 而不是 Socket:
ServerSocket socket = new ServerSocker(8888);
ServerSocket socket = new ServerSocker(8888);