UDP Applications in Java

Source: Internet
Author: User

I implemented the Chat Room Group Chat window by complying with the TCP protocol ServerSocket and sockets in the Javase project chat room. At the same time, when introducing the OSI and TCP/IP reference Model, we also mentioned TCP and UDP (full name user Datagram Protocol, Subscriber Datagram Protocol).

In general, the difference between TCP and UDP is:

The basic difference between 1.TCP and UDP:

(1) TCP-based connection, UDP based on no connection;

(2) TCP requires more system resources, and UDP is less;

(3) The structure of the UDP program is relatively simple;

(4) Stream mode (TCP) and datagram Mode (UDP);

(5) TCP guarantees data correctness, UDP may drop packets;

(6) TCP guarantees data order, UDP is not guaranteed.

2.UDP scenarios include:

(1) For the data reporting method;

(2) network data are mostly short messages;

(3) Have a large number of client;

(4) No special requirements for data security;

(5) The network burden is very heavy, but the response speed requirements are high.

3. Specific programming Differences:

(1) Socket socket parameters are different;

(2) UDP does not need to call listen and accept;

(3) UDP send and receive data using the Send () and receive () method;

(4) TCP: Address information is determined at connect and accept;

(5) UDP: The address information must be specified each time in the Send () and receive () methods.

I then demonstrate the UDP protocol through the code. I wrote two classes, namely UDPA and UDPB, to send messages to UDPB through UDPA.

The code in Udpa.java is as follows:

Import Java.io.ioexception;import Java.net.datagrampacket;import Java.net.datagramsocket;import java.net.inetaddress;/** * UDPA to send UDPB message */public class UDPA {public static void Main (string[] args) throws IOException { Byte[] bs= "I am a, give a message to B." GetBytes ();//message content to be sent UDPA and UDPB ip are native IP, so set a different port number InetAddress Desip = Inetaddress.getlocalhost (); Datagram packet, UDPA port is 10010 Datagrampacket p=new Datagrampacket (BS, Bs.length, Desip, 10010); Create a datagram socket, UDPA port set to 10086 Datagramsocket socket_a=new Datagramsocket (10086); UDPA Send datagrams to UDPB Socket_a.send (P); Close Socket_a sockets Socket_a.close ();}}

The code in Udpb.java is as follows:

import java.io.IOException; 

 

Import Java.net.datagrampacket;import Java.net.datagramsocket;public class UDPB { public static void Main (string[] args) throws IOException { //receiving datagram packets with arrays byte[] Bs=new byte[500]; datagrampacket p=new Datagrampacket (BS, bs.length); //Create datagram Socket datagramsocket socket_b= New Datagramsocket (10010); //Receive datagram packet socket_b.receive (p); System.out.println (new String (BS, 0, P.getlength ())); //close socket socket_b.close (); }}

When the program runs, the UDPB is run, waiting to receive the information sent by UDPA, and then run UDPA, UDPB receive the information UDPA.

UDP Applications in Java

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.