Android-socket Transmission GPRS Network

Source: Internet
Author: User

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