Android如何连接到127.168.1.1?
我的电脑上有一个服务器程序,正在运行并等待Android客户端 我在Android代码中设置了这些:Android如何连接到127.168.1.1?,android,network-programming,Android,Network Programming,我的电脑上有一个服务器程序,正在运行并等待Android客户端 我在Android代码中设置了这些: mHost = "127.168.1.1"; //or getLocalIpAddress()? mPort = 5000; mSocket = new Socket(mHost, mPort); 并在清单中具有以下权限: <uses-permission android:name="android.permission.INTERNET" /> <
mHost = "127.168.1.1"; //or getLocalIpAddress()?
mPort = 5000;
mSocket = new Socket(mHost, mPort);
并在清单中具有以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
我有一个路由器,如何将我的电脑连接到127.168.1.1:5000
编辑:
public static final String getLocalIpAddress() {
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();
String ip4 = inetAddress.getHostAddress().toString();
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip4)) {
Log.d(TAG, "getLocalIpAddress(): " + ip4);
return ip4;
}
}
}
}
catch (Exception e) {
Log.e(TAG, "ServerUtils: getLocalIpAddress(): " + e.getMessage());
}
return null;
}
public静态最终字符串getLocalIpAddress(){
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
字符串ip4=inetAddress.getHostAddress().toString();
如果(!inetAddress.isLoopbackAddress()&&InetAddressUtils.isIPv4Address(ip4)){
Log.d(标记“getLocalIpAddress():”+ip4);
返回ip4;
}
}
}
}
捕获(例外e){
Log.e(标记“ServerUtils:getLocalIpAddress():”+e.getMessage());
}
返回null;
}
这是我的新代码,返回192.168.1.104,它不工作。使用ipconfig,我得到192.168.1.102(这是正确的)
那么,在最后一段代码中,有什么问题?如何获得正确的本地IP?在您的LAN中可能会有所不同,但通常地址192.168.1.1是路由器地址(而不是主机地址) 通过运行以下命令确认您的主机地址: ipconfig 在命令提示下。IPv4地址将是主机地址,网关地址将是路由器地址。确保在PC和设备上使用正确的地址 注意:如果您需要更精确的帮助,您需要在PC和设备上发布代码的相关部分 --编辑-- IP地址就像一个家庭地址,每个设备都有自己的地址。因此,当您在windows计算机上运行ipconfig时,您将获得windows计算机的ip地址。在设备上调用
getLocalIpAddress()
时,您将获得设备IP地址
如果要从设备连接到PC,您需要:
- 在PC上运行侦听端口5000的程序。如果此端口上没有正在侦听的程序,将拒绝任何连接到此端口的尝试
- 在设备上,您运行的程序连接到端口
192.168.1.102(pc IP地址)5000
getLocalIpAddress()
时,您将获得设备IP地址
如果要从设备连接到PC,您需要:
- 在PC上运行侦听端口5000的程序。如果此端口上没有正在侦听的程序,将拒绝任何连接到此端口的尝试
- 在设备上,您运行的程序连接到端口
192.168.1.102(pc IP地址)5000
127.168.1.1默认处于环回范围内,因此连接尝试可能不会离开您的Android设备。尝试将服务器置于192.168.x.x ip上
这就是防火墙的问题。。。。转到路由器的防火墙或系统的防火墙。。。并将5000端口添加到异常列表中。。。入站和出站。。。。第二个问题是你的IP地址。。。127不是一个可用的类。。。这是环回测试(127.0.0.1到127.255.255.254…)将您的地址从127更改为192.168.0.1或1.1 这就是防火墙的问题。。。。转到路由器的防火墙或系统的防火墙。。。并将5000端口添加到异常列表中。。。入站和出站。。。。第二个问题是你的IP地址。。。127不是一个可用的类。。。这是环回测试(127.0.0.1到127.255.255.254…)将您的地址从127更改为192.168.0.1或1.1 您忘记在清单中设置一些权限了吗?Android设备已连接到您的本地网络(通过wi-fi),没有尝试通过4g上网,并且被路由器的防火墙阻止。如果是这样,您的电脑上是否有防火墙(即windows防火墙)您可以发布您的整个清单吗?请确保您的权限处于正确的层次结构级别。遗憾的是,IP正在更改,如何在android中检测正确的本地IP地址?它给了我错误的最后一个号码。你忘了在清单中设置一些权限吗?Android设备连接到你的本地网络(通过wi-fi),没有尝试通过4g上网,并且被路由器的防火墙阻止。如果是这样,您的电脑上是否有防火墙(即windows防火墙)您可以发布您的整个清单吗?请确保您的权限处于正确的层次结构级别。遗憾的是,IP正在更改,如何在android中检测正确的本地IP地址?它给了我错误的最后一个数字,所以你如何解释
java.lang.NullPointerException
?那么你如何解释java.lang.NullPointerException
?这很好,但IP正在更改,请查看我更新的最后一个代码这很好,但IP正在更改,请查看我更新的最后一个代码
public static final String getLocalIpAddress() {
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();
String ip4 = inetAddress.getHostAddress().toString();
if (!inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ip4)) {
Log.d(TAG, "getLocalIpAddress(): " + ip4);
return ip4;
}
}
}
}
catch (Exception e) {
Log.e(TAG, "ServerUtils: getLocalIpAddress(): " + e.getMessage());
}
return null;
}