0

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 !

James M
  • 18,506
  • 3
  • 48
  • 56

4 Answers4

0

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);
Shade
  • 9,936
  • 5
  • 60
  • 85
  • Error: the constructor Socket(InetAddress[], int) is undefined ! – Mohammad Shaker Apr 09 '13 at 13:42
  • Are you sure you changed `getAllByName` to `getByName`? See the link in my post. It shows the `Socket(InetAddress, int)` is defined and documented as of API level 1. – Shade Apr 09 '13 at 13:51
  • Yeah i did it, my code now is: Socket socket = new Socket(InetAddress.getByName("192.168.1.2"), 44444); – Mohammad Shaker Apr 09 '13 at 13:52
  • And the error is the same? Does the error log show the error in the same place? Did you paste my code in multiple files? Paste the new error. – Shade Apr 09 '13 at 13:55
  • There is no error. The server still running and the client running but when i'm trying to connect nothing happens – Mohammad Shaker Apr 09 '13 at 13:58
  • Hmmm, don't really know if relevant, but as a last resort, could you try `Socket socket = new Socket (InetAddress.getByAddress(new byte[]{(byte)192, (byte)168, 1, 2}), 44444);` – Shade Apr 09 '13 at 14:27
0

Are you sure your server is visibile to your smartphone?...You could try to connect a pc to the WIFI and use telnet ip port and verify if you can reach the server.

FrancescoAzzola
  • 2,666
  • 2
  • 15
  • 22
0

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

Naveen
  • 1
-1

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

Tanmay Mandal
  • 39,873
  • 12
  • 51
  • 48
  • Try to install Ping app from android play store and make sure that you are being able to communicate to the port 44444 from your device https://play.google.com/store/apps/details?id=com.ulfdittmer.android.ping – Tanmay Mandal Apr 09 '13 at 13:39
  • I think your port 44444 is not open for the device. – Tanmay Mandal Apr 09 '13 at 13:42
  • There is an option in the spinner of the application's Home Page called "Check Port Range".Select it and give your port number you want to check. – Tanmay Mandal Apr 09 '13 at 13:52
  • When i checked if the port 44444 is open or not it connects on it and told me that it is opened but the program still can't connect to the server ! – Mohammad Shaker Apr 09 '13 at 14:09