Android套接字聊天连接错误
嘿!我正在使用套接字和线程从 java 客户端到 android 客户端调整聊天。服务器保持不变。我已在清单中写入了互联网和 ACCESS_NETWORK_STATE 权限。问题是,当我尝试连接到服务器时,它会抛出一些错误。 try{ socket = new Socket("localhost", 5000);}
行抛出:
可能是什么问题?您想将整个代码放在这里吗?
Hy! I'm adapting a chat using sockets and threads from java client to android client. The server remains the same. I've wrote the internet and ACCESS_NETWORK_STATE
permissions in the manifest. The problem is that when I try to connect to server it throws some errors.
The try{ socket = new Socket("localhost", 5000);}
line throws:
What could be the problem ? Would you want to put the whole code here ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我确信您正在尝试联系本地计算机而不是设备本身。手机将使用
localhost
或127.0.0.1
自行寻址。因此,当您的设备不是服务器并且没有侦听该端口时,连接将会失败。尝试使用
10.0.2.2
。这应该针对您正在开发的机器。 (来源)I'm sure that you're trying to contact your local machine and not the device itself. The phone will address itself using
localhost
or127.0.0.1
. So when your device is not a server and is not listening for that port the connection will fail.Try to use
10.0.2.2
. This should target your machine you're developing on. (source)您是否尝试过使用机器的真实网络IP(例如192.168.0.1)而不是“localhost”?在我看来,错误本身就像没有人在监听端口 5000。所以我猜 Droid 正在尝试连接到自身而不是服务器。
Have you tried using the real network ip of your machine (sth. like 192.168.0.1) instead of "localhost"? The error itself looks to me like there's nobody listening on port 5000. So I guess the Droid is trying to connect to itself instead to the server.