如何在Android模拟器上绑定电脑的IP端口?
我使用这些代码让它监听本地端口56789。我在同一个内网的另一台计算机上编写了一个程序。尝试在那个地方连接它但超时。 所以真正的问题是为什么这种处理绑定端口的方式不正确。谢谢!
try
{
ServerSocket ss=new ServerSocket(56789);
System.out.println("before accpet!");
Socket s=ss.accept();
System.out.println("accpet!");
}
catch(Exception e){
e.printStackTrace();
}
I use these codes to make it listen to the local port of 56789. And I make a program on another computer at the same intranet.Trying to connect it at that place but it times out.
So the real question is why dealing binding port this way is not proper.Thank you!
try
{
ServerSocket ss=new ServerSocket(56789);
System.out.println("before accpet!");
Socket s=ss.accept();
System.out.println("accpet!");
}
catch(Exception e){
e.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 ServerSocket 的 Android 文档,听起来它是绑定的默认情况下为 localhost 127.0.0.1 地址。如果是这种情况,您应该绑定到模拟器的外部 IP 地址,该地址可以是 通过 NetworkInterface 检索。请记住,模拟器的 IP 地址与主机的 IP 地址不同。
Looking at the Android documentation for ServerSocket, it sounds like it is binding to the localhost 127.0.0.1 address by default. If this is the case, you should be binding to the external IP address of the emulator instead, which can be retrieved via NetworkInterface. Keep in mind that the IP address of the emulator is different from the IP address of your host machine.