http://hbxflihua.iteye.com/blog/2231878
本实例使用netty4+protobuf-2.5.0,在win7下执行,并且假设已经安装jdk和maven。
1、下载Protobuf的Windows版,网址如下:https://developers.google.com/protocol-buffers/docs/downloads?hl=zh-cn,本示例基于protoc-2.6.1-win32.zip
2、下载Protobuf Java语言所需的jar包,网址如下:http://repo2.maven.org/maven2/com/google/protobuf/protobuf-java/2.6.1/,本示例基于protobuf-java-2.6.1.jar。
下载并解压protoc-2.5.0-win32.zip和protobuf-2.5.0.zip
到protobuf-2.5.0.zip安装目录protobuf-2.5.0\Java下,执行maven命令:mvn package jar:jar,将生成target\protobuf-java-2.5.0.jar
3、定义proto文件test.proto:
package domain;
option java_package = "com.server.domain";
message TestPro {
required string test = 1;
}
4、将第2部的jar包拷贝到java文件存放目录下,然后运行下面命令,生成java文件:
protoc-2.5.0-win32.zip安装目录\protoc.exe --java_out=java文件存放目录 proto定义文件目录\test.proto
5、将生成的protobuf-java-2.5.0.jar和netty4的jar包放到项目的classpath中,并将第4部生成的java文件放到项目的相应路径下。
6、编写Server端代码
1)、编写handler类:
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class ServerHandler extends SimpleChannelInboundHandler<Test.TestPro> {
@Override
public void channelRead0(ChannelHandlerContext ctx, Test.TestPro msg) throws Exception {
System.out.println("server:" + "channelRead:" + msg.getTest());
Test.TestPro.Builder builder = Test.TestPro.newBuilder();
builder.setTest("Received your message !");
ctx.writeAndFlush(builder.build());
}
}
2)、注册服务,绑定端口:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
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.nio.NioServerSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
public class Server {
public static void main(String[] args) {
EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup();
EventLoopGroup workerEventLoopGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossEventLoopGroup, workerEventLoopGroup);
serverBootstrap.channel(NioServerSocketChannel.class);
serverBootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast("encoder", new ProtobufEncoder());
ch.pipeline().addLast("decoder", new ProtobufDecoder(Test.TestPro.getDefaultInstance()));
ch.pipeline().addLast("handler", new ServerHandler());
};
});
serverBootstrap.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossEventLoopGroup.shutdownGracefully();
workerEventLoopGroup.shutdownGracefully();
}
}
}
7、编写Client端代码
1)、定义Client端的handler类:
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class ClientHandler extends SimpleChannelInboundHandler<Test.TestPro> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Test.TestPro msg) throws Exception {
System.out.println("client:" + "channelRead:" + msg.getTest());
}
}
2)、建立Client端到Server端的连接
import java.io.BufferedReader;
import java.io.InputStreamReader;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.protobuf.ProtobufDecoder;
import io.netty.handler.codec.protobuf.ProtobufEncoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class Client {
public static void main(String[] args) {
EventLoopGroup eventLoopGroup = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(eventLoopGroup);
bootstrap.channel(NioSocketChannel.class);
bootstrap.handler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast("encoder", new ProtobufEncoder());
ch.pipeline().addLast("decoder", new ProtobufDecoder(Test.TestPro.getDefaultInstance()));
ch.pipeline().addLast("handler", new ClientHandler());
};
});
Channel ch = bootstrap.connect("127.0.0.1", 8888).sync().channel();
// 控制台输入
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for (;;) {
String line = in.readLine();
if (line == null || "".equals(line)) {
continue;
}
Test.TestPro.Builder builder = Test.TestPro.newBuilder();
builder.setTest(line);
ch.writeAndFlush(builder.build());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
eventLoopGroup.shutdownGracefully();
}
}
}
http://hbxflihua.iteye.com/blog/2231878
相关推荐
来自于疯狂创客圈 《netty+protobuf 整合实战》的源代码,付上了 protobuf 的 protoc 工具, protoc-2.6.1-win32.zip
客户端与服务端通信,协议用protoBuf。maven项目,其中有startClient与startServer两个mainClass。不懂的可以留言
基于Netty+Redis+protobuf开发的即时通讯服务器 第一版是一个简版,只实现了很少的功能,现在已经迁移到 [v1.0](https://github.com/linyu19872008/cdeer-im/tree/v1.0) 第二版的目标有以下几点: 1 工程用...
通过学习netty + protobuf 开发小项聊天程序的例子,可以掌握 netty 的开发 和 协议栈的设计等
基于Netty+Redis+protobuf开发的即时通讯服务器.zip
springboot集成netty,使用protobuf作为数据交换格式,可以用于智能终端云端服务脚手架。
cdeer-im, 基于Netty+Redis+protobuf开发的即时通讯服务器
基于Netty+TCP+Protobuf实现的Android IM库,包含Protobuf序列化、TCP拆包与粘包、长连接握手认证、心跳机制、断线重连机制、消息重发机制、读写超时机制、离线消息、线程池等功能
通信与协议Netty+Protobuf-游戏设计与开发(1)配套代码
自定义Protobuf的编解码器,和官方的只能发送一个消息格式比较,能发送多个protobuf对象,protobuf文件夹下有生成protobuf的工具和使用说明
从网友那里拿到的共享代码,也没怎么细看,希望可以帮助到初学的朋友们。
java netty 服务端 + unity客户端 +protobuf3 实现网游通讯demo
netty服务器源代码,通信协议使用protobuf
免费下载,适合做毕业设计,课程设计等。所有源码均经过测试,可直接运行,免费下载,下就完事了
Netty发送protoBuf格式数据 Netty发送protoBuf格式数据 Netty发送protoBuf格式数据 Netty发送protoBuf格式数据
Netty中集成Protobuf实现Java对象数据传递示例代码.rar
基于Netty+TCP+Protobuf+okhttp设计模式的SDK,让你拥有像Okhttp一样的使用体验,完全可定制化,内置断线重连,路由自动切换、消息重复、连接超时、读写超时、可定制化拦截器、消息回执(已读,撤回等)、可定制化...
本实例是《Netty 粘包/半包原理与拆包...具体的文章为: Netty +Protobuf 整合二:protobuf 消息通讯协议设计的几个准则 在开始聊天器实战开发之前,还有一个非常基础的问题,需要解决:这就是通讯的粘包和半包问题。
Netty:它使 NIO 编程更加容易,屏蔽了 Java 底层的 NIO 细节。 Protostuff:它基于 Protobuf 序列化框架,面向 POJO,无需编写 .proto 文件。 ZooKeeper:提供服务注册与发现功能,开发分布式系统的必备选择,同时...