I am working on an Android application which sends a udp broadcast message to the devices on LAN. Application works well on the device and also on the emulator running on Windows. However, I couldn't get it working on Linux and Mac Android Emulators.
Since 255.255.255.255 is not working on android, I calculate the broadcast ip by using the subnet mask. (broadcast ip in my case is 192.168.1.255 where host ip is 192.168.1.88/24). But the message is never sent. Also checked with Wireshark but seems like the udp package does not get outside of the emulator.
Is there anyone who achieved to send broadcast messages on Linux Android Emulator?
Any advice will be highly appreciated.
Could you give a sample of your code? It is important when using UDP instead of TCP to use a DatagramSocket with DatagramPackets in stead of a standard Socket or an SSLSocket (or any other kind of Socket for that matter). It seems like that might be what you're doing because it sometimes works, but it might be work trying. If that is the problem and you want any advice, this should suffice:
http://developer.android.com/reference/java/net/DatagramSocket.html
Related
I cannot figure out how to send a UDP packet over a 4G LTE network from an Android app (no WiFi). Does anyone have any ideas or know where I can find some documentation? I need to write two android apps, one client and one server, so that I can send UDP packets with timestamps from one device to the other. This is to test 4G LTE network latency from device to device.
Please help!
Thanks!
You can use the DatagramSocket class:
http://developer.android.com/reference/java/net/DatagramSocket.html
This should work regardless of WiFi or mobile connection type, although you may find some mobile operators have some restrictions on UDP connections.
There are many existing Android Iperf apps that can be used for exactly what you are trying to do. I use Magic Iperf to do this type of testing myself.
I'm making an application which needs to communicate to another app on another device. The only problem is that the IP addresses from the devices aren't allways the same. I want to client to find the server on a specific port, but how can I find devices on the network which have this port opened without me having to enter the server's ip on the client side? I've found Android's NsdManager, but that works from API level 16 and on. I'm developing with level 10.
Thanks in advance!
You can use Zero-configuration networking which can help to solve your problem.Have a look at following websites-http://www.multicastdns.org/ and http://en.wikipedia.org/wiki/Zero-configuration_networking.
You can also make use MDSND, check Bonjour implementation on Android for more info.
My apologies for not responding.
I solved this problem by broadcasting an UDP packet to all devices (255.255.255.255). Al devices listening in the specified port will respond and thereby the client will now know the server's IP address. TCP is used for further communication.
This shouldn't be too difficult.
The private ip address in question should be of the form 192.168.1.x (x being between 1 and 255)
And normally, I believe that number is assigned sequentially, so first I would check if
192.168.1.1 if not that one,
then I'd check
192.168.1.2 then
192.168.1.3 then
etc.
Broadcasting works in most cases, the link below shows how to do it from code:
[Send Broadcast UDP but not receive it on other Android devices
But there are exceptions:
Some phones doesn't recevie broadcasting package correcttly(HTC for example), i solved this problem by broadcasting from HTC phone and once other phone recevied the package, send a udp package to HTC phone(not through broadcasting)
If one of the device is act as hotspot, the broadcasting seems doesn't work at all, in this case, other devcie can try use the gateway ip(which is the hotsport device's ip)
I am using ubuntu 10.04 LTS and have a android ICS device.
I created a wifi-hotspot in my android device. I am connecting the my ubuntu via the wifi-hotspot created above. I stared capturing the packets from wireshark in my pc.
I am unable to see the traffic generated from the device, rather I can see only the traffic from the pc.
Am I missing something or wrong?
You aren't seeing the traffic from the phone because your computer is only receiving and sending packets intended for the computer.
Maybe this will help? http://www.wireshark.org/faq.html#promiscsniff
It is possible that your wireless interface device either isn't in promiscuous mode or doesn't support it.
If i were you, i would scan for all hosts alive in the subnet.Sure android device acts as a hotspot using NAT or something, i'm sure.But the catch is it's moreover a server to which only your pc or whatever might be connected, i guess it would be your lapton. But android itself uses the external ip like service bsnl or something. So it's not at all a client connected to a router or something.
Still if you run an nmap scan you would surely see the default gateway ip, which is of the hotspot.
So, it's enough said right.
I have been able to set up my client/server program so the server runs on the PC and the client on the Android. Either an emulator or a device.
The server receives data from the client, but the client doesn't receive data from the server(all UDP). Eventhough the server is getting the client's address from recvfrom.
I want to make it work in both the Android emulator and the Android device(my phone).
I read that the Android emulator has a virtual router. I tried giving the client the ip 10.0.2.2 when running on the same computer as the server, but that didn't solve it.
There is communication between the client and server, it's just one sided.
Anything I am missing?
I am using Berkley sockets on JNI\C++ for Android, and winsock2 for the PC server.
Edit:
I was able to make it work! So UDP can work on Android both ways!
The issue was that select on winsock2 has the first parameter unused, while on linux\Android it is used and called nfds. Sending NULL to that parameter(like I did in winsock2), basically made select always return 0.
Thanks for the help fge.
I was wondering if you would post a link to source code on the udp packet receiving for android topic, there's a lot of questions with no simple answers regarding it. I myself can't find a simple program for udp packet send from pc to android.
All of the documentation, examples and questions I've seen so far on TCP connections with Android have been between an Android device and a computer. As unreliable as wireless can be, is it possible to make a client-server TCP connection between Android devices over WiFi, and if so, how?
Edit: I guess I should elaborate more on my situation.
My Droid does not respond to ping or accept incoming TCP requests from anything unless I first make the Droid a client and my laptop the server. After this initial connection is established I can then ping from my laptop, or make the Droid a server and my laptop the client. What I can't do is make one Droid the server and another a client, I always get a "No route to host" error.
It feels like there is something blocking incoming connections unless the device initiates a connection to something else, and even then the only request the device will accept is with this device. How can I make my Droid a server that accepts all incoming TCP requests from any device on my local intranet?
You should be able to. Just do like you would do with computer/device connections, except run the client and server code on the devices.
One of the neat things about the Internet is that the Internet doesn't care whether you are establishing a connection between two PC's, a PC and a phone, or two phones. I would look at the Socket documentation, that should be enough to get you rolling.
It's certainly possible. The only problem is figuring out the IP address. I don't expect a phone have a fixed IP... However, if there is a fixed "login" server somewhere in the Web which IP is known to both phones then they can do a handshake via that server and after that continue peer-to-peer.
re; The only problem is figuring out the IP address. I don't expect a phone have a fixed IP.
Try this;
Use the web browser on the phone to log into this website: It should provide you the IP address.
http://whatismyipaddress.com/