2023年5月6日16:38:40 aio nio
parent
414a9c632c
commit
e2bde5f722
|
|
@ -80,13 +80,6 @@ public class AioClientSocketController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void accept(AioSession session) {
|
|
||||||
aioSession = session;
|
|
||||||
appendMsg("客户端链接成功!" + session.getRemoteAddress().toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void failed(Throwable exc, AioSession session) {
|
public void failed(Throwable exc, AioSession session) {
|
||||||
exc.printStackTrace();
|
exc.printStackTrace();
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.net.InetSocketAddress;
|
import java.net.InetSocketAddress;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -73,9 +74,9 @@ public class AioServerSocketController {
|
||||||
appendMsg("服务启动成功============================================================================================");
|
appendMsg("服务启动成功============================================================================================");
|
||||||
// 接收客户端连接
|
// 接收客户端连接
|
||||||
accept();
|
accept();
|
||||||
// if (sync) {
|
if (sync) {
|
||||||
// ThreadUtil.sync(this);
|
ThreadUtil.sync(this);
|
||||||
// }
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
aioServer.setIoAction(new SimpleIoAction() {
|
aioServer.setIoAction(new SimpleIoAction() {
|
||||||
|
|
@ -87,16 +88,11 @@ public class AioServerSocketController {
|
||||||
session.write(BufferUtil.createUtf8("=== Welcome to Hutool socket server. ==="));
|
session.write(BufferUtil.createUtf8("=== Welcome to Hutool socket server. ==="));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void failed(Throwable exc, AioSession session) {
|
|
||||||
exc.printStackTrace();
|
|
||||||
super.failed(exc, session);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doAction(AioSession session, ByteBuffer data) {
|
public void doAction(AioSession session, ByteBuffer data) {
|
||||||
appendMsg("========>服务端接收" + new Date() + "=======>:" + StrUtil.utf8Str(data));
|
appendMsg("========>服务端接收" + new Date() + "=======>:" + StrUtil.utf8Str(data));
|
||||||
if (data.hasRemaining()) {
|
if (data.hasRemaining()) {
|
||||||
|
} else {
|
||||||
session.read();
|
session.read();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -122,7 +118,7 @@ public class AioServerSocketController {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String msg = input.getText();
|
String msg = input.getText();
|
||||||
aioSession.write(ByteBuffer.wrap(msg.getBytes()));
|
aioSession.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8)));
|
||||||
appendMsg("========>服务端发送" + new Date() + "=======>:" + msg );
|
appendMsg("========>服务端发送" + new Date() + "=======>:" + msg );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -558,4 +558,8 @@ public class HomeController implements Serializable {
|
||||||
public void socket_server_nio_menu_item(ActionEvent event) {
|
public void socket_server_nio_menu_item(ActionEvent event) {
|
||||||
load_server_tools(6);
|
load_server_tools(6);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void socket_client_nio_menu_item(ActionEvent event) {
|
||||||
|
load_server_tools(7);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,123 @@
|
||||||
|
package com.zhangmeng.tools.controller;
|
||||||
|
|
||||||
|
import cn.hutool.core.io.BufferUtil;
|
||||||
|
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 cn.hutool.socket.nio.NioClient;
|
||||||
|
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.net.InetSocketAddress;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
import java.nio.channels.SocketChannel;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author : 芊芊墨客
|
||||||
|
* @version : 1.0
|
||||||
|
* @date : 2023-05-06 10:38
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
public class NioClientSocketController {
|
||||||
|
|
||||||
|
@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 NioClient client;
|
||||||
|
|
||||||
|
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 NioClient(ip_address.getText(), Integer.parseInt(port.getText()));
|
||||||
|
client.setChannelHandler((sc)->{
|
||||||
|
ByteBuffer readBuffer = ByteBuffer.allocate(1024);
|
||||||
|
//从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();
|
||||||
|
//returns 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);
|
||||||
|
} else if (readBytes < 0) {
|
||||||
|
sc.close();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
client.listen();
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
try {
|
||||||
|
doWrite(client.getChannel(),msg);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
appendMsg("========>客户端发送" + new Date() + "=======>:" + msg );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void doWrite(SocketChannel channel, String msg) throws IOException {
|
||||||
|
//将缓冲数据写入渠道,返回给客户端
|
||||||
|
channel.write(BufferUtil.createUtf8(msg));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -85,7 +85,7 @@ public class NioServerSocketController {
|
||||||
readBuffer.get(bytes);
|
readBuffer.get(bytes);
|
||||||
String body = StrUtil.utf8Str(bytes);
|
String body = StrUtil.utf8Str(bytes);
|
||||||
appendMsg("========>服务端接收" + new Date() + "=======>:" + body);
|
appendMsg("========>服务端接收" + new Date() + "=======>:" + body);
|
||||||
doWrite(sc, body);
|
//doWrite(sc, body);
|
||||||
} else if (readBytes < 0) {
|
} else if (readBytes < 0) {
|
||||||
IoUtil.close(sc);
|
IoUtil.close(sc);
|
||||||
}
|
}
|
||||||
|
|
@ -107,10 +107,9 @@ public class NioServerSocketController {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void doWrite(SocketChannel channel, String response) throws IOException {
|
public static void doWrite(SocketChannel channel, String msg) throws IOException {
|
||||||
response = "收到消息:" + response;
|
|
||||||
//将缓冲数据写入渠道,返回给客户端
|
//将缓冲数据写入渠道,返回给客户端
|
||||||
channel.write(BufferUtil.createUtf8(response));
|
channel.write(BufferUtil.createUtf8(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void send() {
|
private void send() {
|
||||||
|
|
@ -120,7 +119,7 @@ public class NioServerSocketController {
|
||||||
}
|
}
|
||||||
String msg = input.getText();
|
String msg = input.getText();
|
||||||
try {
|
try {
|
||||||
channel.write(ByteBuffer.wrap(msg.getBytes()));
|
doWrite(channel,msg);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ public class ServerToolsController {
|
||||||
private AnchorPane sshClient;
|
private AnchorPane sshClient;
|
||||||
private AnchorPane socketServer;
|
private AnchorPane socketServer;
|
||||||
private AnchorPane socket_client_aio;
|
private AnchorPane socket_client_aio;
|
||||||
|
private AnchorPane socket_client_nio;
|
||||||
private AnchorPane socket_server_aio;
|
private AnchorPane socket_server_aio;
|
||||||
private AnchorPane socket_server_nio;
|
private AnchorPane socket_server_nio;
|
||||||
|
|
||||||
|
|
@ -294,16 +295,38 @@ public class ServerToolsController {
|
||||||
}
|
}
|
||||||
socket_server_aio(flag);
|
socket_server_aio(flag);
|
||||||
}
|
}
|
||||||
if (newValue.getIndex() == 5){
|
if (newValue.getIndex() == 6){
|
||||||
if (socket_server_nio != null){
|
if (socket_server_nio != null){
|
||||||
flag = true;
|
flag = true;
|
||||||
}
|
}
|
||||||
socket_server_nio(flag);
|
socket_server_nio(flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (newValue.getIndex() == 7){
|
||||||
|
if (socket_client_nio != null){
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
socket_client_nio(flag);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void socket_client_nio(boolean flag) {
|
||||||
|
listView.getSelectionModel().select(7);
|
||||||
|
if (!flag){
|
||||||
|
try {
|
||||||
|
root = FXMLLoader.load(ResourcesUtils.getResource("socket-client-nio"));
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
socket_client_nio = root;
|
||||||
|
}else {
|
||||||
|
root = socket_client_nio;
|
||||||
|
}
|
||||||
|
common_method();
|
||||||
|
}
|
||||||
|
|
||||||
private void socket_server_nio(boolean flag) {
|
private void socket_server_nio(boolean flag) {
|
||||||
listView.getSelectionModel().select(6);
|
listView.getSelectionModel().select(6);
|
||||||
if (!flag){
|
if (!flag){
|
||||||
|
|
@ -424,6 +447,7 @@ public class ServerToolsController {
|
||||||
case Socket_client_aio -> 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_aio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
|
||||||
case Socket_server_nio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
|
case Socket_server_nio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
|
||||||
|
case Socket_client_nio -> new Image(ImagePath.path(ImagePath.ImagePathType.SPRING_SECURITY));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -599,10 +623,33 @@ public class ServerToolsController {
|
||||||
|
|
||||||
public void socket_client_aio_menu_item(ActionEvent event) {
|
public void socket_client_aio_menu_item(ActionEvent event) {
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
|
if (socket_client_aio != null){
|
||||||
|
flag =true;
|
||||||
|
}
|
||||||
|
socket_client_aio(flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void socket_server_aio_menu_item(ActionEvent event) {
|
public void socket_server_aio_menu_item(ActionEvent event) {
|
||||||
boolean flag = false;
|
boolean flag = false;
|
||||||
|
if (socket_server_aio != null){
|
||||||
|
flag =true;
|
||||||
|
}
|
||||||
|
socket_server_aio(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void socket_server_nio_menu_item(ActionEvent event) {
|
||||||
|
boolean flag = false;
|
||||||
|
if (socket_server_nio != null){
|
||||||
|
flag =true;
|
||||||
|
}
|
||||||
|
socket_server_nio(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void socket_client_nio_menu_item(ActionEvent event) {
|
||||||
|
boolean flag = false;
|
||||||
|
if (socket_client_nio != null){
|
||||||
|
flag =true;
|
||||||
|
}
|
||||||
|
socket_client_nio(flag);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -292,7 +292,8 @@ public class ResourcesUtils {
|
||||||
Socket_Server("socket Server", 3),
|
Socket_Server("socket Server", 3),
|
||||||
Socket_client_aio("aio socket client",4),
|
Socket_client_aio("aio socket client",4),
|
||||||
Socket_server_aio("aio socket server",5),
|
Socket_server_aio("aio socket server",5),
|
||||||
Socket_server_nio("aio socket server",6),
|
Socket_server_nio("nio socket server",6),
|
||||||
|
Socket_client_nio("nio socket client",7),
|
||||||
;
|
;
|
||||||
|
|
||||||
ServerTools(String title, int index) {
|
ServerTools(String title, int index) {
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,7 @@
|
||||||
<MenuItem mnemonicParsing="false" text="socket-client-aio 客户端工具" onAction="#socket_client_aio_menu_item"/>
|
<MenuItem mnemonicParsing="false" text="socket-client-aio 客户端工具" onAction="#socket_client_aio_menu_item"/>
|
||||||
<MenuItem mnemonicParsing="false" text="socket-server-aio 服务端工具" onAction="#socket_server_aio_menu_item"/>
|
<MenuItem mnemonicParsing="false" text="socket-server-aio 服务端工具" onAction="#socket_server_aio_menu_item"/>
|
||||||
<MenuItem mnemonicParsing="false" text="socket-server-nio 服务端工具" onAction="#socket_server_nio_menu_item"/>
|
<MenuItem mnemonicParsing="false" text="socket-server-nio 服务端工具" onAction="#socket_server_nio_menu_item"/>
|
||||||
|
<MenuItem mnemonicParsing="false" text="socket-client-nio 服务端工具" onAction="#socket_client_nio_menu_item"/>
|
||||||
</items>
|
</items>
|
||||||
</Menu>
|
</Menu>
|
||||||
</menus>
|
</menus>
|
||||||
|
|
|
||||||
|
|
@ -76,6 +76,8 @@
|
||||||
<MenuItem mnemonicParsing="false" text="socket-server 服务工具" onAction="#socket_server_menu_item"/>
|
<MenuItem mnemonicParsing="false" text="socket-server 服务工具" onAction="#socket_server_menu_item"/>
|
||||||
<MenuItem mnemonicParsing="false" text="socket-client-aio 客户端工具" onAction="#socket_client_aio_menu_item"/>
|
<MenuItem mnemonicParsing="false" text="socket-client-aio 客户端工具" onAction="#socket_client_aio_menu_item"/>
|
||||||
<MenuItem mnemonicParsing="false" text="socket-server-aio 服务端工具" onAction="#socket_server_aio_menu_item"/>
|
<MenuItem mnemonicParsing="false" text="socket-server-aio 服务端工具" onAction="#socket_server_aio_menu_item"/>
|
||||||
|
<MenuItem mnemonicParsing="false" text="socket-server-nio 服务端工具" onAction="#socket_server_nio_menu_item"/>
|
||||||
|
<MenuItem mnemonicParsing="false" text="socket-client-nio 服务端工具" onAction="#socket_client_nio_menu_item"/>
|
||||||
</items>
|
</items>
|
||||||
</Menu>
|
</Menu>
|
||||||
</menus>
|
</menus>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,27 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<?import javafx.geometry.Insets?>
|
||||||
|
<?import javafx.scene.control.Button?>
|
||||||
|
<?import javafx.scene.control.Label?>
|
||||||
|
<?import javafx.scene.control.TextArea?>
|
||||||
|
<?import javafx.scene.control.TextField?>
|
||||||
|
<?import javafx.scene.layout.AnchorPane?>
|
||||||
|
|
||||||
|
<AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.NioClientSocketController">
|
||||||
|
<children>
|
||||||
|
<Label layoutX="91.0" layoutY="95.0" text="ip地址:" />
|
||||||
|
<TextField fx:id="ip_address" layoutX="157.0" layoutY="91.0" prefHeight="25.0" prefWidth="317.0" />
|
||||||
|
<TextField fx:id="port" layoutX="659.0" layoutY="91.0" />
|
||||||
|
<Label layoutX="606.0" layoutY="95.0" text="端口:" />
|
||||||
|
<Label layoutX="91.0" layoutY="163.0" text="内容:" />
|
||||||
|
<TextArea fx:id="content" layoutX="157.0" layoutY="172.0" prefHeight="264.0" prefWidth="988.0" AnchorPane.bottomAnchor="213.0" AnchorPane.leftAnchor="157.0" AnchorPane.rightAnchor="55.0" AnchorPane.topAnchor="172.0" />
|
||||||
|
<Button fx:id="connection" layoutX="864.0" layoutY="91.0" mnemonicParsing="false" text="连接" />
|
||||||
|
<Label layoutX="89.0" layoutY="469.0" text="输入:" AnchorPane.bottomAnchor="163.0" />
|
||||||
|
<TextField fx:id="input" layoutX="155.0" layoutY="465.0" prefHeight="25.0" prefWidth="667.0" AnchorPane.bottomAnchor="159.0" />
|
||||||
|
<Button fx:id="send" layoutX="849.0" layoutY="465.0" mnemonicParsing="false" text="发送" AnchorPane.bottomAnchor="159.0" />
|
||||||
|
<Button fx:id="disconn" layoutX="955.0" layoutY="91.0" mnemonicParsing="false" text="断开连接" />
|
||||||
|
</children>
|
||||||
|
<opaqueInsets>
|
||||||
|
<Insets />
|
||||||
|
</opaqueInsets>
|
||||||
|
</AnchorPane>
|
||||||
Loading…
Reference in New Issue