关键词:netty4 对象 序列化
废话少说,直接上代码了
Client.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 |
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<SocketChannel>() { @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); } } |
ClientHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 |
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" ); } } |
Server.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56 |
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<SocketChannel>() { @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(); } } |
ServerHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 |
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" ); } } |
Student.java.需要传输的对象。别忘了implements Serializable
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 |
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; } } |
Netty4 服务器 客户端 传输对象,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/microsoftmvp/p/3738365.html