UDP Receive problem

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

UDP Receive problem

Postby micksear » Tue Jun 30, 2009 11:43 am

I'm trying to develop a small app that can receive UDP data on a certain port. I've read http://www.anddev.org/udp-networking_-_ ... -t280.html, but this happens within the emulator for both client and server. I'm trying to send data from my PC to the emulator, and it doesn't seem to get as far as the emulator (it's hard to tell for sure)

I have the following very primitive code running in the emulator:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.  
  3.                         int bufSize = 4096;
  4.  
  5.                         DatagramSocket sock = new DatagramSocket(port);
  6.  
  7.                         sock.setReceiveBufferSize(bufSize);
  8.  
  9.                         byte[] buffer = new byte[bufSize];
  10.  
  11.  
  12.  
  13.                         while (true) {
  14.  
  15.                                 DatagramPacket p = new DatagramPacket(buffer, bufSize);
  16.  
  17.                                 sock.receive(p);
  18.  
  19.                                 Log.d("Client", "Received: " + new String(p.getData()));
  20.  
  21.                         }
  22.  
  23. ...
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


And I'm using the following code to send packets into the emulator:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                         DatagramSocket sock = new DatagramSocket();
  2.  
  3.                         DatagramPacket packet = new DatagramPacket(
  4.  
  5.                                         msg.getBytes(),
  6.  
  7.                                         msg.getBytes().length,
  8.  
  9.                                         InetAddress.getByName(addr),
  10.  
  11.                                         port);
  12.  
  13.                        
  14.  
  15.                         sock.send ( packet );
  16.  
  17.                         System.out.println ("Sent " + msg + " to " + addr +":" + port);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


I'm invoking this with something like 'java UDPSenderHarness 127.0.0.1 35123"Hello, Android"
, so localhost, and port 35123

My app's manifest specifies internet with :



Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.     <uses-permission android:name="android.permission.INTERNET" />
  2.  
  3. </manifest>
  4.  
  5.  
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Can anyone shed some light on why I'm not receiving UDP data into the emulator?

Thanks,
Mick
micksear
Freshman
Freshman
 
Posts: 2
Joined: Tue Jun 30, 2009 11:31 am

Top

Postby micksear » Thu Jul 02, 2009 11:28 am

Can anyone help me with this? Is there some way to monitor what data is being received into the emulator or something like that? Am I doing something unsupported?

Cheers,
Mick
micksear
Freshman
Freshman
 
Posts: 2
Joined: Tue Jun 30, 2009 11:31 am

I have the same problem

Postby The_CROW » Sat Sep 12, 2009 7:59 am

Hi,

I have the same problem, when i use the client as a java application it works, but when i use it in Android within the emulator no data was received !!!

Any answer for this problem??

thanks
Best Regards
The_CROW
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Sep 12, 2009 7:56 am
Location: France

Postby p4squ1 » Mon Oct 26, 2009 4:42 pm

UP
p4squ1
Freshman
Freshman
 
Posts: 8
Joined: Fri May 15, 2009 10:18 pm
Location: Italy

Postby p4squ1 » Tue Oct 27, 2009 4:28 pm

I think that the answer is that UDP don't work with android emulator when the client isn't within the emulator!!:-(
p4squ1
Freshman
Freshman
 
Posts: 8
Joined: Fri May 15, 2009 10:18 pm
Location: Italy

Postby Pierre » Mon Nov 16, 2009 2:57 am

same problem here !
my udp packets are truncated !

Try with the new Android emulator 2.0.
It looks that this bug is fixed :/

But some people can receive UDP packets, and i don't understand why :(
Pierre
Freshman
Freshman
 
Posts: 5
Joined: Mon Nov 16, 2009 2:54 am

Top

Postby Dzung » Tue Nov 24, 2009 10:28 am

I also got this problem. I try adb forward and got "cannot bind socket" error. I try redir with no avail, either. I am using window. Can anybody successfully send UDP packet to emulator?

@Pierre: Android 2.0 does not resolve this problem. If it does, please tell me how. Thank you.
Dzung
Freshman
Freshman
 
Posts: 4
Joined: Tue Nov 24, 2009 10:24 am

Postby Dzung » Tue Nov 24, 2009 2:23 pm

Finally, I managed to resolve this bug. Here are the steps you need to follow to send UDP packet to emulator

1) Run your emulator

2) Open command line window and type:

telnet localhost 5554

where 5554 is the port of your emulator. If you are using Window and get "cannot bind socket", you need to turn on the telnet feature.

It will lead you to the emulator control console.

3) In this console, type

redir add udp:5000:5000

4) Suppose A is your PC and B is your emulator and you want to send a packet from A to B.

On your PC, create a datagram packet with IP address 127.0.0.1 and port 5000:

InetAddress serverAddr = InetAddress.getByName(127.0.0.1);
DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, 5000);

On your emulator, create a DatagramSocket with IP address 10.0.2.15 and port 5000

InetAddress serverAddr = InetAddress.getByName(10.0.2.15);
DatagramSocket socket = new DatagramSocket(5000, serverAddr);

Now you can start your server on emulator and send packet from your PC to emulator

5) How does it work?

Ports on emulator are different from port on your local machine. If you want to connect port from your local machine with emulator ports, you need to redirect packet using redir command above.

Application on emulator communicate with the outside network through emulator's virtual IP address 10.0.2.15. When you send a packet from A to B, this packet will go into the port 5000 of local loop back 127.0.0.1. The emulator will then forward packet from port 5000 of local machine to its virtual port 5000 with virtual address 10.0.2.15. B, having listened to this port, collects the arriving packet.

The reason why you can create TCP connection without any trouble is because TCP is a reliable transport protocol and the emulator is responsible for redirecting and forwarding packets. UDP, in contrast, is an unreliable one and you have to handle it yourselves.

Hope this help.

Dzung Pham.
Dzung
Freshman
Freshman
 
Posts: 4
Joined: Tue Nov 24, 2009 10:24 am

Postby jpena » Wed Dec 02, 2009 8:19 am

Thanks a lot. UDP problem solved.
jpena
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 02, 2009 8:18 am

Postby aparna.gopinath » Thu Mar 18, 2010 1:56 pm

Thanks !!!! Problem solved ... I am trying to run the same on a hardware platform .... i.e . The PC sends data and the emulator is able to receive it successfully ( I can see it in the logcat ) ... However when I try to run it on the hardware ,i.e PC sending and hardware receiving .. the socket is being created , it is waiting to receive data as well ... however , no data is being received in spite of sending it from the system :( .... I have not done any port forwarding in the case of the hardware but did it for the emulator .. Is it necessary ?? What could be the possible bottlenecks ?? If I want the emulator to communicate with a system apart from the host system .. How do I do it ?? I would be grateful for any kind of help ...


Thanks and Regards ,

Aparna
aparna.gopinath
Freshman
Freshman
 
Posts: 5
Joined: Thu Jul 23, 2009 5:59 am

Re: UDP Receive problem

Postby russoue » Tue Jun 01, 2010 10:51 pm

Will the problem occur if both the sender and receiver of the UDP packet is running in the emulator?
russoue
Freshman
Freshman
 
Posts: 7
Joined: Fri Nov 16, 2007 6:12 pm

Re: UDP Receive problem

Postby tommypincha » Sun Sep 26, 2010 1:39 am

Hi, excelent tutorial above.
But im'm having a java.net.BindException: Address already in use: Cannot bind... exception when trying to send a packet from PC to emulator, this exception is caused by the binding of the port in telnet, but i don't know how to solve it.

Thanks in advance.

P.D: If I need to send from emulator to PC, is the same way?
tommypincha
Freshman
Freshman
 
Posts: 3
Joined: Sun Sep 26, 2010 1:31 am

Re: UDP Receive problem

Postby Dzung » Mon Sep 27, 2010 4:49 am

You can use other port to resolve this. Listening port on emulator (from PC) does not have to be the same with listening port of the program inside the emulator.
Dzung
Freshman
Freshman
 
Posts: 4
Joined: Tue Nov 24, 2009 10:24 am

Re: UDP Receive problem

Postby tommypincha » Tue Sep 28, 2010 12:16 am

I'm sorry, i don't quite understand you. If change the port of my PC to send packets to a 'newport' I must bind it using telnet (e.g. "redir add udp:5000:newport") returning to the same binding exception still appears.

This is briefly what i'm doing (without changing ports):

Code: Select all
/* SERVER */
DatagramSocket socket = new DatagramSocket(5000, InetAddress.getByName("10.0.2.15");
byte[] buf = new byte[17];
DatagramPacket packet = new DatagramPacket(buf, buf.length);

/* CLIENT */
DatagramSocket socket = new DatagramSocket(5000, InetAddress.getByName("127.0.0.1");
byte[] buf = ("Hello from Client").getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length,   InetAddress.getByName("127.0.0.1"), 5000);


Any correction in my piece of code will be much appreciated, i hate being stuck in something so stupid like this!

Thank you
tommypincha
Freshman
Freshman
 
Posts: 3
Joined: Sun Sep 26, 2010 1:31 am

Re: UDP Receive problem

Postby Dzung » Tue Oct 05, 2010 4:53 am

@tommypincha: I think you should use different ports for server & client. Otherwise you will get an exception on port binding. Is there any reason why you have to use the same ports for both?

Another alternative is to use address 10.0.2.2 on emulator. This makes your work much easier as you do not have to use the telnet. You can refer to the Android Doc:

Referring to localhost from the emulated environment

If you need to refer to your host computer's localhost, such as when you want the emulator client to contact a server running on the same host, use the alias 10.0.2.2 to refer to the host computer's loopback interface. From the emulator's perspective, localhost (127.0.0.1) refers to its own loopback interface.


Common Tasks and How to Do Them in Android
Dzung
Freshman
Freshman
 
Posts: 4
Joined: Tue Nov 24, 2009 10:24 am

Top
Next

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 5 guests