Netty创建UCP
客户端:
public class NettyClient {
public static void main(String[] args) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioDatagramChannel.class)
.handler(new MyChannelInitializer()); // 使用自定义初始化器
Channel ch = b.bind(7398).sync().channel();
ch.writeAndFlush(new DatagramPacket(
Unpooled.copiedBuffer("你好端口7397的bugstack虫洞栈,我是客户端小爱,你在吗!", Charset.forName("GBK")),
new InetSocketAddress("127.0.0.1", 7397))).sync();
ch.closeFuture().await();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
}
服务端:
public class NettyServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioDatagramChannel.class)
.option(ChannelOption.SO_BROADCAST, true) // 允许广播
.handler(new ChannelInitializer<NioDatagramChannel>() {
@Override
protected void initChannel(NioDatagramChannel ch) {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new MyServerHandler()); // 自定义处理器
}
});
ChannelFuture f = b.bind(7397).sync();
System.out.println("itstack-demo-netty udp server start done.");
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}
Netty创建TCP
服务端:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class TcpServer {
private final int port;
public TcpServer(int port) {
this.port = port;
}
public void run() throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // 接受连接的线程组
EventLoopGroup workerGroup = new NioEventLoopGroup(); // 处理连接的线程组
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
// 这里添加自定义的ChannelHandler
}
});
// 绑定端口并启动服务器
ChannelFuture f = b.bind(port).sync();
System.out.println("服务器启动在端口: " + port);
f.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) {
new TcpServer(8080).run();
}
}
客服端:
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
public class TcpClient {
private final String host;
private final int port;
public TcpClient(String host, int port) {
this.host = host;
this.port = port;
}
public void run() throws InterruptedException {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
// 这里添加自定义的ChannelHandler
}
});
// 连接到服务器
ChannelFuture f = b.connect(host, port).sync();
System.out.println("客户端连接到 " + host + ":" + port);
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) {
new TcpClient("127.0.0.1", 8080).run();
}
}
标签:Netty,group,netty,port,new,public,channel
From: https://www.cnblogs.com/firsthelloworld/p/18383192