This commit is contained in:
2023-03-04 16:29:55 +08:00
commit 397ba75479
1007 changed files with 109050 additions and 0 deletions

View File

@@ -0,0 +1,109 @@
package com.jsowell.netty.client;
import com.jsowell.common.constant.Constants;
import com.jsowell.common.util.DateUtils;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Data
public class NettyClient implements Runnable {
static final String HOST = System.getProperty("host", Constants.SOCKET_IP);
static final int PORT = Integer.parseInt(System.getProperty("port", "9011"));
static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
private String content;
public NettyClient(String content) {
this.content = content;
}
@Override
public void run() {
// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
try {
int num = 0;
boolean boo = true;
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new NettyClientChannelInitializer() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("decoder", new StringDecoder());
p.addLast("encoder", new StringEncoder());
p.addLast(new NettyClientHandler());
}
});
ChannelFuture future = b.connect(HOST, PORT).sync();
while (boo) {
num++;
future.channel().writeAndFlush("发送数据=======" + content + "--" + DateUtils.getTime());
try { //休眠一段时间
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//每一条线程向服务端发送的次数
if (num == 5) {
boo = false;
}
}
log.info(content + "-----------------------------" + num);
//future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
/**
* 下面是不加线程的
*/
/*public static void main(String[] args) throws Exception {
sendMessage("hhh你好");
}
public static void sendMessage(String content) throws InterruptedException {
// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new NettyClientChannelInitializer() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("decoder", new StringDecoder());
p.addLast("encoder", new StringEncoder());
p.addLast(new NettyClientHandler());
}
});
ChannelFuture future = b.connect(HOST, PORT).sync();
future.channel().writeAndFlush(content);
future.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}*/
}

View File

@@ -0,0 +1,23 @@
package com.jsowell.netty.client;
import com.jsowell.netty.server.yunkuaichong.NettyServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;
/**
* 客户端初始化,客户端与服务器端连接一旦创建,这个类中方法就会被回调,设置出站编码器和入站解码器,客户端服务端编解码要一致
*/
public class NettyClientChannelInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel channel) throws Exception {
channel.pipeline().addLast("decoder",new StringDecoder(CharsetUtil.UTF_8));
channel.pipeline().addLast("encoder",new StringEncoder(CharsetUtil.UTF_8));
channel.pipeline().addLast(new NettyServerHandler());
}
}

View File

@@ -0,0 +1,71 @@
package com.jsowell.netty.client;
import com.jsowell.common.util.DateUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelId;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.ConcurrentHashMap;
/**
* 客户端处理类
*/
@Slf4j
public class NettyClientHandler extends ChannelInboundHandlerAdapter {
/**
* 计算有多少客户端接入第一个string为客户端ip
*/
private static final ConcurrentHashMap<ChannelId, ChannelHandlerContext> CLIENT_MAP = new ConcurrentHashMap<>();
@Override
public void channelActive(ChannelHandlerContext ctx) {
CLIENT_MAP.put(ctx.channel().id(), ctx);
log.info("ClientHandler Active");
}
/**
* @param ctx
* @author xiongchuan on 2019/4/28 16:10
* @DESCRIPTION: 有服务端端终止连接服务器会触发此函数
* @return: void
*/
@Override
public void channelInactive(ChannelHandlerContext ctx) {
ctx.close();
log.info("服务端终止了服务");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
log.info("回写数据:" + msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
//cause.printStackTrace();
log.info("服务端发生异常【" + cause.getMessage() + "");
ctx.close();
}
/**
* @param msg 需要发送的消息内容
* @param channelId 连接通道唯一id
* @author xiongchuan on 2019/4/28 16:10
* @DESCRIPTION: 客户端给服务端发送消息
* @return: void
*/
public void channelWrite(ChannelId channelId, String msg) {
ChannelHandlerContext ctx = CLIENT_MAP.get(channelId);
if (ctx == null) {
log.info("通道【" + channelId + "】不存在");
return;
}
//将客户端的信息直接返回写入ctx
ctx.write(msg + " 时间:" + DateUtils.getTime());
//刷新缓存区
ctx.flush();
}
}

View File

@@ -0,0 +1,14 @@
package com.jsowell.netty.client;
/**
* 模拟多客户端发送报文
*/
public class TestNettyClient {
public static void main(String[] args) {
//开启10条线程每条线程就相当于一个客户端
for (int i = 1; i <= 300; i++) {
new Thread(new NettyClient("thread" + "--" + i)).start();
}
}
}