netty实现对象传输
Maven
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hik.nettydemo</groupId>
<artifactId>nettydemo</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha2</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling</artifactId>
<version>1.4.10.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.marshalling</groupId>
<artifactId>jboss-marshalling-serial</artifactId>
<version>1.4.10.Final</version>
</dependency>
</dependencies>
</project>
Server.java
package com.hik.netty.Server;
import com.hik.netty.Common.MarshallingCodeCFactory;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
public class Server {
public static ChannelHandler marshallingEncoderCache;
public static void main(String[] args) {
new Server().bind(9999);
}
public void bind(int port) {
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
try {
marshallingEncoderCache = MarshallingCodeCFactory.buildMarshallingEncoder();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO))
.childHandler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("marencoder", marshallingEncoderCache);
ch.pipeline().addLast(MarshallingCodeCFactory.buildMarshallingDecoder());
ch.pipeline().addLast("chunkedWriteHandler", new ChunkedWriteHandler());
ch.pipeline().addLast("ServerHandler", new ServerHandler());
}
});
ChannelFuture f = serverBootstrap.bind(port).sync();
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
ServerHandler.java
package com.hik.netty.Server;
import com.hik.netty.POJO.Request;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
public class ServerHandler extends ChannelHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Request request = (Request) msg;
System.out.println(request.getActionCode());
System.out.println(request.getIp());
System.out.println(request.getReqMes());
System.out.println(new String(request.getData()));
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
}
}
Client.java
package com.hik.netty.Client;
import com.hik.netty.Common.MarshallingCodeCFactory;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
public class Client {
public static String ip;
public static void main(String[] args) throws Exception {
int port = 9999;
String host = "127.0.0.1";
if (args != null && args.length > 0) {
try {
host = args[0];
port = Integer.valueOf(args[1]);
} catch (NumberFormatException e) {
}
}
new Client().connect(port, host);
}
public void connect(int port, String host) throws Exception {
NioEventLoopGroup nioEventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(nioEventLoopGroup).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("marDecoder", MarshallingCodeCFactory.buildMarshallingDecoder());
ch.pipeline().addLast("marEncoder", MarshallingCodeCFactory.buildMarshallingEncoder());
ch.pipeline().addLast(new ClientHandler());
}
});
ChannelFuture f = bootstrap.connect(host, port).sync();
f.channel().closeFuture().sync();
} finally {
nioEventLoopGroup.shutdownGracefully();
}
}
}
ClientHandler.java
package com.hik.netty.Client;
import com.hik.netty.POJO.Request;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;
public class ClientHandler extends ChannelHandlerAdapter {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
Request request = new Request();
request.setActionCode(1);
request.setIp("127.0.0.1");
request.setReqMes("request");
request.setData("helloworld".getBytes());
ctx.writeAndFlush(request);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
}
}
Request.java
package com.hik.netty.POJO;
import java.io.Serializable;
public class Request implements Serializable {
private static final long serialVersionUID = 1L;
String ip;
byte[] data;
int actionCode;
String reqMes;
public int getActionCode() {
return actionCode;
}
public void setActionCode(int actionCode) {
this.actionCode = actionCode;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public String getReqMes() {
return reqMes;
}
public void setReqMes(String reqMes) {
this.reqMes = reqMes;
}
}
Response.java
package com.hik.netty.POJO;
import java.io.Serializable;
public class Response implements Serializable {
private static final long serialVersionUID = 1L;
Request req;
String resMessage;
String resType;
String resCode;
// Object deleteFiles;
// Object newFiles;
// Object overrideFiles;
public byte[] data;
public String getResType() {
return resType;
}
public void setResType(String resType) {
this.resType = resType;
}
public String getResCode() {
return resCode;
}
public void setResCode(String resCode) {
this.resCode = resCode;
}
// public Object getDeleteFiles() {
// return deleteFiles;
// }
//
// public void setDeleteFiles(Object deleteFiles) {
// this.deleteFiles = deleteFiles;
// }
//
// public Object getNewFiles() {
// return newFiles;
// }
//
// public void setNewFiles(Object newFiles) {
// this.newFiles = newFiles;
// }
//
// public Object getOverrideFiles() {
// return overrideFiles;
// }
//
// public void setOverrideFiles(Object overrideFiles) {
// this.overrideFiles = overrideFiles;
// }
public Request getReq() {
return req;
}
public void setReq(Request req) {
this.req = req;
}
public String getResMessage() {
return resMessage;
}
public void setResMessage(String resMessage) {
this.resMessage = resMessage;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
}