Netty4 服务器 客户端 传输对象
2020-12-13 05:43
标签:blog class c code java ext 关键词:netty4 对象 序列化 废话少说,直接上代码了 Client.java ClientHandler.java Server.java ServerHandler.java Student.java.需要传输的对象。别忘了implements Serializable Netty4 服务器 客户端 传输对象,搜素材,soscw.com Netty4 服务器 客户端 传输对象 标签:blog class c code java ext 原文地址:http://www.cnblogs.com/microsoftmvp/p/3738365.html
import
io.netty.bootstrap.Bootstrap;
import
io.netty.channel.*;
import
io.netty.channel.nio.NioEventLoopGroup;
import
io.netty.channel.socket.SocketChannel;
import
io.netty.channel.socket.nio.NioSocketChannel;
import
io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import
io.netty.handler.codec.LengthFieldPrepender;
import
io.netty.handler.codec.serialization.ClassResolvers;
import
io.netty.handler.codec.serialization.ObjectDecoder;
import
io.netty.handler.codec.serialization.ObjectEncoder;
public
class
Client
{
public
Student SendAndGet(String ip,
int
port,Student s)
{
EventLoopGroup group =
new
NioEventLoopGroup();
Student ret=
null
;
try
{
Bootstrap b =
new
Bootstrap();
b.group(group);
final
ClientHandler chl=
new
ClientHandler();
b.channel(NioSocketChannel.
class
).option(ChannelOption.TCP_NODELAY,
true
);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS,
10000
);
b.handler(
new
ChannelInitializer
@Override
protected
void
initChannel(SocketChannel ch)
throws
Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(
"frameDecoder"
,
new
LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,
0
,
4
,
0
,
4
));
pipeline.addLast(
"frameEncoder"
,
new
LengthFieldPrepender(
4
));
pipeline.addLast(
"encode"
,
new
ObjectEncoder());
pipeline.addLast(
"decode"
,
new
ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(
null
)));
pipeline.addLast(
"handler"
, chl);
}
});
ChannelFuture f = b.connect(ip, port).sync();
f.channel().writeAndFlush(s);
f.channel().closeFuture().sync();
ret=chl.getMessage();
}
catch
(Exception e)
{
e.printStackTrace();
}
finally
{
group.shutdownGracefully();
}
return
ret;
}
public
static
void
main(String[] args)
throws
Exception {
Student s=
new
Student(
"hello"
,
23
);
Student g=
new
Client().SendAndGet(
"localhost"
,
9988
,s);
}
}
import
io.netty.channel.ChannelHandlerContext;
import
io.netty.channel.ChannelInboundHandlerAdapter;
public
class
ClientHandler
extends
ChannelInboundHandlerAdapter
{
private
Student student;
@Override
public
void
channelRead(ChannelHandlerContext ctx, Object msg)
throws
Exception
{
System.out.println(
"client接收到服务器返回的消息"
);
student=(Student)msg;
}
public
Student getMessage()
{
return
this
.student;
}
@Override
public
void
exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws
Exception
{
System.out.println(
"client exception is general"
);
}
}
import
io.netty.bootstrap.ServerBootstrap;
import
io.netty.channel.ChannelFuture;
import
io.netty.channel.ChannelInitializer;
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.NioServerSocketChannel;
import
io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import
io.netty.handler.codec.LengthFieldPrepender;
import
io.netty.handler.codec.serialization.ClassResolvers;
import
io.netty.handler.codec.serialization.ObjectDecoder;
import
io.netty.handler.codec.serialization.ObjectEncoder;
public
class
Server {
private
static
final
String IP =
"localhost"
;
private
static
final
int
PORT =
9988
;
protected
static
final
int
BIZGROUPSIZE = Runtime.getRuntime().availableProcessors() *
2
;
protected
static
final
int
BIZTHREADSIZE =
4
;
private
static
final
EventLoopGroup bossGroup =
new
NioEventLoopGroup(BIZGROUPSIZE);
private
static
final
EventLoopGroup workerGroup =
new
NioEventLoopGroup(BIZTHREADSIZE);
protected
void
run()
throws
Exception {
try
{
ServerBootstrap b =
new
ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.
class
);
b.option(ChannelOption.SO_BACKLOG,
1000000
);
b.childHandler(
new
ChannelInitializer
@Override
public
void
initChannel(SocketChannel ch)
throws
Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(
new
LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,
0
,
4
,
0
,
4
));
pipeline.addLast(
new
LengthFieldPrepender(
4
));
pipeline.addLast(
"encode"
,
new
ObjectEncoder());
pipeline.addLast(
"decode"
,
new
ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(
null
)));
pipeline.addLast(
new
ServerHandler());
}
});
ChannelFuture f = b.bind(IP, PORT).sync();
f.channel().closeFuture().sync();
}
finally
{
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public
static
void
main(String[] args)
throws
Exception {
System.out.println(
"开始启动服务器..."
);
new
Server().run();
// HelloServer.shutdown();
}
}
import
io.netty.channel.ChannelHandlerContext;
import
io.netty.channel.ChannelInboundHandlerAdapter;
public
class
ServerHandler
extends
ChannelInboundHandlerAdapter
{
@Override
public
void
channelRead(ChannelHandlerContext ctx, Object msg)
throws
Exception
{
Student s=(Student)msg;
System.out.println(
"SERVER接收到消息"
);
ctx.channel().writeAndFlush(
new
Student(
"world"
,
23
));
ctx.close();
}
@Override
public
void
channelActive(ChannelHandlerContext ctx)
throws
Exception
{
System.out.println(
">>>>>>>>"
);
}
@Override
public
void
exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws
Exception
{
System.out.println(
"exception is general"
);
}
}
import
java.io.Serializable;
public
class
Student
implements
Serializable
{
/**
*
*/
private
static
final
long
serialVersionUID = 1L;
private
String name;
private
int
age;
public
Student(String name,
int
age)
{
this
.name=name;
this
.age=age;
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
int
getAge() {
return
age;
}
public
void
setAge(
int
age) {
this
.age = age;
}
}