Mobile phone using GPRS network and server socket Communication, code: http://download.csdn.net/detail/wu20093346/7768481
Use the UDP protocol with the Socket debug tool to test.
Socketactivity.java:
Package Com.example.socket;import Java.net.datagrampacket;import Java.net.datagramsocket;import Java.net.inetaddress;import Android.os.bundle;import Android.app.activity;import Android.view.View;import Android.widget.button;import Android.view.view.onclicklistener;public class Socketactivity extends Activity {private Button Startbutton = null, @Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (r.layout.activity_socket); Startbutton = (Button) Findviewbyid (R.id.startlistener); Startbutton.setonclicklistener (New Startsocketlistener ());} Class Startsocketlistener implements onclicklistener{@Overridepublic void OnClick (View v) {new Serverthread (). Start ();}} Class Serverthread extends Thread{public void run () {try{datagramsocket socket = new Datagramsocket (1234); inetaddress ServerAddress = Inetaddress.getbyname ("115.238.249.92"); String str = "Hello"; byte data[] = Str.getbytes ();D atagrampacket packet = new Datagrampacket (data,data.length,serveraddress,1234); socket.send (packet);} catch (Exception e) {e.printstacktrace ();}}}}
Activity_socket.xml:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Socketactivity "> <button android:id=" @+id/startlistener " android:layout_width=" Wrap_content " android:layout_height= "wrap_content" android:layout_centerhorizontal= "true" android:layout_ Centervertical= "true" android:onclick= "Onclicklistener" android:text= "@string/start"/></ Relativelayout>
The Socket Debug tool creates a UDP server listening 1234 port with a native IP of 115.238.249.92. Install run APK, press the Send button and the server side receives the data.
Note Add in Androidmanifest.xml:
<uses-permission android:name= "Android.permission.INTERNET"/>