2023年5月6日16:38:40 aio nio

master
zhangmeng 2023-05-06 16:39:02 +08:00
parent 414a9c632c
commit e2bde5f722
10 changed files with 218 additions and 25 deletions

View File

@ -80,13 +80,6 @@ public class AioClientSocketController {
}
}
@Override
public void accept(AioSession session) {
aioSession = session;
appendMsg("客户端链接成功!" + session.getRemoteAddress().toString());
}
@Override
public void failed(Throwable exc, AioSession session) {
exc.printStackTrace();

View File

@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Date;
/**
@ -73,9 +74,9 @@ public class AioServerSocketController {
appendMsg("服务启动成功============================================================================================");
// 接收客户端连接
accept();
// if (sync) {
// ThreadUtil.sync(this);
// }
if (sync) {
ThreadUtil.sync(this);
}
}
};
aioServer.setIoAction(new SimpleIoAction() {
@ -87,16 +88,11 @@ public class AioServerSocketController {
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()) {
} else {
session.read();
}
}
@ -122,7 +118,7 @@ public class AioServerSocketController {
return;
}
String msg = input.getText();
aioSession.write(ByteBuffer.wrap(msg.getBytes()));
aioSession.write(ByteBuffer.wrap(msg.getBytes(StandardCharsets.UTF_8)));
appendMsg("========>服务端发送" + new Date() + "=======>:" + msg );
}
}

View File

@ -558,4 +558,8 @@ public class HomeController implements Serializable {
public void socket_server_nio_menu_item(ActionEvent event) {
load_server_tools(6);
}
public void socket_client_nio_menu_item(ActionEvent event) {
load_server_tools(7);
}
}

View File

@ -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));
}
}

View File

@ -85,7 +85,7 @@ public class NioServerSocketController {
readBuffer.get(bytes);
String body = StrUtil.utf8Str(bytes);
appendMsg("========>服务端接收" + new Date() + "=======>:" + body);
doWrite(sc, body);
//doWrite(sc, body);
} else if (readBytes < 0) {
IoUtil.close(sc);
}
@ -107,10 +107,9 @@ public class NioServerSocketController {
});
}
public static void doWrite(SocketChannel channel, String response) throws IOException {
response = "收到消息:" + response;
public static void doWrite(SocketChannel channel, String msg) throws IOException {
//将缓冲数据写入渠道,返回给客户端
channel.write(BufferUtil.createUtf8(response));
channel.write(BufferUtil.createUtf8(msg));
}
private void send() {
@ -120,7 +119,7 @@ public class NioServerSocketController {
}
String msg = input.getText();
try {
channel.write(ByteBuffer.wrap(msg.getBytes()));
doWrite(channel,msg);
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -44,6 +44,7 @@ public class ServerToolsController {
private AnchorPane sshClient;
private AnchorPane socketServer;
private AnchorPane socket_client_aio;
private AnchorPane socket_client_nio;
private AnchorPane socket_server_aio;
private AnchorPane socket_server_nio;
@ -294,16 +295,38 @@ public class ServerToolsController {
}
socket_server_aio(flag);
}
if (newValue.getIndex() == 5){
if (newValue.getIndex() == 6){
if (socket_server_nio != null){
flag = true;
}
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) {
listView.getSelectionModel().select(6);
if (!flag){
@ -424,6 +447,7 @@ public class ServerToolsController {
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));
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) {
boolean flag = false;
if (socket_client_aio != null){
flag =true;
}
socket_client_aio(flag);
}
public void socket_server_aio_menu_item(ActionEvent event) {
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);
}
}

View File

@ -292,7 +292,8 @@ public class ResourcesUtils {
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),
Socket_server_nio("nio socket server",6),
Socket_client_nio("nio socket client",7),
;
ServerTools(String title, int index) {

View File

@ -84,6 +84,7 @@
<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-nio 服务端工具" onAction="#socket_server_nio_menu_item"/>
<MenuItem mnemonicParsing="false" text="socket-client-nio 服务端工具" onAction="#socket_client_nio_menu_item"/>
</items>
</Menu>
</menus>

View File

@ -76,6 +76,8 @@
<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-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>
</Menu>
</menus>

View File

@ -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>