这篇文章主要介绍了Java基于ServletContextListener实现UDP监听,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
使用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==&#