/**
* 启动项目后就创建Server Socket服务
*/
@PostConstruct
public void runServerSocket() {
try {
ExecutorService executorService = Executors.newFixedThreadPool(10);// 创建线程池
ServerSocket serverSocket = new ServerSocket(9090);// 在设备上配置的服务端监听端口为9090
executorService.execute(()->{
while (true) {
try {
Socket socket = serverSocket.accept();
InputStream in = socket.getInputStream();// 获取输入流,用来读取数据
byte[] data = new byte[128];
in.read(data);
String dataStr = new String(data,"utf-8");
System.out.println(dataStr);
in.close();
socket.close();
executorService.shutdown();
} catch (Exception e) {
executorService.shutdown();
} finally {
executorService.shutdown();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}