Summary of UDP in Java

Source: Internet
Author: User

Let's talk about the InetAddress class, with a small example:

Import Java.net.inetaddress;import Java.net.unknownhostexception;public class Ipdemo {public static void main (string[] args) throws Unknownhostexception {//inetaddress i = inetaddress.getlocalhost (); Get local information//system.out.println (i);   InetAddress i = inetaddress.getbyname ("www.baidu.com"); System.out.println (I.gethostaddress ()); System.out.println (I.gethostname ()); }}

UDP is a non-connected transmission mode, transmission speed, but the size of each transmission can not exceed 64K

How to write UDP?

To send data steps:

1. Creating the Socket Service

2. Create a packet

3. Encapsulate data into a packet, add IP and ports, and data

4. Send

5. Close Resources

To receive data steps:

1. Create a socket service and listen for ports

2. Create a packet to receive data

3. Receiving data into a packet with the socket

4. Extracting data from a packet

5. Close Resources

The

is shown in code below:

Import Java.net.datagrampacket;import java.net.datagramsocket;import java.net.inetaddress;class UdpSend{//1. Create the socket service//2. Transfer data and package//3. Send//4. Close resource public static void main (string[] args) throws Exception {//1. Create socket service Datagramsocket socket = new Datagramsocket (10000);//Specify transmitter Port//2. Transfer data and package byte[] buf = "UDP Hello". GetBytes ();D Atagrampacket packet = new Datagrampacket (buf, Buf.length, Inetaddress.getbyname ("localhost"), 8888);//Specify Application Port// Send Socket.send (packet);//Close Resource socket.close (); System.out.println ("Over");}} /* * 1. Create a service socket and listen on the port * 2. Define a packet to accept the packet * 3. Data received using the socket service is saved to, in the packet * 4. Remove data * 5. Close resource */class udprece{public static Voi D main (string[] args) throws Exception {datagramsocket ds = new Datagramsocket (8888);//port of the specified application byte[] buf = new byte[102 4]; Datagrampacket DP = new Datagrampacket (buf, Buf.length);d s.receive (DP); String IP = dp.getaddress (). gethostaddress (); int port = Dp.getport (); String data = new String (Dp.getdata (), 0,dp.getlength ()); System.out.println (ip+ ":" +port+ ":" +data);d S.closE ();}} public class Udpdemo {public static void main (string[] args) {}}

The

transmits the data one time, and the following is an enhanced version.

Import Java.io.bufferedreader;import Java.io.inputstreamreader;import Java.net.datagrampacket;import Java.net.datagramsocket;import java.net.inetaddress;class send{public static void Main (string[] args) throws Exception {Datagramsocket socket = new Datagramsocket (); BufferedReader reader = new BufferedReader (new InputStreamReader (system.in)); String line =null;while (line = Reader.readline ())!=null) {if (' 886 '. Equals line) {break;} byte[] buf = line.getbytes ();D atagrampacket dp = new Datagrampacket (Buf,buf.length,inetaddress.getbyname ("127.0.0.1")   , 8888); Socket.send (DP);} Reader.close (); Socket.close ();}} Class rece{@SuppressWarnings ("resource") public static void main (string[] args) throws Exception {Datagramsocket socket = New Datagramsocket (8888), while (true) {byte[] buf = new byte[1024];D atagrampacket dp =new datagrampacket (BUF, Buf.length) ; Socket.receive (DP); String data = new String (Dp.getdata (), 0,dp.getlength ()); String IP = dp.getaddress (). gethostaddress (); int port = Dp.getport (); SysteM.out.println (ip+ ":" +port+ ":" +data);}}} public class UdpDemo2 {public static void main (string[] args) {}}

Summary: In fact, the network transmission parts in Java, are encapsulated into objects, very convenient to use, in the network programming is mainly to remember the steps.

Summary of UDP 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.