mirror of
https://github.com/tiennm99/zfoo.git
synced 2026-10-14 14:17:56 +00:00
perf[orm]: 简化测试用例
This commit is contained in:
1 parent
835f5cfec0
commit
00a685713f
24 files changed
+44
-410
No files matched your search
@@ -15,7 +15,7 @@ public class AioClientTest implements Runnable {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
public void clientTest() {
|
||||||
AioClientTest aioClient = new AioClientTest("127.0.0.1", 9999, 1);
|
var aioClient = new AioClientTest();
|
||||||
aioClient.init();
|
aioClient.init();
|
||||||
new Thread(aioClient, "client").start();
|
new Thread(aioClient, "client").start();
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
@@ -23,15 +23,7 @@ public class AioClientTest implements Runnable {
|
|||||||
|
|
||||||
|
|
||||||
private AsynchronousSocketChannel client;
|
private AsynchronousSocketChannel client;
|
||||||
private String host;
|
private CountDownLatch latch = new CountDownLatch(1);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
@@ -44,7 +36,7 @@ public class AioClientTest implements Runnable {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
ConnectCompletionHandler handler = new ConnectCompletionHandler(client, latch);
|
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 {
|
try {
|
||||||
latch.await();
|
latch.await();
|
||||||
client.close();
|
client.close();
|
||||||
|
|||||||
@@ -15,29 +15,22 @@ public class AioServerTest implements Runnable {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
AioServerTest aioServer = new AioServerTest(9999, 1);
|
AioServerTest aioServer = new AioServerTest();
|
||||||
aioServer.init();
|
aioServer.init();
|
||||||
new Thread(aioServer, "server").start();
|
new Thread(aioServer, "server").start();
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private CountDownLatch latch = new CountDownLatch(1);
|
||||||
private int port;
|
|
||||||
|
|
||||||
private CountDownLatch latch;
|
|
||||||
|
|
||||||
private AsynchronousServerSocketChannel asynchronousServerSocketChannel;
|
private AsynchronousServerSocketChannel asynchronousServerSocketChannel;
|
||||||
|
|
||||||
public AioServerTest(int port, int latchNum) {
|
|
||||||
this.port = port;
|
|
||||||
latch = new CountDownLatch(latchNum);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open();
|
asynchronousServerSocketChannel = AsynchronousServerSocketChannel.open();
|
||||||
asynchronousServerSocketChannel.bind(new InetSocketAddress(port));
|
asynchronousServerSocketChannel.bind(new InetSocketAddress(9999));
|
||||||
System.out.println("The Time server is start in port:" + port);
|
System.out.println("The Time server is start in port:" + 9999);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ public class TcpTest {
|
|||||||
server = new ServerSocket(9999);
|
server = new ServerSocket(9999);
|
||||||
// 2.接受客户端连接,阻塞式
|
// 2.接受客户端连接,阻塞式
|
||||||
Socket socket = server.accept();
|
Socket socket = server.accept();
|
||||||
System.out.println("hello http!!!!!!");
|
|
||||||
|
|
||||||
// 3.发送数据+接受数据
|
// 3.发送数据+接受数据
|
||||||
String message = "welocme to internet!!!";
|
String message = "welocme to internet!!!";
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public class UdpTest {
|
|||||||
byte[] data = message.getBytes();
|
byte[] data = message.getBytes();
|
||||||
// 3.打包DatagramPacket,发送的地点及端口
|
// 3.打包DatagramPacket,发送的地点及端口
|
||||||
DatagramPacket packet = new DatagramPacket(data, data.length,
|
DatagramPacket packet = new DatagramPacket(data, data.length,
|
||||||
new InetSocketAddress("localhost", 8888));
|
new InetSocketAddress("localhost", 8000));
|
||||||
// 4.发送
|
// 4.发送
|
||||||
client.send(packet);
|
client.send(packet);
|
||||||
// 5.释放资源
|
// 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
|
@Test
|
||||||
public void clientTest() {
|
public void clientTest() {
|
||||||
new EchoClientTest().connect(9999, "127.0.0.1");
|
var client = new EchoClientTest();
|
||||||
System.out.println("hello");
|
client.connect(9999, "127.0.0.1");
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -31,19 +31,11 @@ public class EchoServerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
System.out.println("hello");
|
var server = new EchoServerTest();
|
||||||
EchoServerTest server = new EchoServerTest(9999);
|
|
||||||
server.init();
|
server.init();
|
||||||
System.out.println("hello");
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public EchoServerTest(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
//配置服务端nio线程组
|
//配置服务端nio线程组
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||||
@@ -55,7 +47,7 @@ public class EchoServerTest {
|
|||||||
.childHandler(new ChildChannelHandler());
|
.childHandler(new ChildChannelHandler());
|
||||||
|
|
||||||
//绑定端口,同步等待成功
|
//绑定端口,同步等待成功
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||||
//等待服务端监听端口关闭
|
//等待服务端监听端口关闭
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -19,7 +19,8 @@ public class EchoClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
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");
|
System.out.println("hello");
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,19 +28,11 @@ public class EchoServerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
System.out.println("hello");
|
var server = new EchoServerTest();
|
||||||
EchoServerTest server = new EchoServerTest(9999);
|
|
||||||
server.init();
|
server.init();
|
||||||
System.out.println("hello");
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public EchoServerTest(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
//配置服务端nio线程组
|
//配置服务端nio线程组
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||||
@@ -52,7 +44,7 @@ public class EchoServerTest {
|
|||||||
.childHandler(new ChildChannelHandler());
|
.childHandler(new ChildChannelHandler());
|
||||||
|
|
||||||
//绑定端口,同步等待成功
|
//绑定端口,同步等待成功
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||||
//等待服务端监听端口关闭
|
//等待服务端监听端口关闭
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -26,23 +26,12 @@ public class FileClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
public void clientTest() {
|
||||||
FileClientTest client = new FileClientTest(9999, "127.0.0.1", "rainbow.txt");
|
var client = new FileClientTest();
|
||||||
client.connect();
|
client.connect();
|
||||||
System.out.println("hello");
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
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() {
|
public void connect() {
|
||||||
EventLoopGroup group = new NioEventLoopGroup();
|
EventLoopGroup group = new NioEventLoopGroup();
|
||||||
Bootstrap bootstrap = new Bootstrap();
|
Bootstrap bootstrap = new Bootstrap();
|
||||||
@@ -51,7 +40,7 @@ public class FileClientTest {
|
|||||||
.handler(new ChildChannelHandler());
|
.handler(new ChildChannelHandler());
|
||||||
ChannelFuture future = null;
|
ChannelFuture future = null;
|
||||||
try {
|
try {
|
||||||
future = bootstrap.connect(host, port).sync();
|
future = bootstrap.connect("127.0.0.1", 9999).sync();
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
@@ -66,7 +55,7 @@ public class FileClientTest {
|
|||||||
channel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
|
channel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
|
||||||
channel.pipeline().addLast(new LineBasedFrameDecoder(1024));
|
channel.pipeline().addLast(new LineBasedFrameDecoder(1024));
|
||||||
channel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));//三者组合起来就是文本换行编码解码器
|
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
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
System.out.println("hello");
|
var server = new FileServerTest();
|
||||||
FileServerTest server = new FileServerTest(9999);
|
|
||||||
server.init();
|
server.init();
|
||||||
System.out.println("hello");
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public FileServerTest(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
//配置服务端nio线程组
|
//配置服务端nio线程组
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||||
@@ -54,7 +46,7 @@ public class FileServerTest {
|
|||||||
.childHandler(new ChildChannelHandler());
|
.childHandler(new ChildChannelHandler());
|
||||||
|
|
||||||
//绑定端口,同步等待成功
|
//绑定端口,同步等待成功
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||||
//等待服务端监听端口关闭
|
//等待服务端监听端口关闭
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ public class SubscribeClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
public void clientTest() {
|
||||||
new SubscribeClientTest().connect(9999, "127.0.0.1");
|
var client = new SubscribeClientTest();
|
||||||
System.out.println("hello");
|
client.connect(9999, "127.0.0.1");
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,19 +29,11 @@ public class SubscribeServerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
System.out.println("hello");
|
var server = new SubscribeServerTest();
|
||||||
SubscribeServerTest server = new SubscribeServerTest(9999);
|
|
||||||
server.init();
|
server.init();
|
||||||
System.out.println("hello");
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public SubscribeServerTest(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
//配置服务端nio线程组
|
//配置服务端nio线程组
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||||
@@ -53,7 +45,7 @@ public class SubscribeServerTest {
|
|||||||
.childHandler(new ChildChannelHandler());
|
.childHandler(new ChildChannelHandler());
|
||||||
|
|
||||||
//绑定端口,同步等待成功
|
//绑定端口,同步等待成功
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||||
//等待服务端监听端口关闭
|
//等待服务端监听端口关闭
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -17,7 +17,8 @@ public class TimeClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
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);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,16 +24,11 @@ public class TimeServerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
TimeServerTest server = new TimeServerTest(9999);
|
var server = new TimeServerTest();
|
||||||
server.init();
|
server.init();
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public TimeServerTest(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
//配置服务端nio线程组
|
//配置服务端nio线程组
|
||||||
@@ -44,7 +39,7 @@ public class TimeServerTest {
|
|||||||
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
||||||
.option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());
|
.option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());
|
||||||
//绑定端口,同步等待成功
|
//绑定端口,同步等待成功
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||||
//等待服务端监听端口关闭
|
//等待服务端监听端口关闭
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ public class TimeClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
public void clientTest() {
|
||||||
new TimeClientTest().connect(9999, "127.0.0.1");
|
var client = new TimeClientTest();
|
||||||
System.out.println("hello");
|
client.connect(9999, "127.0.0.1");
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,19 +26,11 @@ public class TimeServerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
System.out.println("hello");
|
var server = new TimeServerTest();
|
||||||
TimeServerTest server = new TimeServerTest(9999);
|
|
||||||
server.init();
|
server.init();
|
||||||
System.out.println("hello");
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public TimeServerTest(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
//配置服务端nio线程组
|
//配置服务端nio线程组
|
||||||
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
EventLoopGroup bossGroup = new NioEventLoopGroup();//服务端接受客户端连接
|
||||||
@@ -48,7 +40,7 @@ public class TimeServerTest {
|
|||||||
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
|
||||||
.option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());
|
.option(ChannelOption.SO_BACKLOG, 1024).childHandler(new ChildChannelHandler());
|
||||||
//绑定端口,同步等待成功
|
//绑定端口,同步等待成功
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||||
//等待服务端监听端口关闭
|
//等待服务端监听端口关闭
|
||||||
future.channel().closeFuture().sync();
|
future.channel().closeFuture().sync();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -13,13 +13,11 @@ import io.netty.util.CharsetUtil;
|
|||||||
*/
|
*/
|
||||||
public class UDPClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {
|
public class UDPClientHandler extends SimpleChannelInboundHandler<DatagramPacket> {
|
||||||
|
|
||||||
private static String DICTIONARY = "bbb";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
||||||
String req = packet.content().toString(CharsetUtil.UTF_8);
|
String req = packet.content().toString(CharsetUtil.UTF_8);
|
||||||
System.out.println(req);
|
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()));
|
, packet.sender()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -26,19 +26,12 @@ public class UDPClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
public void clientTest() {
|
||||||
System.out.println("hello");
|
var client = new UDPClientTest();
|
||||||
UDPClientTest server = new UDPClientTest(9999);
|
client.init();
|
||||||
server.init();
|
|
||||||
System.out.println("hello");
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public UDPClientTest(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
//配置服务端nio线程组
|
//配置服务端nio线程组
|
||||||
@@ -54,11 +47,9 @@ public class UDPClientTest {
|
|||||||
|
|
||||||
//向内网的所有机器广播UDP消息
|
//向内网的所有机器广播UDP消息
|
||||||
channel.writeAndFlush(new DatagramPacket(Unpooled.copiedBuffer("client"
|
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)) {
|
channel.closeFuture().await(5000);
|
||||||
System.out.println("查询超时!");
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
@@ -13,14 +13,12 @@ import io.netty.util.CharsetUtil;
|
|||||||
*/
|
*/
|
||||||
public class UDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
|
public class UDPServerHandler extends SimpleChannelInboundHandler<DatagramPacket> {
|
||||||
|
|
||||||
private static String DICTIONARY = "aaa";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket packet) throws Exception {
|
||||||
String req = packet.content().toString(CharsetUtil.UTF_8);
|
String req = packet.content().toString(CharsetUtil.UTF_8);
|
||||||
System.out.println(req);
|
System.out.println(req);
|
||||||
if (req.equals("client")) {
|
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()));
|
, packet.sender()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,19 +22,11 @@ public class UDPServerTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
System.out.println("hello");
|
var server = new UDPServerTest();
|
||||||
UDPServerTest server = new UDPServerTest(9999);
|
|
||||||
server.init();
|
server.init();
|
||||||
System.out.println("hello");
|
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
public UDPServerTest(int port) {
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
//配置服务端nio线程组
|
//配置服务端nio线程组
|
||||||
EventLoopGroup group = new NioEventLoopGroup();//服务端接受客户端连接
|
EventLoopGroup group = new NioEventLoopGroup();//服务端接受客户端连接
|
||||||
@@ -43,7 +35,7 @@ public class UDPServerTest {
|
|||||||
bootstrap.group(group).channel(NioDatagramChannel.class)
|
bootstrap.group(group).channel(NioDatagramChannel.class)
|
||||||
.option(ChannelOption.SO_BROADCAST, true).handler(new UDPServerHandler());
|
.option(ChannelOption.SO_BROADCAST, true).handler(new UDPServerHandler());
|
||||||
//绑定端口,同步等待成功
|
//绑定端口,同步等待成功
|
||||||
ChannelFuture future = bootstrap.bind(port).sync();
|
ChannelFuture future = bootstrap.bind(9999).sync();
|
||||||
//等待服务端监听端口关闭
|
//等待服务端监听端口关闭
|
||||||
future.channel().closeFuture().await();
|
future.channel().closeFuture().await();
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
|
|||||||
@@ -17,29 +17,23 @@ public class NioClientTest implements Runnable {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clientTest() {
|
public void clientTest() {
|
||||||
NioClientTest nioClient = new NioClientTest("localhost", 9999);
|
var nioClient = new NioClientTest();
|
||||||
nioClient.init();
|
nioClient.init();
|
||||||
new Thread(nioClient, "clinetThread").start();
|
new Thread(nioClient, "clinetThread").start();
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String ip;
|
|
||||||
private int port;
|
|
||||||
|
|
||||||
private Selector selector;
|
private Selector selector;
|
||||||
private SocketChannel channel;
|
private SocketChannel channel;
|
||||||
private volatile boolean stop;
|
private volatile boolean stop;
|
||||||
|
|
||||||
public NioClientTest(String ip, int port) {
|
|
||||||
this.ip = ip;
|
|
||||||
this.port = port;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void init() {
|
public void init() {
|
||||||
try {
|
try {
|
||||||
channel = SocketChannel.open();
|
channel = SocketChannel.open();
|
||||||
channel.configureBlocking(false);
|
channel.configureBlocking(false);
|
||||||
channel.connect(new InetSocketAddress(ip, port));
|
channel.connect(new InetSocketAddress("localhost", 9999));
|
||||||
|
|
||||||
this.selector = Selector.open();
|
this.selector = Selector.open();
|
||||||
channel.register(selector, SelectionKey.OP_CONNECT);
|
channel.register(selector, SelectionKey.OP_CONNECT);
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ public class NioServerTest implements Runnable {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void serverTest() {
|
public void serverTest() {
|
||||||
NioServerTest nioServer = new NioServerTest();
|
var nioServer = new NioServerTest();
|
||||||
nioServer.init(9999);
|
nioServer.init(9999);
|
||||||
new Thread(nioServer, "serverThread").start();
|
new Thread(nioServer, "serverThread").start();
|
||||||
ThreadUtils.sleep(Long.MAX_VALUE);
|
ThreadUtils.sleep(Long.MAX_VALUE);
|
||||||
|
|||||||
Reference in new issue
Block a user