mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-05-18 21:26:35 +00:00
perf[orm]: 简化测试用例
This commit is contained in:
@@ -15,7 +15,7 @@ public class AioClientTest implements Runnable {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
AioClientTest aioClient = new AioClientTest("127.0.0.1", 9999, 1);
|
||||
var aioClient = new AioClientTest();
|
||||
aioClient.init();
|
||||
new Thread(aioClient, "client").start();
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
@@ -23,15 +23,7 @@ public class AioClientTest implements Runnable {
|
||||
|
||||
|
||||
private AsynchronousSocketChannel client;
|
||||
private String host;
|
||||
private int port;
|
||||
private CountDownLatch latch;
|
||||
|
||||
public AioClientTest(String host, int port, int latchNum) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.latch = new CountDownLatch(latchNum);
|
||||
}
|
||||
private CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
@@ -44,7 +36,7 @@ public class AioClientTest implements Runnable {
|
||||
@Override
|
||||
public void run() {
|
||||
ConnectCompletionHandler handler = new ConnectCompletionHandler(client, latch);
|
||||
client.connect(new InetSocketAddress(host, port), handler, handler);
|
||||
client.connect(new InetSocketAddress("127.0.0.1", 9999), handler, handler);
|
||||
try {
|
||||
latch.await();
|
||||
client.close();
|
||||
|
||||
@@ -15,29 +15,22 @@ public class AioServerTest implements Runnable {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
AioServerTest aioServer = new AioServerTest(9999, 1);
|
||||
AioServerTest aioServer = new AioServerTest();
|
||||
aioServer.init();
|
||||
new Thread(aioServer, "server").start();
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
private int port;
|
||||
|
||||
private CountDownLatch latch;
|
||||
private CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
private AsynchronousServerSocketChannel asynchronousServerSocketChannel;
|
||||
|
||||
public AioServerTest(int port, int latchNum) {
|
||||
this.port = port;
|
||||
latch = new CountDownLatch(latchNum);
|
||||
}
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open();
|
||||
asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
|
||||
System.out.println("The Time server is start in port:" + port);
|
||||
asynchronousServerSocketChannel.bind(new InetSocketAddress(9999));
|
||||
System.out.println("The Time server is start in port:" + 9999);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ public class TcpTest {
|
||||
server = new ServerSocket(9999);
|
||||
// 2.接受客户端连接,阻塞式
|
||||
Socket socket = server.accept();
|
||||
System.out.println("hello http!!!!!!");
|
||||
|
||||
// 3.发送数据+接受数据
|
||||
String message = "welocme to internet!!!";
|
||||
|
||||
@@ -47,7 +47,7 @@ public class UdpTest {
|
||||
byte[] data = message.getBytes();
|
||||
// 3.打包DatagramPacket,发送的地点及端口
|
||||
DatagramPacket packet = new DatagramPacket(data, data.length,
|
||||
new InetSocketAddress("localhost", 8888));
|
||||
new InetSocketAddress("localhost", 8000));
|
||||
// 4.发送
|
||||
client.send(packet);
|
||||
// 5.释放资源
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
package com.zfoo.net.base.bio.chat;
|
||||
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import com.zfoo.util.ThreadUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Scanner;
|
||||
|
||||
@Ignore
|
||||
public class BioClientTest {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
System.out.println("请输入名称:");
|
||||
String name = new Scanner(System.in).nextLine();
|
||||
|
||||
new BioClientTest().launchClient(name);
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
private String name;
|
||||
|
||||
private class SendThread implements Runnable {
|
||||
// 控制台输入流
|
||||
private BufferedReader console;
|
||||
// 管道输出流
|
||||
private DataOutputStream dataOut;
|
||||
// 控制线程
|
||||
private boolean isRunning;
|
||||
|
||||
private SendThread() {
|
||||
console = new BufferedReader(new InputStreamReader(System.in));
|
||||
isRunning = true;
|
||||
}
|
||||
|
||||
public SendThread(Socket client) {
|
||||
this();
|
||||
try {
|
||||
dataOut = new DataOutputStream(client.getOutputStream());
|
||||
send(name);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
isRunning = false;
|
||||
IOUtils.closeIO(console, dataOut);
|
||||
}
|
||||
}
|
||||
|
||||
private String getMessageFromConsole() {
|
||||
try {
|
||||
System.out.println("please input message:");
|
||||
return console.readLine();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void send(String message) {
|
||||
if (null != message) {
|
||||
try {
|
||||
dataOut.writeUTF(message);
|
||||
dataOut.flush();
|
||||
} catch (IOException e) {
|
||||
isRunning = false;
|
||||
IOUtils.closeIO(dataOut, console);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (isRunning) {
|
||||
send(getMessageFromConsole());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class ReceiveThread implements Runnable {
|
||||
// 输入流
|
||||
private DataInputStream dataIn;
|
||||
// 线程标识
|
||||
private boolean isRunning = true;
|
||||
|
||||
|
||||
public ReceiveThread(Socket client) {
|
||||
try {
|
||||
dataIn = new DataInputStream(client.getInputStream());
|
||||
} catch (IOException e) {
|
||||
isRunning = false;
|
||||
IOUtils.closeIO(dataIn);
|
||||
}
|
||||
}
|
||||
|
||||
private String receive() {
|
||||
try {
|
||||
return dataIn.readUTF();
|
||||
} catch (IOException e) {
|
||||
isRunning = false;
|
||||
IOUtils.closeIO(dataIn);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (isRunning) {
|
||||
System.out.println(receive());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void launchClient(String name) {
|
||||
// 1.创建客户端,必须指定服务器+端口,此时就在连接
|
||||
Socket client = null;
|
||||
Thread sendThread = null;
|
||||
Thread receiveThread = null;
|
||||
this.name = name;
|
||||
try {
|
||||
|
||||
client = new Socket("localhost", 9999);// 系统自动分配客户端的端口号
|
||||
// 2.发送数据+接受数据
|
||||
sendThread = new Thread(new SendThread(client));
|
||||
receiveThread = new Thread(new ReceiveThread(client));
|
||||
sendThread.start();
|
||||
receiveThread.start();
|
||||
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,130 +0,0 @@
|
||||
package com.zfoo.net.base.bio.chat;
|
||||
|
||||
import com.zfoo.protocol.util.IOUtils;
|
||||
import com.zfoo.util.ThreadUtils;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Ignore
|
||||
public class BioServerTest {
|
||||
|
||||
@Test
|
||||
public void bioTest() {
|
||||
new BioServerTest().launchServer();
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private List<ServerChannel> listClient = new ArrayList<ServerChannel>();
|
||||
|
||||
private class ServerChannel implements Runnable {
|
||||
// 发送数据
|
||||
private DataOutputStream dataOut;
|
||||
// 接受数据
|
||||
private DataInputStream dataIn;
|
||||
private String name;
|
||||
private boolean isRunning = true;
|
||||
|
||||
public ServerChannel(Socket client) {
|
||||
try {
|
||||
dataOut = new DataOutputStream(client.getOutputStream());
|
||||
dataIn = new DataInputStream(client.getInputStream());
|
||||
this.name = dataIn.readUTF();
|
||||
System.out.println(name);
|
||||
send("欢迎你进入聊天室!");
|
||||
sendToAll(this.name + "进入聊天室!");
|
||||
|
||||
} catch (IOException e) {
|
||||
quit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private String receive() {
|
||||
String message = null;
|
||||
|
||||
try {
|
||||
message = dataIn.readUTF();
|
||||
} catch (IOException e) {
|
||||
quit();
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private void send(String message) {
|
||||
if (message == null || message.equals(""))
|
||||
return;
|
||||
|
||||
try {
|
||||
System.out.println(message);
|
||||
dataOut.writeUTF(message);
|
||||
dataOut.flush();
|
||||
} catch (IOException e) {
|
||||
quit();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void sendToAll(String message) {
|
||||
if (message.startsWith("@") && message.contains(":")) {
|
||||
String name = message.substring(1, message.indexOf(":"));
|
||||
String content = message.substring(message.indexOf(":") + 1);
|
||||
for (ServerChannel other : listClient) {
|
||||
if (other.name.equals(name)) {
|
||||
other.send(this.name + "悄悄对你说:" + content);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
for (ServerChannel other : listClient) {
|
||||
if (other == this) {
|
||||
continue;
|
||||
}
|
||||
other.send(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void quit() {
|
||||
IOUtils.closeIO(dataOut);
|
||||
isRunning = false;
|
||||
listClient.remove(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
while (isRunning) {
|
||||
sendToAll(receive());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void launchServer() {
|
||||
// 1.穿件服务器,指定端口
|
||||
ServerSocket server = null;
|
||||
try {
|
||||
server = new ServerSocket(9999);
|
||||
while (true) {
|
||||
// 2.接受客户端连接,阻塞式
|
||||
Socket client = server.accept();
|
||||
ServerChannel serverChannel = new ServerChannel(client);
|
||||
// 3.发送数据+接受数据
|
||||
Thread clienThread = new Thread(serverChannel);
|
||||
listClient.add(serverChannel);
|
||||
clienThread.start();
|
||||
System.out.println("server started! Connect to client successfully");
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,8 +22,8 @@ public class EchoClientTest {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
new EchoClientTest().connect(9999, "127.0.0.1");
|
||||
System.out.println("hello");
|
||||
var client = new EchoClientTest();
|
||||
client.connect(9999, "127.0.0.1");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -31,19 +31,11 @@ public class EchoServerTest {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
System.out.println("hello");
|
||||
EchoServerTest server = new EchoServerTest(9999);
|
||||
var server = new EchoServerTest();
|
||||
server.init();
|
||||
System.out.println("hello");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private int port;
|
||||
|
||||
public EchoServerTest(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//配置服务端nio线程组
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||
@@ -55,7 +47,7 @@ public class EchoServerTest {
|
||||
.childHandler(new ChildChannelHandler());
|
||||
|
||||
//绑定端口,同步等待成功
|
||||
ChannelFuture future = bootstrap.bind(port).sync();
|
||||
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||
//等待服务端监听端口关闭
|
||||
future.channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -19,7 +19,8 @@ public class EchoClientTest {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
new EchoClientTest().connect(9999, "127.0.0.1");
|
||||
var client = new EchoClientTest();
|
||||
client.connect(9999, "127.0.0.1");
|
||||
System.out.println("hello");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
@@ -28,19 +28,11 @@ public class EchoServerTest {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
System.out.println("hello");
|
||||
EchoServerTest server = new EchoServerTest(9999);
|
||||
var server = new EchoServerTest();
|
||||
server.init();
|
||||
System.out.println("hello");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private int port;
|
||||
|
||||
public EchoServerTest(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//配置服务端nio线程组
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||
@@ -52,7 +44,7 @@ public class EchoServerTest {
|
||||
.childHandler(new ChildChannelHandler());
|
||||
|
||||
//绑定端口,同步等待成功
|
||||
ChannelFuture future = bootstrap.bind(port).sync();
|
||||
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||
//等待服务端监听端口关闭
|
||||
future.channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -26,23 +26,12 @@ public class FileClientTest {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
FileClientTest client = new FileClientTest(9999, "127.0.0.1", "rainbow.txt");
|
||||
var client = new FileClientTest();
|
||||
client.connect();
|
||||
System.out.println("hello");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
private int port;
|
||||
private String host;
|
||||
private String filePath;
|
||||
|
||||
public FileClientTest(int port, String host, String filePath) {
|
||||
this.port = port;
|
||||
this.host = host;
|
||||
this.filePath = filePath;
|
||||
}
|
||||
|
||||
public void connect() {
|
||||
EventLoopGroup group = new NioEventLoopGroup();
|
||||
Bootstrap bootstrap = new Bootstrap();
|
||||
@@ -51,7 +40,7 @@ public class FileClientTest {
|
||||
.handler(new ChildChannelHandler());
|
||||
ChannelFuture future = null;
|
||||
try {
|
||||
future = bootstrap.connect(host, port).sync();
|
||||
future = bootstrap.connect("127.0.0.1", 9999).sync();
|
||||
future.channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
@@ -66,7 +55,7 @@ public class FileClientTest {
|
||||
channel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
|
||||
channel.pipeline().addLast(new LineBasedFrameDecoder(1024));
|
||||
channel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));//三者组合起来就是文本换行编码解码器
|
||||
channel.pipeline().addLast(new ClientHandler(filePath));
|
||||
channel.pipeline().addLast(new ClientHandler("rainbow.txt"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,19 +30,11 @@ public class FileServerTest {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
System.out.println("hello");
|
||||
FileServerTest server = new FileServerTest(9999);
|
||||
var server = new FileServerTest();
|
||||
server.init();
|
||||
System.out.println("hello");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private int port;
|
||||
|
||||
public FileServerTest(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//配置服务端nio线程组
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||
@@ -54,7 +46,7 @@ public class FileServerTest {
|
||||
.childHandler(new ChildChannelHandler());
|
||||
|
||||
//绑定端口,同步等待成功
|
||||
ChannelFuture future = bootstrap.bind(port).sync();
|
||||
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||
//等待服务端监听端口关闭
|
||||
future.channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -25,8 +25,8 @@ public class SubscribeClientTest {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
new SubscribeClientTest().connect(9999, "127.0.0.1");
|
||||
System.out.println("hello");
|
||||
var client = new SubscribeClientTest();
|
||||
client.connect(9999, "127.0.0.1");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,19 +29,11 @@ public class SubscribeServerTest {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
System.out.println("hello");
|
||||
SubscribeServerTest server = new SubscribeServerTest(9999);
|
||||
var server = new SubscribeServerTest();
|
||||
server.init();
|
||||
System.out.println("hello");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private int port;
|
||||
|
||||
public SubscribeServerTest(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//配置服务端nio线程组
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||
@@ -53,7 +45,7 @@ public class SubscribeServerTest {
|
||||
.childHandler(new ChildChannelHandler());
|
||||
|
||||
//绑定端口,同步等待成功
|
||||
ChannelFuture future = bootstrap.bind(port).sync();
|
||||
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||
//等待服务端监听端口关闭
|
||||
future.channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -17,7 +17,8 @@ public class TimeClientTest {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
new TimeClientTest().connect(9999, "127.0.0.1");
|
||||
var client = new TimeClientTest();
|
||||
client.connect(9999, "127.0.0.1");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -24,16 +24,11 @@ public class TimeServerTest {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
TimeServerTest server = new TimeServerTest(9999);
|
||||
var server = new TimeServerTest();
|
||||
server.init();
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private int port;
|
||||
|
||||
public TimeServerTest(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//配置服务端nio线程组
|
||||
@@ -44,7 +39,7 @@ public class TimeServerTest {
|
||||
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
||||
.option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());
|
||||
//绑定端口,同步等待成功
|
||||
ChannelFuture future = bootstrap.bind(port).sync();
|
||||
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||
//等待服务端监听端口关闭
|
||||
future.channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -19,8 +19,8 @@ public class TimeClientTest {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
new TimeClientTest().connect(9999, "127.0.0.1");
|
||||
System.out.println("hello");
|
||||
var client = new TimeClientTest();
|
||||
client.connect(9999, "127.0.0.1");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,19 +26,11 @@ public class TimeServerTest {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
System.out.println("hello");
|
||||
TimeServerTest server = new TimeServerTest(9999);
|
||||
var server = new TimeServerTest();
|
||||
server.init();
|
||||
System.out.println("hello");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private int port;
|
||||
|
||||
public TimeServerTest(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//配置服务端nio线程组
|
||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||
@@ -48,7 +40,7 @@ public class TimeServerTest {
|
||||
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
||||
.option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());
|
||||
//绑定端口,同步等待成功
|
||||
ChannelFuture future = bootstrap.bind(port).sync();
|
||||
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||
//等待服务端监听端口关闭
|
||||
future.channel().closeFuture().sync();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -13,13 +13,11 @@ import io.netty.util.CharsetUtil;
|
||||
*/
|
||||
public class UDPClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {
|
||||
|
||||
private static String DICTIONARY = "bbb";
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
||||
String req = packet.content().toString(CharsetUtil.UTF_8);
|
||||
System.out.println(req);
|
||||
ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(DICTIONARY, CharsetUtil.UTF_8)
|
||||
ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("clent", CharsetUtil.UTF_8)
|
||||
, packet.sender()));
|
||||
}
|
||||
|
||||
|
||||
@@ -26,19 +26,12 @@ public class UDPClientTest {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
System.out.println("hello");
|
||||
UDPClientTest server = new UDPClientTest(9999);
|
||||
server.init();
|
||||
System.out.println("hello");
|
||||
var client = new UDPClientTest();
|
||||
client.init();
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
private int port;
|
||||
|
||||
public UDPClientTest(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//配置服务端nio线程组
|
||||
@@ -54,11 +47,9 @@ public class UDPClientTest {
|
||||
|
||||
//向内网的所有机器广播UDP消息
|
||||
channel.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("client"
|
||||
, CharsetUtil.UTF_8), new InetSocketAddress("127.0.0.1", port))).sync();
|
||||
, CharsetUtil.UTF_8), new InetSocketAddress("127.0.0.1", 9999))).sync();
|
||||
|
||||
if (!channel.closeFuture().await(5000)) {
|
||||
System.out.println("查询超时!");
|
||||
}
|
||||
channel.closeFuture().await(5000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
|
||||
@@ -13,14 +13,12 @@ import io.netty.util.CharsetUtil;
|
||||
*/
|
||||
public class UDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
|
||||
|
||||
private static String DICTIONARY = "aaa";
|
||||
|
||||
@Override
|
||||
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
||||
String req = packet.content().toString(CharsetUtil.UTF_8);
|
||||
System.out.println(req);
|
||||
if (req.equals("client")) {
|
||||
ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer(UDPServerHandler.DICTIONARY, CharsetUtil.UTF_8)
|
||||
ctx.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("this is server", CharsetUtil.UTF_8)
|
||||
, packet.sender()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,19 +22,11 @@ public class UDPServerTest {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
System.out.println("hello");
|
||||
UDPServerTest server = new UDPServerTest(9999);
|
||||
var server = new UDPServerTest();
|
||||
server.init();
|
||||
System.out.println("hello");
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private int port;
|
||||
|
||||
public UDPServerTest(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
//配置服务端nio线程组
|
||||
EventLoopGroup group = new NioEventLoopGroup();//服务端接受客户端连接
|
||||
@@ -43,7 +35,7 @@ public class UDPServerTest {
|
||||
bootstrap.group(group).channel(NioDatagramChannel.class)
|
||||
.option(ChannelOption.SO_BROADCAST, true).handler(new UDPServerHandler());
|
||||
//绑定端口,同步等待成功
|
||||
ChannelFuture future = bootstrap.bind(port).sync();
|
||||
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||
//等待服务端监听端口关闭
|
||||
future.channel().closeFuture().await();
|
||||
} catch (InterruptedException e) {
|
||||
|
||||
@@ -17,29 +17,23 @@ public class NioClientTest implements Runnable {
|
||||
|
||||
@Test
|
||||
public void clientTest() {
|
||||
NioClientTest nioClient = new NioClientTest("localhost", 9999);
|
||||
var nioClient = new NioClientTest();
|
||||
nioClient.init();
|
||||
new Thread(nioClient, "clinetThread").start();
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
}
|
||||
|
||||
private String ip;
|
||||
private int port;
|
||||
|
||||
private Selector selector;
|
||||
private SocketChannel channel;
|
||||
private volatile boolean stop;
|
||||
|
||||
public NioClientTest(String ip, int port) {
|
||||
this.ip = ip;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public void init() {
|
||||
try {
|
||||
channel = SocketChannel.open();
|
||||
channel.configureBlocking(false);
|
||||
channel.connect(new InetSocketAddress(ip, port));
|
||||
channel.connect(new InetSocketAddress("localhost", 9999));
|
||||
|
||||
this.selector = Selector.open();
|
||||
channel.register(selector, SelectionKey.OP_CONNECT);
|
||||
|
||||
@@ -19,7 +19,7 @@ public class NioServerTest implements Runnable {
|
||||
|
||||
@Test
|
||||
public void serverTest() {
|
||||
NioServerTest nioServer = new NioServerTest();
|
||||
var nioServer = new NioServerTest();
|
||||
nioServer.init(9999);
|
||||
new Thread(nioServer, "serverThread").start();
|
||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||
|
||||
Reference in New Issue
Block a user