简体   繁体   English

为什么我不能创建从 Android 设备到 PC 的 tcp 套接字

[英]Why can not I create tcp socket from Android device to PC

I'm trying to connect my Android app to desktop server.我正在尝试将我的 Android 应用程序连接到桌面服务器。 The problem is when I try to create LAN via portable Wi-Fi hotspot on my smartphone everything works fine, but when I connect PC and phone to the router I have TimeOutException creating a socket in Android app.问题是当我尝试通过智能手机上的便携式 Wi-Fi 热点创建 LAN 时一切正常,但是当我将 PC 和手机连接到路由器时,我有 TimeOutException 在 Android 应用程序中创建一个套接字。 On the other hand, when I connect desktop client to desktop server via router it works fine too, so the problem occures only when I try to connect mobile client to desktop server via router.另一方面,当我通过路由器将桌面客户端连接到桌面服务器时,它也可以正常工作,所以只有当我尝试通过路由器将移动客户端连接到桌面服务器时才会出现问题。

Client code (Java):客户端代码(Java):

Socket socket = new Socket();
socket.connect(new InetSocketAddress(ip, port), 1000); // TimeOutException

Server code (C++/Qt):服务器代码(C++/Qt):

QTcpServer m_tcp_server;
// ...
m_tcp_server->listen(QHostAddress::Any, m_port);

ip address and port are right (100%) ip地址和端口正确(100%)

UPD:升级版:

Thank you, guys.感谢你们。

It was just a router problem.这只是一个路由器的问题。 I tested in on the another one and there is no problem我在另一个上测试了,没有问题

try check which ip you have assigned on android pohone and if you have route to the destination server ip.尝试检查您在 android pohone 上分配的 ip 以及是否有到目标服务器 ip 的路由。

Is possilbe you get different ip on wifi and different ip by cable.是否可能您在 wifi 上获得不同的 ip,通过电缆获得不同的 ip。

Also you can try an android net tool to try connect to the server.您也可以尝试使用 android net 工具尝试连接到服务器。

I had same problem, In my case i created server socket with wrong ip address.我有同样的问题,在我的情况下,我创建了错误的 IP 地址的服务器套接字。 Hotspot network band is 192.168.43.1~255热点网络频段为192.168.43.1~255

You need to create server socket with ip address that hotspot dhcp made like 192.168.43.11您需要使用热点 dhcp 制作的 IP 地址创建服务器套接字,例如 192.168.43.11

I had wrongfully made server socket with ip 192.168.0.22, this is wrong.我错误地使用 ip 192.168.0.22 创建了服务器套接字,这是错误的。

And your android app should open client socket with it.你的 android 应用程序应该用它打开客户端套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM