简体   繁体   English

服务器/客户端Android程序无法在真正的android设备上运行

[英]Server/Client Android program doesn't work on real android device

I'm making a Server/Client program that sends and receives data between them. 我正在制作一个在它们之间发送和接收数据的服务器/客户端程序。 The program is working perfectly on the emulator, but when testing it on a real android device through WIFI it can't connect to the server. 该程序可以在模拟器上完美运行,但是当通过WIFI在真实的android设备上对其进行测试时,它无法连接至服务器。

Server Side 服务器端

ServerSocket serverSocket = new ServerSocket(44444);
Socket clientSocket = serverSocket.accept();

Client Side 客户端

Socket socket = new Socket("192.168.1.2", 44444);

The problem isn't in sending data, it's in the connection establishment. 问题不在于发送数据,而在于连接建立。 That Android phone doesn't see the server side ! 那部Android手机看不到服务器端!

You are using a constructor, which expects a string hostname . 您正在使用构造函数,该构造函数需要一个字符串hostname However, you are providing an internet address. 但是,您正在提供一个互联网地址。 You have to use another constructor , which accepts an address as a parameter. 你必须使用另一个构造函数 ,它接受一个地址作为参数。

Try the following code on the client side: 在客户端尝试以下代码:

Socket socket = new Socket (InetAddress.getByName ("192.168.1.2"), 44444);

您确定服务器对智能手机可用吗?...您可以尝试将PC连接到WIFI并使用telnet ip端口并验证是否可以访问服务器。

the android devices can not detect the ad-hoc network of windows you have to first make your PC hotspot by doing this : android设备无法检测到Windows的ad-hoc网络,您必须首先通过以下操作使PC成为热点:

step 1: open CMD (admin privileged) and run following command netsh wlan hostednetwork mode=allow ssid=NAME key=PASSWORD 步骤1:打开CMD(具有管理员特权),然后运行以下命令netsh wlan hostednetwork mode = allow ssid = NAME key = PASSWORD

this will make wifi , a hotspot now run this to start wifi hotspot: netsh wlan start hostednetwork 这将使wifi成为热点,现在运行一个热点以启动wifi热点:netsh wlan start hostednetwork

and to stop hotspot run this command : netsh wlan stop hostednetwork 并停止热点运行以下命令:netsh wlan stop hostednetwork

The problem is in your network. 问题出在您的网络中。 Please make sure that the real android device and the your server( 192.168.1.2 ) are in the same network. 请确保真实的android设备和您的服务器( 192.168.1.2 )在同一网络中。

If those are in the same network only then you can communicate with the server from yourclient 如果仅在同一网络中,则可以从客户端与服务器通信

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

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