Android Socket连接被拒绝 [英] Android Socket connection refused

查看:314
本文介绍了Android Socket连接被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Android上打开一个Socket,但是无论我尝试哪种方式,每次尝试时,我都会不断收到连接被拒绝的错误.

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


以下是我用来获取本地IP地址的方法:


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;

}

我得到的错误是:

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)


我认为其Ipaddress和/或端口有错误吗?但是,谁能为我指出正确的方向,可能是什么原因呢?


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?

推荐答案

从您的代码中,我假设您正在尝试打开从Android应用程序到同一Android设备的套接字连接.

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

基于Linux和所有其他操作系统,Android可能具有某种防火墙或连接规则,这些规则会阻止套接字成功连接.查看文档,这可能是Android配置问题,而不是Java问题.

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.

这篇关于Android Socket连接被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆