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.