Android Learning notes-network programming

Source: Internet
Author: User

1. Transmitting data using the UDP protocol

The UDP protocol is unreliable and cannot be determined by the receiving party after the packet is sent out

Write the server in Java as follows.

Package com.umgsai.server;import java.net.datagrampacket;import java.net.datagramsocket;import  java.util.Calendar;import java.util.Date;public class Server {static Date  date ;p Ublic static void main (String[] args)  {while  (True)  {try {   Create a Datagramsocket object and specify the port number to listen for Datagramsocket socket = new datagramsocket (4572); byte data[] = new byte[1024];//  Create an empty Datagrampacket object Datagrampacket packet  = new datagrampacket (data, data.length);//  uses the Receive method to receive data sent by the client socket.receive ( packet); String result = new string (Packet.getdata (), Packet.getoffset (),  packet.getLength ()); Date = calendar.getinstance (). GetTime (); SYSTEM.OUT.PRINTLN (date); SYSTEM.OUT.PRINTLN ("Result--->"  + result); Socket.close ();}  catch  (exception e)  {e.printstacktrace ();}}}

The client is written inside Android

Interface Activity_main.xml

<button android:id= "@+id/sendmessage" android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "Send Message"/>

The operation of the connection network in the android4.0+ operating system cannot be performed in the main thread

Public class mainactivity extends activity {private button sendmessage  = null; @Overrideprotected  void oncreate (bundle savedinstancestate)  { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);sendmessage =  (Button)  findviewbyid (R.id.sendmessage); Sendmessage.setonclicklistener (New clientlistener ());} class clientlistener implements onclicklistener{@Overridepublic  void onclick (View &NBSP;V)  {handlerthread handlerthread = new handlerthread ("Handler_Thread");             handlerthread.start ();             ClientHandler clientHandler = new  ClientHandler (Handlerthread.getlooper ());             message msg = clienthandler.obtainmessaGE ();            bundle bundle =  New bundle ();             msg.setdata (Bundle);             //  sends MSG to the target object, which is the handler object that generates the MSG object             msg.sendtotarget ();}} class clienthandler extends handler {         Public clienthandler ()  {         }          public clienthandler (Looper looper)  {             super (Looper);         }          @Override          public void handlemessaGE (message msg)  {             Super.handlemessage (msg);            bundle  Bundle = msg.getdata ();             try  {//  first create a Datagramsocket object Datagramsocket socket = new datagramsocket (4572);//   Create a inetaddreeinetaddress serveraddress = inetaddress.getbyname ("192.168.77.215"); string str =  "Hello"; byte data[] = str.getbytes ();//  Create a Datagrampacket object, and specify which address this packet sends to the network, and the port number Datagrampacket packet = new datagrampacket (Data,data.length,  serveraddress, 4572);//  Call the socket object's Send method, send data socket.send (packet); Socket.close (); Toast.maketext (mainactivity.this,  "Data Sent",  toast.length_short). Show ();  catch  (exception e)  {// todo: handle excepTione.printstacktrace ();}         }    }}

Permissions required by the client

<uses-permission android:name= "Android.permission.INTERNET"/>


This article is from "Avatar" blog, please make sure to keep this source http://shamrock.blog.51cto.com/2079212/1582887

Android Learning notes-network programming

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.