demo based on UDP sending messages to each other

I. demo function and significance This demo is mainly to demonstrate the application mode based on UDP protocol. It consists of two sections of code:...

I. demo function and significance

This demo is mainly to demonstrate the application mode based on UDP protocol. It consists of two sections of code:

(1) the role of udpprovider is to receive the data sent by UDPSearcher and send a string data back to UDPSearcher

(2) the role of udpsearcher is to send a piece of data to UDPProvider and receive the data sent from UDPProvider

II. demo workflow

III. demo source code

(1) UDPProvider end code

package socket.SocketDemo2.demo1; import socket.SocketDemo2.MessageCreator; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; /** * Created by lifuqing on 2019/4/8 15:39 * Email : [email protected] */ public class UDPProvider { public static void main(String [] args) throws IOException { System.out.println("UDPProvider Started."); // Listen to 20000 ports DatagramSocket ds = new DatagramSocket(20000); // Build receiving entity final byte[] buf = new byte[512]; DatagramPacket receivePack = new DatagramPacket(buf, buf.length); // Receive data and place it in the entity receivePack ds.receive(receivePack); // Print received information and sender's information // Sender's IP address String ip = receivePack.getAddress().getHostAddress();//Get IP address from entity int port = receivePack.getPort();//port int dataLen = receivePack.getLength();//Size String data = new String(receivePack.getData(), 0, dataLen);//Get data //output System.out.println("UDPProvider receive form ip:" + ip + "\tport:" + port + "\tdata:" + data); //Build a loopback data String responseData = "Receive data with len:"+data.length(); byte[] responseDataBytes = responseData.getBytes(); // Build a loopback message directly from the sender DatagramPacket responsePacket = new DatagramPacket(responseDataBytes, responseDataBytes.length, receivePack.getAddress(), receivePack.getPort()); ds.send(responsePacket); System.out.println("UDPProvider Finished."); ds.close(); } }

(2) UDPSearcher end code

package socket.SocketDemo2.demo1; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; /** * Created by lifuqing on 2019/4/8 15:39 * Email : [email protected] */ public class UDPSearcher { public static void main(String [] args) throws IOException { System.out.println("UDPSearcher Started."); // As a searcher, you do not need to specify a port DatagramSocket ds = new DatagramSocket(); //Build a request data String requestData = "Hello world"; byte[] requestDataBytes = requestData.getBytes(); // Build a loopback message directly from the sender DatagramPacket responsePacket = new DatagramPacket(requestDataBytes, requestDataBytes.length); responsePacket.setAddress(InetAddress.getLocalHost()); responsePacket.setPort(20000); //Send out ds.send(responsePacket); // Build receiving entity final byte[] buf = new byte[512]; DatagramPacket receivePack = new DatagramPacket(buf, buf.length); // Receive data and place it in the entity receivePack ds.receive(receivePack); // Print received information and sender's information // Sender's IP address String ip = receivePack.getAddress().getHostAddress();//Get IP address from entity int port = receivePack.getPort();//port int dataLen = receivePack.getLength();//Size String data = new String(receivePack.getData(), 0, dataLen);//Get data //output System.out.println("UDPSearcher receive form ip:" + ip + "\tport:" + port + "\tdata:" + data); //complete System.out.println("UDPSearcher Finished."); ds.close(); } }

Four, summary

This paper introduces the simplest demo of UDP application, and draws a demo workflow chart to sort out the UDP workflow. If there is any deficiency, please give us more advice

3 December 2019, 12:49 | Views: 8307

Add new comment

For adding a comment, please log in
or create account

0 comments