1

I want to develop an Android app that connects with a Windows desktop application via TCP/IP. However I have very little knowledge of networking and so please forgive me if this is a very basic doubt.

My Windows based laptop as well as Android phone are connected to the internet via the same WiFi router. Now I checked the IP address for my laptop as well as phone using a website. Both are same! If both have the same IP address, then to achieve networking between these devices I will choose different set of port numbers.

  1. Will this connection work?
  2. Is the connection happening via the internet or just locally on my router?

EDIT: After reading the answer from @Doon, I have broadened my question.

Let's say the local address of laptop is 192.168.1.10 and that of phone is 192.168.1.20. If I code my application to use these IP addresses, it should work as it is a local network. But what if I want my laptop to connect with another phone which is not connected to the WiFi router, rather by 3G network. Then which IP address should be used for the laptop and the other phone? Since I am not allowed to use any other server, I am going to use port forwarding i.e. the user will type in the IP address displayed on the other device. The connection could be initiated on either one of the devices.

If you could also show how to do this programmatically, it would be very helpful. My Windows application is developed in C++ using Qt.

Cool_Coder
  • 4,888
  • 16
  • 57
  • 99

1 Answers1

2

All of your devices are sharing 1 external or WAN IP address using NAPT (network address port translation). Internally on your LAN each device has its own address. So yes it will work but you are going to need to use internal addrss and the devices actual IP address not its perceived address via an external service.

As for the connection locally or via router that all depends on where you are connecting to. If both end points are on your lan or on the same Subnet then the router will not be involved. So in the average home network between your phone and desktop both connected to the same network say via wifi then they are most likely layer 2 adjacent (see the OSI 7 layer model for more info on layer 2 vs layer 3). But once they are not on the same network then routing will be involved and your router will be used. If the phone is connected to 3G or the cell data network and you want it to talk to your desktop on your home network you are going to need to deal with port forwarding on your router and other such fun things.

In regards to updates. Once you leave the local network it gets more complicated especially with IPv4 as address are running out so there is more and more use of nat or IPv6 with 6 to 4 gateways. Do you want the laptop to initiate connect to the phone or phone to the laptop? But normally you will need to iterate your address on your interfaces. Then connect with an external service to get your external IP address and compare and see if they are the same. if both endpoints are dynamically assigned you will need some sort of location mechanism could be dynamic DNS could be locator service etc.

Doon
  • 19,719
  • 3
  • 40
  • 44
  • So how do I find the devices actual IP address? – Cool_Coder Jan 10 '15 at 06:13
  • 1
    On windows ipconfig /all should show you your ipaddress. Probably something like 192.168.0.x or another RFC 1918 assigned space. As for the phone you can see it in the settings under wifi. But. What Language are you coding in windows. If you are looking to get it programatically it will be Lang dependent. As for android see http://stackoverflow.com/questions/6064510/how-to-get-ip-address-of-the-device – Doon Jan 10 '15 at 06:20
  • I am using C++ with Qt for my windows application. – Cool_Coder Jan 10 '15 at 06:24
  • 1
    http://qt-project.org/forums/viewthread/1439 Might help in qt. else check the Lang reference. As your devices might have multiple interfaces you need to find the active one. – Doon Jan 10 '15 at 06:32