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