2

Here'e the problem.

I have two android phones, both connected to the same wi-fi network. One phone is listening with a ServerSocket and the other attempts to connect with this socket.

However, this is not happening. (i.e) The connection does not get established. However, when I create a listening ServerSocket on my PC (also on the same network) and set my phones to connect to the PC, the connection is established.

In other words, I am able to create the connection between a phone and PC but not between phone and phone. Does anyone have any idea of what might possibly be causing this problem?

Thank you!

Below is the code I have used for getting the IPv4 address.

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
int[] address = new int[4];
String ip = "";
for (int i = 0; i < 4; i++) {
   address[i] = ipAddress % 256;
   ipAddress /= 256;
   if (i != 3)
      ip += (String.valueOf(address[i]) + ".");
   else
      ip += String.valueOf(address[i]);
}
return ip;

Edit:

I tried logging the exact location and cause and it gave the following error:

java.net.SocketException: No route to host at 
org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method) at 
dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357) at 
org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:207) at 
org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:186) at 
java.net.Socket.startupSocket(Socket.java:735) at 
java.net.Socket.<init>(Socket.java:263) at 
com.test.word.WifiActivity$12.run(WifiActivity.java:460) at
java.lang.Thread.run(Thread.java:1027)
  • If you are connecting two phone with API version 14 or above then it might be a `IP version 6` issue. – Ali Imran Dec 08 '12 at 13:08
  • One phone is on 16 and the other is on 12. I am quite sure the network uses IPv4. – Praveen Janakarajan Dec 08 '12 at 13:39
  • See this link and solve your problem :-http://stackoverflow.com/questions/11015912/how-do-i-get-ip-address-in-ipv4-format – Ali Imran Dec 08 '12 at 13:43
  • I know that this is the IPV6 issue as i faced this in developing a two player network game :) – Ali Imran Dec 08 '12 at 13:44
  • Can you connect from the PC as a client to the phone as the server? Have you verified that you have the right IP addresses for the phones? Can you connect from a client on the phone to a server on the SAME phone, using both the loopback address and the actual IP address? – Chris Stratton Dec 08 '12 at 15:01
  • @Chris , everything that you suggested works. And I have verified that the IP addresses are correct. – Praveen Janakarajan Dec 08 '12 at 15:56

1 Answers1

0

Forgetting the network and re-connecting to it solved the problem.

Thanks for all your help!