Java ServletContextListener 实现UDP监听

摘要: 使用spring boot实现项目启动时的监听, UDPListener import java.io.IOException;import java.io.UnsupportedEncodingException;import java.net.DatagramPacket;import java阅读全文

使用spring boot实现项目启动时的监听,

UDPListener

1
	

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

1
	

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
	@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

1
2
3
4
5
6
7
8
	@SpringBootApplication
@ServletComponentScan
public class DemoApplication {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
1
@ServletComponentScan Servlet扫描,启动时把servlet、filter、listener自动扫描注入 UDP测试客户端(也可以用测试工具发送UDP包测试):
1
	

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	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();
}
}

}

1
结果:

1