Android ServerSocket accepts a wi-fi hotspot

2k views Asked by At

I have an Android device that is running as an access point (a.k.a. Wi-Fi hotspot) and it is running this code:

serverSocket = new ServerSocket(serverPort);

serverSocket.setReuseAddress(true);

while (RUNNING) {
    try {
        final Socket socket = serverSocket.accept();

I will have some other client device connect to this device's Wi-Fi hotspot, and try to connect to the socket, and it will not be able to.

The access point's IP address is something like 192.168.43.1, so I will try to connect from the client device to 192.168.43.1:8080 and it will not work.

However, if both devices are connected to another Wi-Fi network, the serverSocket.accept(); will work just fine! I am using port 8080.

Do I need to tell the server socket to accept on the interface "ap0"? (Can I do that?)

I changed the port to 8988 and now it works just fine. Is it a firewall issue on the android device?

Any ideas how to navigate this issue?

1

There are 1 answers

3
5er On

Yes you need to open socket on the server side also.

You should try the Android Example WiFiDirectDemo accessible in Android legacy: \Android\android-studio\sdk\samples\android-19\legacy just import it into your enviroment and give a try..