Android-Socket transmission GPRS network

Source: Internet
Author: User

Android-Socket transmission GPRS network

 

Use UDP protocol and Socket debugging tools for testing.

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();DatagramPacket packet = new DatagramPacket(data,data.length,serverAddress,1234);socket.send(packet);}catch (Exception e){e.printStackTrace();}}}}
Activity_socket.xml:

 

 

 
  
 

 

The Socket debugging tool creates a UDP Server listening port 1234 with the local IP address 115.238.249.92. Install and run apk, and press Send to receive data from the server.

 

 

 

Note: add the following in AndroidManifest. xml:

 

 

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.