相关文章
更多最近更新
更多Netty入门示例
2021-05-23 17:28|来源: 网路
服务端
package com._656463.netty.ch01;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class HelloServer {
public static void main(String[] args) {
//创建Boss和work的NioEventLoopGroup,两个都算无限循环
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workGroup = new NioEventLoopGroup();
try {
//创建服务端启动对象,配置参数
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap
//设置线程组
.group(bossGroup, workGroup)
//使用NioServerSocketChannel作为服务端的通道实现
.channel(NioServerSocketChannel.class)
//设置线程队列的连接个数
.option(ChannelOption.SO_BACKLOG, 128)
//设置保存活动的连接状态
.childOption(ChannelOption.SO_KEEPALIVE, true)
//创建一个通道初始对象
.childHandler(new ChannelInitializer<SocketChannel>() {
//给pipeline设置处理器
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HelloServerHandler());
}
});
ChannelFuture future = bootstrap.bind(8080).sync();
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
服务端处理器
package com_656463.netty.ch01;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import java.util.concurrent.TimeUnit;
/**
* 自定义Handler
*/
public class HelloServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("server channelRead..");
ByteBuf buf = (ByteBuf) msg;
System.out.println("客户端【" + ctx.channel().remoteAddress() + "】发送消息 :" + buf.toString(CharsetUtil.UTF_8));
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("hello client", CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
客户端
package com_656463.netty.ch01;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class HelloClient {
public static void main(String[] args) {
NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
Bootstrap bootstrap = new Bootstrap();
try {
bootstrap.group(eventExecutors)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HelloClientHandler());
}
});
ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 8080).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
eventExecutors.shutdownGracefully();
}
}
}
客户端处理类
package com_656463.netty.ch01;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
/**
* 自定义Handler
*/
public class HelloClientHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ctx.writeAndFlush(Unpooled.copiedBuffer("hello server",CharsetUtil.UTF_8));
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("server channelRead..");
ByteBuf buf = (ByteBuf) msg;
System.out.println("服务端【" + ctx.channel().remoteAddress() + "】回复消息 :" + buf.toString(CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
相关问答
更多-
netty如何使用[2022-05-14]
和spring , 先写一个类,然后与一般的bean同样配置当您看到org.jboss.netty命名空间时,表示v3.x io.netty用于v4。 在线文档位于http://netty.io/docs/ When you see the org.jboss.netty namespace, that indicates v3.x io.netty is used in v4. Online documentation is located at http://netty.io/docs/Netty Camel样品(Netty Camel samples)[2023-06-08]
我假设您仍想与Camel集成。 我先来看看骆驼文档 。 在此之后,你需要开始尝试。 我有一个例子,我创建了一个Camel处理器作为Netty服务器。 Netty组件的工作原理是From端点是消耗的服务器,To端点是生成的客户端。 我需要一个To端点,它是一个服务器,组件不支持它。 我只是将Camel Processor实现为一个Spring bean,它在初始化时启动了Netty Server。 JBoss Netty文档和示例非常好。 值得一提的是。 这是我瘦身的例子。 它是一个向所有连接的客户端发送消息 ...如果要运行4.0.27.Final的示例,则需要确保使用作为发布标记一部分的示例。 所以使用这些: https://github.com/netty/netty/tree/netty-4.0.27.Final/example/src/main/java/io/netty/example If you want to run examples with 4.0.27.Final you need to ensure you use the examples which are part of the rele ...在这篇文章中你可以找到你回答,在Netty 3.X中,这些包来自org.jboss.netty。* http://netty.io/3.10/api/index.html 但是从Netty 4.X开始,包装来自io.netty。*参见: http ://netty.io/4.0/api/index.html 是的,版本3和版本4有几个不同之处。 我建议您使用稳定版本进行开发,请在http://netty.io/wiki/index.html中查看 In this post you could find yo ...将byte[]传递给Netty write方法相当于传递未加密的ByteBuf 。 我们可以在ByteArrayEncoder的源代码中看到这一点,它在内部调用Unpooled#wrappedBuffer(byte[]) : @Override protected void encode(ChannelHandlerContext ctx, byte[] msg, ListNetty处理HTTP请求(Netty Handle HTTP requests)[2023-07-16]
如果您编写后端servlet应用程序,那么使用Netty作为应用程序的后端有什么意义呢? 您可以使用其中一个接收HTTP请求并处理它们。 由于您正在尝试使用Netty,因此您应该在处理程序本身内处理HTTP请求,然后仅从那里发出HTTP响应。 使用tomcat将是一种矫枉过正。 If you write a backend servlet application then what is the point of using Netty as a backend for your application ? ...没有理由,这很可能是错过了被删除。 我将清理示例以将其删除。 谢谢你让我们知道 There is not reason and this was most likely missed to be removed. I will clean up the example to remove it. Thanks for let us know请参阅netty的“示例”[ 2 ]部分中的安全聊天[ 1 ]示例。 Refer the secure chat[1] example in the "examples"[2] section of netty.Netty HttpServer api改变/与可用示例不同(Netty HttpServer api changed/differs from available examples)[2023-08-28]
目前建立项目的推荐方法是使用http://start.spring.io/,正如Josh Long在他的视频中所建议的那样。 这是因为spring reactive现在只是候选版本,我们需要兼容的版本来运行samples.This是通过将这个部分添加到代码来实现的:org.springframework.boot.experimen ...