I created a UDP server on android app, and this UDP server is reachable via wifi (Local network). I can receive and send data. However when I change to 4G network. The UDP server on Android not able to receive anything. I use ip chicken to check my cell phone IP address and my UDP server port is 2004. Is there anything wrong? or my public ip is not correct?
Assuming that your phone IP address is correct, and it looks like you have checks this, it is important to know that mobile network providers may not support UDP inbound connections on their networks.
While it's not true to say that all don't, it is true to say that not all do so if you want your solution to be portable across networks you may need to look at alternatives anyway.
There are examples and guides on traversing network address translation boundaries on networks - one good example, which is looking at peer to peer applications, is here: http://bford.info/pub/net/p2pnat/
Related
I am trying to develop an android application, through I could share some files or other applications in real time(can be thought as a miniature version of remote desktop sharing). So I am trying to set up a P2P connection between 2 phones, but for that phones would be requiring IP address of each other. Is there any way by which I can get the IP address of the other device by just using the phone number. If not then how will I get to know the IP addresses of the devices?
Could you please post some other suggestions for setting up a real-time P2P connection ?
The phones don't have public IP addresses, and aren't (usually) in the same subnet, so you can't just send IP packets directly from one to the other. You'll need a server to relay the packets. One way I've done this is to use the asmack library and an XMPP server like OpenFile.
Can i interact with a server(android device) from a client(android device) just to query a database located in the server using TCP sockets?
And Yes and No.
If your device is within wireless network and have own local IP. You can connect to it from client phone if it is in the same network just providing proper IP and Port.
If server phone is located in another wireless network, then Port have to be forwarder using router settings to the server port. Bear in mind that if you will reconnect to wireless network by phone, then IP will be automatically assigned by network infrastructure and it might be changed from previous.
If you want to connect to server phone when it is on 3G network (or similar non wifi), you wont have any specific IP, and it is not static, that way it will be almost impossible to create reliable network. It is more Peer-to-Peer model of network you are looking for.
It is much better if you use Server in the middle as Gateway, that way it will be reliable. The only thing is that you will have to host server and its processes with all traffic and so on.
Here is my scenario.
I have my device (android phone).
And I know the IP address of another device (which is some remote device in the Internet)
So how do I send data to the other device?
Do I use protocols like Tcp or Udp?
EDIT : And I know there are public IP addresses and private IP addresses (like when a device connects via WiFi). In such a case is it even possible to use Tcp or Udp???
What you're looking to do is a common task with a common problem (but also with a common solution). You want to send data from one device (computer/phone/etc..) to another. Here's how it works behind the scenes:
There are a lot of computers in the world. Each computer is given an identity - a name, if you will, which is called the computer's IP address. There are many versions/standards of the IP address, and the current standard names an IP address as such: "111.222.333.444". There are only so many permutations with 12 digits of numbers - and entire ranges of IP addresses are reserved. In other words, there aren't enough IP addresses to go around for computers.
Now, routers were invented, among other reasons, to combat this problem. Routers give all computers within its local area network (LAN) one public (WAN) IP address. With an average of one router per household, this saves many IP addresses. Unfortunately, it also breaks direct communication between computers, because how does one communicate with three computers all using the same name? How does one identify computer A from computer B from computer C when they all share the same WAN IP address?
First, let's clarify that - just like there are a few versions and standards of IP addresses, so there are a few types of routers. By the way, it's not the router that is to blame for the problems of direct communication between computers - it's actually the router's underlying technology of network address translation (NAT). Anyways, there are a few types of NAT, and although information on the internet is relatively scarce on these types, you can still Wikipedia it and read a few university research papers. The types are Full Cone NAT, Restricted NAT, Port Restricted NAT, and Symmetric NAT.
So, computers in a LAN share the same WAN IP. This is a problem. What's one solution? What if we instructed the router to route all incoming traffic directed to a certain port, to a certain computer in our local area network? For example, if three LAN computers share the WAN IP of 1.1.1.1, we can setup a procedure called port forwarding to instruct the router to route all packets arriving on port 6000 to 192.168.1.101:3500.
uPnP is just a fancy technology (not always present in routers - or more importantly, hardly ever enabled by default) that allows you to port forward using code.
If we can't use uPnP to port forward because not all devices have it enabled, there must be another solution - and there is: hole punching.
Hole punching comes in two flavors: UDP and TCP, although TCP hole punching is a bit more complicated because TCP is inherently first a connection-oriented protocol. UDP hole punching is easier because you can send packets without a connection (in fact, is there really such a thing as a connection?)
You can read the procedure on UDP hole punching on Wikipedia and other sites. The concept works like this, though. Any computer sending an outbound packet will expect a reply, meaning the router will allow the inbound packet's reply. At first, two computers will not expect a reply from each other, so the first packet each computer sends will be rejected by the router. If we continue sending packets, however, the router will allow the rest of the packets to come through, because a reply is expected from each computer. As long as this hole-punched connection is maintained active by occasional keep-alive packets, it will stay alive. Unfortunately, this method of UDP hole punching is unsuccessful for Symmetric NATs (explained in the links below). That's why the end-all solution is to simply proxy the connection and traffic through your own server. This requires a farm of computers, so it's quite costly. But this end-all solution is called TURN. STUN is a web service you can query to determine your NAT type and public IP (as opposed to parsing cmyip.com). Just some terms you'll encounter along your personal research.
Playing around with these concepts help. Here are some resources to get your started:
Types of NATs: http://think-like-a-computer.com/2011/09/16/types-of-nat/
A Method for Symmetric NAT Traversal (basically by spamming a lot more packets to a lot more ports): http://www.goto.info.waseda.ac.jp/~wei/file/wei-apan-v10.pdf
Lidgren Networking Library (which has NAT traversal built-in, this is a C# library): http://code.google.com/p/lidgren-network-gen3/w/list
In most realistic cases, you send data to the other device by sending the data to an intermediate device from which the other phone picks it up. Can can use protocols like TCP or UDP directly between the two devices, and that may or may not work depending on the protocols and how the devices are getting Internet access.
For example, one device can send an email and the other device can retrieve that email. You can generalize that model to other types of communication.
I am implementing a mobile chat application, I am using socket connection for implementing p2p connectivity. My chat is working fine with android devices. My issue is I can connect a device in 3g network or with in the same WiFi network but the connection is not working when a device form outside WiFi try to connect a device in the WiFi network. I know it's because of the local IP of the device assigned by the WiFi. How can I root and connect a device in the WiFi?
Sounds more like a router settings issue, than an app issue (meaning that users of your app would need to do this for their own networks as well).
Your wifi router needs to know how to forward communication to your device. So say that your app is connecting on port 1234, then you need to tell your router to forward communication from the outside on port 1234 to the internal IP of your phone in your wifi network.
The phone that is outside of the network should target your network's external IP and port 1234.
Sajan, your issue is not only an IP mapping issue, but also a NAT traversal issue. It is not always possible to punch holes in NATs. And when it is, it must be done with some sophisticated techniques including a super peer located outside of your wifi/lan which will read the translated address from your inside peer.
Unless you are using UDP and the NAT is friendly, it is not possible to send TCP communication directly to the inside peer. In most case, you'll have to check what is possible with the NAT, and if there is something possible, you'll need to perform mapping prediction and tell both peer to attempt com on predicted IP addresses.
That's valid if you don't want you users to have to configure their NATs. And even in that case, such configuration will not always be enough (if there are cascades of NATs, or proxies for example).
I am trying to make an application which can connect from a mobile to a wifi controller. Planning to do a socket connection(multicast) ... The only values that i am able to get through the wifi android apis are SSID and BSSID.. Can anyone tell me if i am supposed to proceed with these values or am i supposed to connect with an IP address ? If i am supposed to connect with BSSID.. how do i go about it?
Wifi access points need not have an IP address, any more than wired switches and hubs do. However a large number do, as they also act on the IP layer as DHCP servers and network gateways.
What exactly are you trying to do? It sounds likely that it is not the Wifi access point proper that you need to talk to.
If it supports TCP and UDP, it will of course have an IP address. This will not be directly discoverable from the Wifi APIs, and you'll need some other means of discovering that. It's hard to say what exactly will be a reasonable method to do this without knowing what the AP is, and what services it provides.
Common ways of enumerating services provided on a network include DHCP; multicast DNS/DNS service discovery which is used by "Bonjour" and "Zeroconf"), SSDP and uPnP, and of course static configuration.
I'm not familiar enough with the Android network APIs to recommend anything, though http://jmdns.sourceforge.net/ appears to be a usable Java multicast DNS library.