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
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.