一 准备protobuf环境
1 下载protobuf-2.6.1,解压,进入vsprojects目录,用VC打开工程,编译出 protoc.exe
2 生成.proto文件
message TestMsg
{
required string name = 1;
}
放到自己的目录,此例子中放到了f:根目录下。
然后执行:
protoc.exe -I=f:\ --python_out=f:\ f:\TestMsgProto.proto
生成 TestMsgProto_pb2.py,给python用
protoc.exe -I=f:\ --java_out=f:\ f:\TestMsgProto.proto
生成TestMsgProto.java,给java工程用
3 生成对应c++,python,java的class文件
二 python
1 copy protoc.exe到protobuf-2.6.1/python
2 python setup.py build
3 python setup.py install
4 测试用客户端代码:
import google.protobuf
import TestMsgProto_pb2
import socket
address = ('127.0.0.1', 8800)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(address)
msg = TestMsgProto_pb2.TestMsg()
msg.name='name'
test_str = msg.SerializeToString()
print test_str
ret=s.send(test_str)
print ret
s.close()
三 java环境
可参考:https://github.com/vincepeng/gameNettyDemo
主要看 https://github.com/vincepeng/gameNettyDemo/tree/master/GameNettyServer/bin/com/server/java/netty
这下面的几个文件,可以直接放到自己工程里,或者自己稍改动一下。
注意事项:
新建myeclipse maven工程,pom.xml增加:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.30.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.6.1</version>
</dependency>
然后,主要处理:decoder和encoder,相比gameNettyDemo做了些减化,下面是做过修改的代码:
//NettyMsgEncoder
public class NettyMsgEncoder extends MessageToByteEncoder<String> {
@Override
protected void encode(ChannelHandlerContext ctx, String name, ByteBuf byteBuf) throws Exception {
TestMsgProto.TestMsg.Builder builder = TestMsgProto.TestMsg.newBuilder();
builder.setName(name);
TestMsgProto.TestMsg msg = builder.build();
byteBuf.writeBytes(msg.toByteArray());
}
}
// NettyMsgDecoder
public class NettyMsgDecoder extends LengthFieldBasedFrameDecoder {
//以下两个构造函数,在本例中用不上
public NettyMsgDecoder(ByteOrder byteOrder, int maxFrameLength, int lengthFieldOffset, int lengthFieldLength, int lengthAdjustment, int initialBytesToStrip, boolean failFast) {
super(byteOrder, maxFrameLength, lengthFieldOffset, lengthFieldLength, lengthAdjustment, initialBytesToStrip, failFast);
}
public NettyMsgDecoder() {
this(ByteOrder.BIG_ENDIAN, 100000, 0, 4, 2, 4, true);
}
@Override
protected Object decode(ChannelHandlerContext ctx, ByteBuf byteBuf) throws Exception {
if (byteBuf != null && byteBuf.readableBytes()>0){
int len = byteBuf.readableBytes();
byte[] data = new byte[len];
byteBuf.readBytes(data);
TestMsgProto.TestMsg msg = TestMsgProto.TestMsg.parseFrom(data);
//return msg.getName();
return msg;
}
return null;
}
}
//NettyServerInitializer
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new NettyMsgDecoder())// 解码器
.addLast("encoder", new NettyMsgEncoder())// 编码器
.addLast("handler", new ServerHanlder());
}
}
//ServerHanlder
@Sharable
public class ServerHanlder extends SimpleChannelInboundHandler<TestMsgProto.TestMsg> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, TestMsgProto.TestMsg msg)
throws Exception {
if (msg != null)
System.out.println(msg.getName());
}
}
//SimpleChatServer
public class SimpleChatServer {
private int port;
public SimpleChatServer(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
//.childHandler(new SimpleChatServerInitializer())
.childHandler(new NettyServerInitializer())
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture f = b.bind(port).sync(); // (7)
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
System.out.println("SimpleChatServer 关闭了");
}
}
}
然后,在Myeclipse里运行java服务器程序
再启动python 远程连接,看接收情况。
从代码上看,小项目用python是多么方便。
上面例子只有java netty的server和python的client .
稍做修改,就能实现python的server和java的client,复杂的依然复杂,简洁的依然简洁。