Analysis of some problems of receiving UDP packets by Android simulator _android

Source: Internet
Author: User

In this paper, some problems of receiving UDP packets by the Android simulator are analyzed. Share to everyone for your reference, specific as follows:

The Android emulator is unable to receive UDP packets

The code is as follows:

Datagrampacket pack = null;
Datagramsocket mail_data = null;
byte receiver[] = new byte[100];
try { 
  pack = new Datagrampacket (receiver,receiver.length); 
  Mail_data = new Datagramsocket (22221); 
  Mail_data.receive (pack); 
catch (Exception e) {
  e.printstacktrace ();
}

The receive function is not receiving data and is waiting.

Workaround:

1. Run simulator

2. open Window command line

Execution: telnet localhost 5554
5554 is the simulator's port and will go into the Android console after execution

3. Execute under Console:

Redir Add udp:22221:22221
Where the first port number is the PC ports and the second port number is the emulator port. (Port mapping)
After executing this command, the data received by PC 22221 Port will be transferred to 22221 ports of the simulator to receive UDP packets from Port 22221.

That is to say: The program UDP receive port on the Android emulator should be set to the 22221,PC server to send UDP packets to the PC's ip+ port 22221.

At this point, the PC's 22221 ports are occupied.

Some problems of receiving UDP packets from Android simulator

Received UDP packet length size

Because the transmission protocol is based on protocol buffer, the packets received are dynamically changing, and the Java packet receives to define the byte[size first, so that the received datagram length becomes fixed, so the received datagram is repeatedly reported when deserializing.

Workaround: use Datagrampacket.getlength () to intercept the actual datagram size, and then redefine a byte[of that size to deserialize it.

public static void Main (string[] args) throws IOException {  
 System.out.println ("Wait for Acceptance");  
 Datagramsocket datagramsocket = new Datagramsocket (8080);//Specify port to receive information  
 byte[] buf = new byte[1024];//Specify the maximum size of the accepted information  
 datagrampacket datagrampacket = new Datagrampacket (buf, 1024);  
 Datagramsocket.receive (datagrampacket);/Blocking wait  
 String string = new String (Datagrampacket.getdata (), 0,  
    Datagrampacket.getlength ())  
    + "from" 
    + datagrampacket.getaddress (). Gethostaddress ()  
    + ":" + Datagrampacket.getport ();  
 System.out.println (string);  
 Datagramsocket.close ();  
}

UDP packet Length limit received

The simulator has a limit on the size of the packet! More than 8k (fuzzy memory is more than 8,000 byte) about the data will not be received.

After the quantitative analysis, is 8193byte, is 8K (8192byte), surpasses the simulator to receive.
Where to be discarded unknown!

The real machine is unlimited.

For more information on Android-related content readers can view the site topics: "The Android Communication method Summary", "Android Debugging techniques and common problem solving method summary", "Android Development introduction and Advanced Course", "Android Multimedia operation tips Summary (audio, Video, audio, etc.), "Summary of Android Basic components usage", "Android View Overview", "Android Layout layout skills Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.