使用spring boot實現項目啟動時的監聽,
UDPListener
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
@WebListener public class UDPListener implements ServletContextListener { public static final int MAX_UDP_DATA_SIZE = 4096; public static final int UDP_PORT = 26666; @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("========UDPListener Initialized========="); try {
// 啟動一個線程,監聽UDP數據報 new Thread(new UDPProcess(UDP_PORT)).start(); } catch (Exception e) { e.printStackTrace(); } } class UDPProcess implements Runnable { DatagramSocket socket = null; public UDPProcess(final int port) throws SocketException { socket = new DatagramSocket(port); } @Override public void run() { // TODO Auto-generated method stub System.out.println("=======UDPProcess======"); while (true) { byte[] buffer = new byte[MAX_UDP_DATA_SIZE]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); try { socket.receive(packet); new Thread(new Process(packet)).start(); } catch (IOException e) { e.printStackTrace(); } } } } class Process implements Runnable { public Process(DatagramPacket packet) throws UnsupportedEncodingException { // TODO Auto-generated constructor stub byte[] buffer = packet.getData();// 接收到的UDP信息,然后解碼 String srt1 = new String(buffer,"GBK").trim(); String srt2 = new String(buffer, "UTF-8").trim(); String srt3 = new String(buffer,"ISO-8859-1").trim(); System.out.println("=======Process srt1 GBK======" + srt1); System.out.println("=======Process srt2 UTF-8======" + srt2); System.out.println("=======Process srt3 ISO-8859-1======" + srt3); } @Override public void run() { // TODO Auto-generated method stub System.out.println("====Process run====="); } } @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("========UDPListener Destroyed========="); } }
DemoApplication
@SpringBootApplication @ServletComponentScan public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
@ServletComponentScan Servlet掃描,啟動時把servlet、filter、listener自動掃描注入
UDP測試客戶端(也可以用測試工具發送UDP包測試):
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class UDPClientTest { public static final String SERVER_HOSTNAME = "localhost"; // 服務器端口 public static final int SERVER_PORT = 26666; // 本地發送端口 public static final int LOCAL_PORT = 8888; public static void main(String[] args) { try { // 1,創建udp服務。通過DatagramSocket對象。 DatagramSocket socket = new DatagramSocket(LOCAL_PORT); // 2,確定數據,並封裝成數據包。DatagramPacket(byte[] buf, int length, InetAddress // address, int port) byte[] buf = "你好,世界".getBytes(); DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName(SERVER_HOSTNAME), SERVER_PORT); // 3,通過socket服務,將已有的數據包發送出去。通過send方法。 socket.send(dp); // 4,關閉資源。 socket.close(); } catch (IOException e) { e.printStackTrace(); } } }
結果: