一 准备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环境
可参考: />
主要看 />
这下面的几个文件,可以直接放到自己工程里,或者自己稍改动一下。
注意事项:
新建myeclipse maven工程,pom.xml增加:
io.netty
netty-all
4.0.30.final
compile
com.google.protobuf
protobuf-java
2.6.1
然后,主要处理:decoder和encoder,相比gamenettydemo做了些减化,下面是做过修改的代码:
//nettymsgencoder
public class nettymsgencoder extends messagetobyteencoder
{
@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 {
@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 {
@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,复杂的依然复杂,简洁的依然简洁。
阅读(4375) | 评论(0) | 转发(0) |