diff --git a/src/main/java/com/zhangmeng/tools/controller/AioClientSocketController.java b/src/main/java/com/zhangmeng/tools/controller/AioClientSocketController.java
new file mode 100644
index 0000000..cbedf4c
--- /dev/null
+++ b/src/main/java/com/zhangmeng/tools/controller/AioClientSocketController.java
@@ -0,0 +1,118 @@
+package com.zhangmeng.tools.controller;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.BufferUtil;
+import cn.hutool.core.lang.Console;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.socket.aio.AioClient;
+import cn.hutool.socket.aio.AioSession;
+import cn.hutool.socket.aio.SimpleIoAction;
+import com.zhangmeng.tools.utils.AlertUtils;
+import javafx.fxml.FXML;
+import javafx.scene.control.Button;
+import javafx.scene.control.TextArea;
+import javafx.scene.control.TextField;
+
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.util.Date;
+
+/**
+ * @author : 芊芊墨客
+ * @version : 1.0
+ * @date : 2023-05-06 10:38
+ */
+public class AioClientSocketController {
+
+ @FXML
+ public TextField ip_address;
+
+ @FXML
+ public TextArea content;
+
+ @FXML
+ public Button connection;
+
+ @FXML
+ public TextField input;
+
+ @FXML
+ public Button send;
+
+ @FXML
+ public TextField port;
+
+ @FXML
+ public Button disconn;
+
+ private AioClient client;
+
+ private AioSession aioSession;
+
+ private void appendMsg(String msg) {
+ content.appendText(System.lineSeparator());
+ content.appendText(msg);
+ }
+
+ @FXML
+ public void initialize() {
+ ip_address.setText("localhost");
+ port.setText("8083");
+ connection.setOnAction(event -> {
+ content.setText(null);
+ if (ip_address.getText().length() == 0 ){
+ AlertUtils.alert_warning("ip不能为空");
+ return;
+ }
+ if (port.getText().length() == 0 ){
+ AlertUtils.alert_warning("端口不能为空");
+ return;
+ }
+
+ client = new AioClient(new InetSocketAddress(ip_address.getText(), Integer.parseInt(port.getText())), new SimpleIoAction() {
+
+ @Override
+ public void doAction(AioSession session, ByteBuffer data) {
+ appendMsg("========>客户端接收" + new Date() + "=======>:" + StrUtil.utf8Str(data));
+ if (data.hasRemaining()) {
+ } else {
+ session.read();
+ }
+ }
+
+ @Override
+ public void accept(AioSession session) {
+ aioSession = session;
+ appendMsg("客户端链接成功!" + session.getRemoteAddress().toString());
+ }
+
+
+ @Override
+ public void failed(Throwable exc, AioSession session) {
+ exc.printStackTrace();
+ super.failed(exc, session);
+ }
+ });
+
+ client.read();
+ });
+
+ send.setOnAction(event -> {
+ send();
+ });
+
+ disconn.setOnAction(event -> {
+ client.close();
+ });
+ }
+
+ private void send() {
+ if (input.getText().length() == 0) {
+ AlertUtils.alert_warning("发送的内容不能为空");
+ return;
+ }
+ String msg = input.getText();
+ client.write(ByteBuffer.wrap(msg.getBytes()));
+ appendMsg("========>客户端发送" + new Date() + "=======>:" + msg );
+ }
+}
diff --git a/src/main/java/com/zhangmeng/tools/controller/AioServerSocketController.java b/src/main/java/com/zhangmeng/tools/controller/AioServerSocketController.java
new file mode 100644
index 0000000..a237d96
--- /dev/null
+++ b/src/main/java/com/zhangmeng/tools/controller/AioServerSocketController.java
@@ -0,0 +1,128 @@
+package com.zhangmeng.tools.controller;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.io.BufferUtil;
+import cn.hutool.core.lang.Console;
+import cn.hutool.core.thread.ThreadUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.log.StaticLog;
+import cn.hutool.socket.aio.AioClient;
+import cn.hutool.socket.aio.AioServer;
+import cn.hutool.socket.aio.AioSession;
+import cn.hutool.socket.aio.SimpleIoAction;
+import com.zhangmeng.tools.utils.AlertUtils;
+import javafx.fxml.FXML;
+import javafx.scene.control.Button;
+import javafx.scene.control.TextArea;
+import javafx.scene.control.TextField;
+import lombok.extern.slf4j.Slf4j;
+
+import java.net.InetSocketAddress;
+import java.nio.ByteBuffer;
+import java.util.Date;
+
+/**
+ * @author : 芊芊墨客
+ * @version : 1.0
+ * @date : 2023-05-06 10:38
+ */
+@Slf4j
+public class AioServerSocketController {
+
+ @FXML
+ public TextArea content;
+
+ @FXML
+ public TextField input;
+
+ @FXML
+ public Button send;
+
+ @FXML
+ public TextField port;
+
+ public Button start_server;
+
+ public Button close_server;
+
+ private AioServer aioServer;
+
+ private AioSession aioSession;
+
+ private void appendMsg(String msg) {
+ content.appendText(System.lineSeparator());
+ content.appendText(msg);
+ }
+
+ @FXML
+ public void initialize() {
+
+ port.setText("8083");
+
+ start_server.setOnAction(event -> {
+ content.setText(null);
+ if (port.getText().length() == 0) {
+ AlertUtils.alert_warning("端口不能为空");
+ return;
+ }
+ new Thread(() -> {
+ aioServer = new AioServer(Integer.parseInt(port.getText())) {
+ @Override
+ public void start(boolean sync) {
+ log.debug("Aio Server started, waiting for accept.");
+ appendMsg("服务启动成功============================================================================================");
+ // 接收客户端连接
+ accept();
+// if (sync) {
+// ThreadUtil.sync(this);
+// }
+ }
+ };
+ aioServer.setIoAction(new SimpleIoAction() {
+ @Override
+ public void accept(AioSession session) {
+ aioSession = session;
+ String msg = "【客户端】:{} 连接。" + session.getRemoteAddress();
+ appendMsg(msg);
+ session.write(BufferUtil.createUtf8("=== Welcome to Hutool socket server. ==="));
+ }
+
+ @Override
+ public void failed(Throwable exc, AioSession session) {
+ exc.printStackTrace();
+ super.failed(exc, session);
+ }
+
+ @Override
+ public void doAction(AioSession session, ByteBuffer data) {
+ appendMsg("========>服务端接收" + new Date() + "=======>:" + StrUtil.utf8Str(data));
+ if (data.hasRemaining()) {
+ session.read();
+ }
+ }
+ }).start(true);
+ }).start();
+ });
+
+ send.setOnAction(event -> {
+ send();
+ });
+
+ close_server.setOnAction(event -> {
+ if (aioServer.isOpen()) {
+ aioServer.close();
+ appendMsg("服务关闭成功============================================================================================");
+ }
+ });
+ }
+
+ private void send() {
+ if (input.getText().length() == 0) {
+ AlertUtils.alert_warning("发送的内容不能为空");
+ return;
+ }
+ String msg = input.getText();
+ aioSession.write(ByteBuffer.wrap(msg.getBytes()));
+ appendMsg("========>服务端发送" + new Date() + "=======>:" + msg );
+ }
+}
diff --git a/src/main/java/com/zhangmeng/tools/controller/HomeController.java b/src/main/java/com/zhangmeng/tools/controller/HomeController.java
index 848d329..1b43791 100644
--- a/src/main/java/com/zhangmeng/tools/controller/HomeController.java
+++ b/src/main/java/com/zhangmeng/tools/controller/HomeController.java
@@ -546,4 +546,16 @@ public class HomeController implements Serializable {
public void batch_update_file_name_menu_item(ActionEvent event) {
load_small_tools(14);
}
+
+ public void socket_client_aio_menu_item(ActionEvent event) {
+ load_server_tools(4);
+ }
+
+ public void socket_server_aio_menu_item(ActionEvent event) {
+ load_server_tools(5);
+ }
+
+ public void socket_server_nio_menu_item(ActionEvent event) {
+ load_server_tools(6);
+ }
}
\ No newline at end of file
diff --git a/src/main/java/com/zhangmeng/tools/controller/NioServerSocketController.java b/src/main/java/com/zhangmeng/tools/controller/NioServerSocketController.java
new file mode 100644
index 0000000..e8e919e
--- /dev/null
+++ b/src/main/java/com/zhangmeng/tools/controller/NioServerSocketController.java
@@ -0,0 +1,129 @@
+package com.zhangmeng.tools.controller;
+
+import cn.hutool.core.io.BufferUtil;
+import cn.hutool.core.io.IORuntimeException;
+import cn.hutool.core.io.IoUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.socket.aio.AioServer;
+import cn.hutool.socket.aio.AioSession;
+import cn.hutool.socket.aio.SimpleIoAction;
+import cn.hutool.socket.nio.NioServer;
+import com.zhangmeng.tools.utils.AlertUtils;
+import javafx.fxml.FXML;
+import javafx.scene.control.Button;
+import javafx.scene.control.TextArea;
+import javafx.scene.control.TextField;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.SocketChannel;
+import java.util.Date;
+
+/**
+ * @author : 芊芊墨客
+ * @version : 1.0
+ * @date : 2023-05-06 15:18
+ */
+@Slf4j
+public class NioServerSocketController {
+
+ @FXML
+ public TextArea content;
+
+ @FXML
+ public TextField input;
+
+ @FXML
+ public Button send;
+
+ @FXML
+ public TextField port;
+
+ public Button start_server;
+
+ public Button close_server;
+
+ private NioServer nioServer;
+
+ private SocketChannel channel;
+
+ private void appendMsg(String msg) {
+ content.appendText(System.lineSeparator());
+ content.appendText(msg);
+ }
+
+ @FXML
+ public void initialize() {
+
+ port.setText("8083");
+
+ start_server.setOnAction(event -> {
+ content.setText(null);
+ if (port.getText().length() == 0) {
+ AlertUtils.alert_warning("端口不能为空");
+ return;
+ }
+ new Thread(() -> {
+ nioServer = new NioServer(Integer.parseInt(port.getText()));
+ nioServer.setChannelHandler((sc) -> {
+
+ channel = sc;
+
+ ByteBuffer readBuffer = ByteBuffer.allocate(1024);
+ try {
+ //从channel读数据到缓冲区
+ int readBytes = sc.read(readBuffer);
+ if (readBytes > 0) {
+ //Flips this buffer. The limit is set to the current position and then
+ // the position is set to zero,就是表示要从起始位置开始读取数据
+ readBuffer.flip();
+ //eturns the number of elements between the current position and the limit.
+ // 要读取的字节长度
+ byte[] bytes = new byte[readBuffer.remaining()];
+ //将缓冲区的数据读到bytes数组
+ readBuffer.get(bytes);
+ String body = StrUtil.utf8Str(bytes);
+ appendMsg("========>服务端接收" + new Date() + "=======>:" + body);
+ doWrite(sc, body);
+ } else if (readBytes < 0) {
+ IoUtil.close(sc);
+ }
+ } catch (IOException e) {
+ throw new IORuntimeException(e);
+ }
+ });
+ nioServer.listen();
+ }).start();
+ });
+
+ send.setOnAction(event -> {
+ send();
+ });
+
+ close_server.setOnAction(event -> {
+ nioServer.close();
+ appendMsg("服务关闭成功============================================================================================");
+ });
+ }
+
+ public static void doWrite(SocketChannel channel, String response) throws IOException {
+ response = "收到消息:" + response;
+ //将缓冲数据写入渠道,返回给客户端
+ channel.write(BufferUtil.createUtf8(response));
+ }
+
+ private void send() {
+ if (input.getText().length() == 0) {
+ AlertUtils.alert_warning("发送的内容不能为空");
+ return;
+ }
+ String msg = input.getText();
+ try {
+ channel.write(ByteBuffer.wrap(msg.getBytes()));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ appendMsg("========>服务端发送" + new Date() + "=======>:" + msg);
+ }
+}
diff --git a/src/main/java/com/zhangmeng/tools/controller/ServerToolsController.java b/src/main/java/com/zhangmeng/tools/controller/ServerToolsController.java
index d2adb2c..220c108 100644
--- a/src/main/java/com/zhangmeng/tools/controller/ServerToolsController.java
+++ b/src/main/java/com/zhangmeng/tools/controller/ServerToolsController.java
@@ -43,6 +43,10 @@ public class ServerToolsController {
private AnchorPane ftpServer;
private AnchorPane sshClient;
private AnchorPane socketServer;
+ private AnchorPane socket_client_aio;
+ private AnchorPane socket_server_aio;
+ private AnchorPane socket_server_nio;
+
public static final String color_cell = "#f4f4f4";
@@ -276,10 +280,75 @@ public class ServerToolsController {
}
socketServer(flag);
}
+
+ if (newValue.getIndex() == 4){
+ if (socket_client_aio != null){
+ flag = true;
+ }
+ socket_client_aio(flag);
+ }
+
+ if (newValue.getIndex() == 5){
+ if (socket_server_aio != null){
+ flag = true;
+ }
+ socket_server_aio(flag);
+ }
+ if (newValue.getIndex() == 5){
+ if (socket_server_nio != null){
+ flag = true;
+ }
+ socket_server_nio(flag);
+ }
}
});
}
+ private void socket_server_nio(boolean flag) {
+ listView.getSelectionModel().select(6);
+ if (!flag){
+ try {
+ root = FXMLLoader.load(ResourcesUtils.getResource("socket-server-nio"));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ socket_server_nio = root;
+ }else {
+ root = socket_server_nio;
+ }
+ common_method();
+ }
+
+ private void socket_server_aio(boolean flag) {
+ listView.getSelectionModel().select(5);
+ if (!flag){
+ try {
+ root = FXMLLoader.load(ResourcesUtils.getResource("socket-server-aio"));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ socket_server_aio = root;
+ }else {
+ root = socket_server_aio;
+ }
+ common_method();
+ }
+
+ private void socket_client_aio(boolean flag) {
+ listView.getSelectionModel().select(4);
+ if (!flag){
+ try {
+ root = FXMLLoader.load(ResourcesUtils.getResource("socket-client-aio"));
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ socket_client_aio = root;
+ }else {
+ root = socket_client_aio;
+ }
+ common_method();
+ }
+
private void socketServer(boolean flag){
listView.getSelectionModel().select(3);
if (!flag){
@@ -288,9 +357,9 @@ public class ServerToolsController {
} catch (IOException e) {
e.printStackTrace();
}
- ftpServer = root;
+ socketServer = root;
}else {
- root = ftpServer;
+ root = socketServer;
}
common_method();
}
@@ -352,6 +421,9 @@ public class ServerToolsController {
case Ftp_Server -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
case SSH_Client -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
case Socket_Server -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
+ case Socket_client_aio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
+ case Socket_server_aio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
+ case Socket_server_nio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
};
}
@@ -524,4 +596,13 @@ public class ServerToolsController {
public void maven_jar_install_menu_item(ActionEvent event) {
load_small_tools(10);
}
+
+ public void socket_client_aio_menu_item(ActionEvent event) {
+ boolean flag = false;
+
+ }
+
+ public void socket_server_aio_menu_item(ActionEvent event) {
+ boolean flag = false;
+ }
}
diff --git a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java
index 8b3b97f..b2d9874 100644
--- a/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java
+++ b/src/main/java/com/zhangmeng/tools/utils/ResourcesUtils.java
@@ -26,6 +26,7 @@
package com.zhangmeng.tools.utils;
import javafx.scene.image.Image;
+import javafx.scene.layout.AnchorPane;
import java.net.URL;
@@ -38,19 +39,19 @@ public class ResourcesUtils {
public static final String base_fxml = "/fxml/";
- public static URL getResource(String name){
+ public static URL getResource(String name) {
String path = base_fxml + name + ".fxml";
return ResourcesUtils.class.getResource(path);
}
- public static Image getBg(){
- return new Image(ImagePath.path(ImagePath.ImagePathType.BACKGROUND_IMAGE));
+ public static Image getBg() {
+ return new Image(ImagePath.path(ImagePath.ImagePathType.BACKGROUND_IMAGE));
}
- public enum Menu{
- Md5("md5加密",0),
- SpringSecurity("spring加密",1),
- Jks_File("spring加密",2),
+ public enum Menu {
+ Md5("md5加密", 0),
+ SpringSecurity("spring加密", 1),
+ Jks_File("spring加密", 2),
;
@@ -80,12 +81,12 @@ public class ResourcesUtils {
}
- public enum Player{
+ public enum Player {
- Video("视频播放",0),
- Music("音乐播放",1),
- VipParser("vip解析",2),
- MusicParser("音乐解析",3),
+ Video("视频播放", 0),
+ Music("音乐播放", 1),
+ VipParser("vip解析", 2),
+ MusicParser("音乐解析", 3),
;
Player(String title, int index) {
@@ -113,22 +114,22 @@ public class ResourcesUtils {
private int index;
}
- public enum SmallTools{
- Hex_16("16进制(Hex)",0),
- Unicode("Unicode和字符串转换",1),
- JWT_WEB("json-web-token",2),
- COLOR_CHOOSE("颜色选择",3),
- Qr_CODE("生成二维码",4),
- Date_Query("日历",5),
- Cron("cron表达式",6),
- Mail("发送邮件",7),
- TelePhone("手机号工具",8),
- JSONView("json工具",9),
- Maven_Install_Jar("maven安装jar",10),
- Word_ocr("文字识别ocr",11),
- Bar_Code("条形码识别",12),
- Pdf_Ocr("pdf识别",13),
- BatchUpdateFileName("批量修改文件名",14),
+ public enum SmallTools {
+ Hex_16("16进制(Hex)", 0),
+ Unicode("Unicode和字符串转换", 1),
+ JWT_WEB("json-web-token", 2),
+ COLOR_CHOOSE("颜色选择", 3),
+ Qr_CODE("生成二维码", 4),
+ Date_Query("日历", 5),
+ Cron("cron表达式", 6),
+ Mail("发送邮件", 7),
+ TelePhone("手机号工具", 8),
+ JSONView("json工具", 9),
+ Maven_Install_Jar("maven安装jar", 10),
+ Word_ocr("文字识别ocr", 11),
+ Bar_Code("条形码识别", 12),
+ Pdf_Ocr("pdf识别", 13),
+ BatchUpdateFileName("批量修改文件名", 14),
;
SmallTools(String title, int index) {
@@ -156,12 +157,12 @@ public class ResourcesUtils {
private int index;
}
- public enum CodecTools{
+ public enum CodecTools {
- Base62("Base62编码解码",0),
- Base64("Base64编码解码",1),
- Base32("Base32编码解码",2),
- MorseCoder ("摩尔斯电码",3),
+ Base62("Base62编码解码", 0),
+ Base64("Base64编码解码", 1),
+ Base32("Base32编码解码", 2),
+ MorseCoder("摩尔斯电码", 3),
;
CodecTools(String title, int index) {
@@ -189,11 +190,11 @@ public class ResourcesUtils {
private int index;
}
- public enum SqlTools{
+ public enum SqlTools {
- MySql_Code_Generate("mysql 代码生成",0),
- MyBatis_plus_Generate("mybatis-plus 代码生成",1),
- Json_To_JavaBean("json转javabean 代码生成",2),
+ MySql_Code_Generate("mysql 代码生成", 0),
+ MyBatis_plus_Generate("mybatis-plus 代码生成", 1),
+ Json_To_JavaBean("json转javabean 代码生成", 2),
;
SqlTools(String title, int index) {
@@ -221,9 +222,9 @@ public class ResourcesUtils {
private int index;
}
- public enum NetWorkTools{
+ public enum NetWorkTools {
- Netty_Client_Websocket("netty websocket 客户端",0),
+ Netty_Client_Websocket("netty websocket 客户端", 0),
;
NetWorkTools(String title, int index) {
@@ -251,11 +252,11 @@ public class ResourcesUtils {
private int index;
}
- public enum HttpTools{
+ public enum HttpTools {
- Http_Request("http 请求工具",0),
- Http_Upload("http 上传工具",1),
- Http_DownLoad("http 下载工具",2),
+ Http_Request("http 请求工具", 0),
+ Http_Upload("http 上传工具", 1),
+ Http_DownLoad("http 下载工具", 2),
;
HttpTools(String title, int index) {
@@ -283,12 +284,15 @@ public class ResourcesUtils {
private int index;
}
- public enum ServerTools{
+ public enum ServerTools {
- Http_Server("Http Server工具",0),
- Ftp_Server("Ftp Server工具",1),
- SSH_Client("ssh 连接工具",2),
- Socket_Server("socket Server",3);
+ Http_Server("Http Server工具", 0),
+ Ftp_Server("Ftp Server工具", 1),
+ SSH_Client("ssh 连接工具", 2),
+ Socket_Server("socket Server", 3),
+ Socket_client_aio("aio socket client",4),
+ Socket_server_aio("aio socket server",5),
+ Socket_server_nio("aio socket server",6),
;
ServerTools(String title, int index) {
diff --git a/src/main/resources/fxml/home.fxml b/src/main/resources/fxml/home.fxml
index fbe36a9..faed74c 100644
--- a/src/main/resources/fxml/home.fxml
+++ b/src/main/resources/fxml/home.fxml
@@ -81,6 +81,9 @@
+
+
+
diff --git a/src/main/resources/fxml/server-tools.fxml b/src/main/resources/fxml/server-tools.fxml
index d09bca6..ad97eb9 100644
--- a/src/main/resources/fxml/server-tools.fxml
+++ b/src/main/resources/fxml/server-tools.fxml
@@ -74,6 +74,8 @@
+
+
diff --git a/src/main/resources/fxml/socket-client-aio.fxml b/src/main/resources/fxml/socket-client-aio.fxml
new file mode 100644
index 0000000..3ce1783
--- /dev/null
+++ b/src/main/resources/fxml/socket-client-aio.fxml
@@ -0,0 +1,27 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/resources/fxml/socket-server-aio.fxml b/src/main/resources/fxml/socket-server-aio.fxml
new file mode 100644
index 0000000..93e80a8
--- /dev/null
+++ b/src/main/resources/fxml/socket-server-aio.fxml
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/src/main/resources/fxml/socket-server-nio.fxml b/src/main/resources/fxml/socket-server-nio.fxml
new file mode 100644
index 0000000..940b3ef
--- /dev/null
+++ b/src/main/resources/fxml/socket-server-nio.fxml
@@ -0,0 +1,25 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+