Android's TCP and UDP transport simple program

Source: Internet
Author: User
Tags connect socket

TCP UDP commonly used network communication mode, the specific content and transmission mode can be Baidu search here no longer repeat,

The main thing I do is to send out the source code, for everyone's reference.

First, after the TCP establishes the connection, both sides of the communication can carry on the data transmission simultaneously, secondly, he is full duplex, in the guarantee reliability, uses the time-out retransmission and the piggyback confirmation mechanism.

Common TCP connection diagrams

Server-side code

    try {Boolean Endflag = false;                  ServerSocket ss = new ServerSocket (12345);                      while (!endflag) {//waits for client to connect Socket s = ss.accept ();                      BufferedReader input = new BufferedReader (Newinputstreamreader (S.getinputstream ())); Note that the second parameter data is true will automatically flush, otherwise you need to manually operate Output.flush () PrintWriter output = Newprintwriter (s.getoutputstre                      AM (), true);                      String message = Input.readline ();                      LOG.D ("Tcp Demo", "Message from Client:" +message);                      OUTPUT.PRINTLN ("message received!");                      Output.flush ();                      if ("ShutDown". Equals (Message)) {endflag=true;                  } s.close ();                     } ss.close ();  } catch (Unknownhostexception e) {e.printstacktrace ();            } catch (IOException e) {e.printstacktrace ();  }
Client code:


    Try {                  socket s = new socket ("localhost", 12345);                  Outgoing stream redirect to socket                  OutputStream out = S.getoutputstream ();                  Note that the second parameter data is true will automatically flush, otherwise you need to manually operate Out.flush ()                  printwriter output = new PrintWriter (out, true);                  Output.println ("Hello ideasandroid!");                  BufferedReader input = new BufferedReader (Newinputstreamreader (S                          . getInputStream ()));                  Read line (s)                  String message = Input.readline ();                  LOG.D ("Tcp Demo", "Message from Server:" + message);                  S.close ();                     } catch (Unknownhostexception e) {                  e.printstacktrace ();              } catch (IOException e) {                  e.printstacktrace ();              
UDP Connection Diagram

UDP server-side code:

The UDP server listens on ports Integer port = 12345;              The size of the bytes received, the data sent by the client cannot exceed this size byte[] message = new byte[1024];                  try {//Establish socket connection Datagramsocket datagramsocket = new Datagramsocket (port);                  Datagrampacket datagrampacket = new Datagrampacket (message, message.length); try {while (true) {//Prepare to receive data Datagramsocket                          . receive (Datagrampacket);                                  LOG.D ("UDP Demo", datagrampacket.getaddress (). Gethostaddress (). ToString ()                      + ":" + New String (Datagrampacket.getdata ()));                  }} catch (IOException e) {e.printstacktrace ();              }} catch (SocketException e) {e.printstacktrace (); }

Client code:

public static void Send (String message) {message = (message = = null?)              "Hello ideasandroid!": message);              int server_port = 12345;              Datagramsocket s = null;              try {s = new datagramsocket ();              } catch (SocketException e) {e.printstacktrace ();              } inetaddress local = null;              try {//Replace server-side IP local = inetaddress.getbyname ("localhost");              } catch (Unknownhostexception e) {e.printstacktrace ();              } int msg_length = Message.length ();              byte[] Messagemessagebyte = Message.getbytes ();              Datagrampacket p = new Datagrampacket (Messagebyte, Msg_length, Local, server_port);              try {s.send (P);              } catch (IOException e) {e.printstacktrace (); }          }



Android's TCP and UDP transport simple program

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.