An example analysis of UDP sending for the development of socket programming in Android _android

Source: Internet
Author: User

The example of this article describes the implementation of UDP for the development of socket programming in Android. Share to everyone for your reference. The specific analysis is as follows:

Need to implement the function: the use of UDP socket programming, when pressed the confirmation key, the simulator to send text box data, PC computer network debugging assistant to receive

First, the environment:

Win7 + eclipse + SDK

Second, code:

Package test.soket; Import Com.test_button. 
R 
Import Java.io.DataOutputStream; 
Import java.io.IOException; 
Import Java.net.DatagramPacket; 
Import Java.net.DatagramSocket; 
Import java.net.InetAddress; 
Import Java.net.Socket; 
Import java.net.SocketException; 
Import java.net.UnknownHostException; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import Android.view.View; 
Import Android.widget.Button; 
Import Android.widget.TextView; 
 public class Test_socket extends activity {public static TextView show; 
 public static Button Press; 
 public static Boolean flag; 
 private static final int max_data_packet_length = 40; 
 Private byte[] buffer = new Byte[max_data_packet_length]; 
 Private Datagrampacket Datapacket; 
 Private Datagramsocket Udpsocket; /** called the activity is a. 
  * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Setcontentview (R.layout.main); Open control Space show = (TextView) Findviewbyid (R.ID.EDITTEXT1); 
  Press = (Button) Findviewbyid (R.id.button1); 
  Flag = false; 
  Soket_send thread = new Soket_send (); 
  Thread.init (); 
  Thread.Start (); 
  try {udpsocket = new Datagramsocket (5554); 
  catch (SocketException e) {//TODO auto-generated catch block E.printstacktrace (); 
  } datapacket = new Datagrampacket (buffer, max_data_packet_length); String str = "HELLO,JDH"; This is the data to be transmitted byte out [] = Str.getbytes (); 
  The transmission content is decomposed into byte datapacket.setdata (out); 
  Datapacket.setlength (out.length); 
  Datapacket.setport (5554); 
   try {inetaddress broadcastaddr = inetaddress.getbyname ("192.168.0.248"); 
   Datapacket.setaddress (BROADCASTADDR); 
  Udpsocket.send (Datapacket); 
  catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); 
    Press.setonclicklistener (New Button.onclicklistener () {@Override public void OnClick (View v) { 
    Flag = true; 
   /* String str = "HELLO,JDH"; This is the data to be transmitted byte out [] = Str.getbytes (); 
    The transmission content is decomposed into byte datapacket.setdata (out); 
    Datapacket.setlength (out.length); 
    *//Get input box text charsequence str =test_socket.show.gettext (); 
    byte out[] = Str.tostring (). GetBytes (); 
    Datapacket.setdata (out); 
    Datapacket.setlength (out.length); 
     try {inetaddress broadcastaddr = inetaddress.getbyname ("192.168.0.248"); 
     Datapacket.setaddress (BROADCASTADDR); 
    Udpsocket.send (Datapacket); 
    catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); 
 } 
   } 
  }); } 
}

The interface is as follows:

Note: In the emulator IP is native IP, the port is the emulator name

I hope this article will help you with your 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.