简体   繁体   中英

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.

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 !

You are using a constructor, which expects a string 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 :

step 1: open CMD (admin privileged) and run following command 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

and to stop hotspot run this command : 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.

If those are in the same network only then you can communicate with the server from yourclient

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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