Simple TCP and UDP transmission programs for Android

Source: Internet
Author: User

Simple TCP and UDP transmission programs for Android

The network communication mode that is often used for tcp udp. The specific content and transmission mode can be searched by Baidu,

I am mainly sending the source code for your reference.

First, after the TCP connection is established, both parties can transmit data at the same time, and secondly, it is full-duplex. In terms of reliability, timeout retransmission and RST validation mechanisms are used.

Common tcp connection Diagram

Server code <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + PHN0cm9uZz48L3N0cm9uZz48cHJlIGNsYXNzPQ = "brush: java;"> try {Boolean endFlag = false; ServerSocket ss = new ServerSocket (12345); while (! EndFlag) {// wait for the client to connect to Socket s = ss. accept (); BufferedReader input = new BufferedReader (newInputStreamReader (s. getInputStream (); // note that if the second parameter is set to true, it will be automatically flush; otherwise, you need to manually operate the output. flush () PrintWriter output = newPrintWriter (s. getOutputStream (), true); String message = input. readLine (); Log. d ("Tcp Demo", "message from Client:" + message); output. println ("message received ed! "); // 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 if the second parameter is set to true, it will be 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 code:

// UDP server listening port Integer port = 12345; // the size of the received byte. The data sent by the client cannot exceed this size byte [] message = new byte [1024]; try {// establish a 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 it with the server IP address 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 ();}}



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.