UDP: Unreliable, high efficiency, datagram/non-connected
Demo_1:
Server side:
Import Java.io.ioexception;import java.net.*;p ublic class Testudpserver {public static void main (string[] args) {byte buf [] = new Byte[1024];try {datagramsocket ds = new Datagramsocket (5678);D atagrampacket dp = new Datagrampacket (buf, Buf.leng th); while (true) {try {ds.receive (DP); System.out.println (New String (Buf,0,dp.getlength ()));} catch (IOException e) {e.printstacktrace ();}}} catch (SocketException e) {e.printstacktrace ();}}}
Client side:
Import Java.io.ioexception;import java.net.*;p ublic class Testudpclient {public static void main (string[] args) {byte[] b UF = (New String ("Hello java")). GetBytes ();D atagrampacket dp = new Datagrampacket (buf, Buf.length, New Inetsocketaddress ( "192.168.56.1", 5678));D atagramsocket ds;try {ds = new Datagramsocket (9999); try {ds.send (DP);} catch (IOException e) {E.P Rintstacktrace ();} Ds.close ();} catch (SocketException e) {e.printstacktrace ();}}}
Operation Result:
Network Programming in Java-3