Java learning note 52 (network programming: UDP protocol case)

InetAddress class: Represents the IP address in the Inter...

InetAddress class:

Represents the IP address in the Internet, for example:

package demo; import java.net.InetAddress; import java.net.UnknownHostException; public class InetAddressDemo { public static void main(String[] args) throws UnknownHostException { function1(); function2(); } public static void function1() throws UnknownHostException { InetAddress inet = InetAddress.getLocalHost(); System.out.println(inet.getHostName());// Get host name // output:DESKTOP-Q3O8AEO System.out.println(inet.getHostAddress());// Get host IP // output:192.168.87.1 } public static void function2() throws UnknownHostException { // Get other IP InetAddress inet = InetAddress.getByName("www.baidu.com"); System.out.println(inet); // output:www.baidu.com/111.13.100.92 } }

UDP protocol: no connection communication protocol, maximum 64KB, insecure

TCP protocol: connection oriented communication protocol, reliable and secure, three handshakes to confirm the connection, relatively slow, suitable for big data transmission

UDP example:

Sender:

package demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; public class UDPSend { public static void main(String[] args) { try { byte[] data = "Hello".getBytes(); InetAddress inet = InetAddress.getByName("127.0.0.1"); DatagramPacket dp = new DatagramPacket(data, data.length, inet, 6000); DatagramSocket ds = new DatagramSocket(); ds.send(dp); ds.close(); } catch (IOException ex) { System.out.println(ex); } } }

Receiver:

package demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) { try { DatagramSocket ds = new DatagramSocket(6000); byte[] data = new byte[1024]; DatagramPacket dp = new DatagramPacket(data, data.length); ds.receive(dp); int length = dp.getLength(); String ip = dp.getAddress().getHostAddress(); System.out.println(ip + ":" + new String(data, 0, length)); ds.close(); } catch (IOException ex) { System.out.println(ex); } } }

Run the receiver first, then the sender. The result is as follows:

Output: 127.0.0.1: Hello

The function can be improved to realize the chat of keyboard input:

Sender:

package demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.util.Scanner; public class UDPSend { public static void main(String[] args) { try { Scanner sc = new Scanner(System.in); DatagramSocket ds = new DatagramSocket(); InetAddress inet = InetAddress.getByName("127.0.0.1"); while (true) { String message = sc.nextLine(); byte[] data = message.getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length, inet, 7000); ds.send(dp); } } catch (IOException ex) { System.out.println(ex); } } }

Receiving end:

package demo; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class UDPReceive { public static void main(String[] args) { try { DatagramSocket ds = new DatagramSocket(7000); byte[] data = new byte[1024]; while (true) { DatagramPacket dp = new DatagramPacket(data, data.length); ds.receive(dp); int length = dp.getLength(); String ip = dp.getAddress().getHostAddress(); System.out.println(ip + ":" + new String(data, 0, length)); } } catch (IOException ex) { System.out.println(ex); } } }

Running successively, messages sent at the sending end can be received at the receiving end at any time, so a simple chat function is realized here

Under the same LAN, multiple computers can also chat, as long as the corresponding IP address can be modified

5 May 2020, 03:36 | Views: 8905

Add new comment

For adding a comment, please log in
or create account

0 comments