protobuf 是什么?html
Protocol buffers是一种编码方法构造的一种有效而可扩展的格式的数据。 谷歌使用其内部几乎RPC协议和文件格式的全部协议缓冲区。java
参考文档android
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview.html c++
API的 参考文档 web
protobuf 适用的语言c#
正宗(Google 本身内部用的)的protobuf支持三种语言:Java 、c++和Pyton,很遗憾的是并不支持.Net 或者 Lua 等语言,但社区的力量是不容忽视的,因为protobuf确实比Json、XML有速度上的优点和使用的方便,而且能够作到向前兼容、向后兼容等众多特色,因此protobuf社区又弄了个protobuf.net的组件而且还支持众多语言,详细能够看这个连接:http://code.google.com/p/protobuf/wiki/ThirdPartyAddOns,具体某种语言的使用请各自对号入座,本篇只是讲使用android 与c++服务器通信(测试过)或者与PC 通信,使用java与C#之间互相通信方面的DEMO,方面读者作参考。api
使用protobuf协议
定义protobuf协议 服务器
定义protobuf协议必须建立一个以.proto为后缀的文件,以本篇为例,本篇建立了一个叫msg.proto的消息文件,内容以下:app
package msginfo;
message CMsg
{
required
string
msghead
=
1
;
required
string
msgbody
=
2
;
}
message CMsgHead
{
required int32 msglen
=
1
;
required int32 msgtype
=
2
;
required int32 msgseq
=
3
;
required int32 termversion
=
4
;
required int32 msgres
=
5
;
required
string
termid
=
6
;
}
message CMsgReg
{
optional int32 area
=
1
;
optional int32 region
=
2
;
optional int32 shop
=
3
;
optional int32 ret
=
4
;
optional
string
termid
=
5
[defalut
=
"
12345
"
];
}
message CMsgLogin
{
optional int32 ret
=
1
;
}
message CMsgLogout
{
optional int32 ret
=
1
;
}socket
package在Java里面表明这个文件所在的包名,在c#里面表明该文件的命名空间,message表明一个类,
required 表明该字段必填,optional 表明该字段可选,并能够为其设置默认值,默认值格式 :[defalut=字符串就是"123" ,整型就是 123]。
如何编译该proto文件
java或android 使用的编译方法
正宗的proto能够在Linux下编译也有提供win版编译,因为Linux下编译要配置什么g++呀,之类的有点麻烦,以前作的步骤都忘得差很少,那仍是回到win版编译吧,而net 版则是须要在win版下编译。
正宗google 的protobuf 下载列表请参照:http://code.google.com/p/protobuf/downloads/list ,选择其中的win版本下载。解压后会获得一个protoc.exe 文件,此时就能够开始编译了,先以java 为例,编译的步骤以下:
- cmd 打开命令工具
- 以我电脑为例,该exe 文件我放在F:\protoc 目录下,先cd 到该目录 cd F:\protoc
- 再次进入目录后会发现该目录多了一个文件夹,即以该proto的package命名的的目录,会产生一个Msg.java的文件,这时这个文件就能够使用到咱们的java或者 android 工程了。
- 最后一步下载一个protobuf-java-2.3.0.jar的jar 包引用到你的java和android工程 里面,OK。能够使用你的protobuf了。以下图:

c#或者之后的Windows Phone 7 使用的编译方法:
.net 版的protobuf来源于proto社区,有两个版本。一个版本叫protobuf-net,官方站点:http://code.google.com/p/protobuf-net/ 写法上比较符合c#一向的写法。另外一个版本叫protobuf-csharp-sport ,
官方站点:http://code.google.com/p/protobuf-csharp-port/ 写法上跟java上的使用极其类似,比较遵循Google 的原生态写法,因此作跨平台仍是选择第二版本吧。由于你会发现几乎和java的写法没啥两样,本篇也是使用这个版本。
进入该站点,下载你要的win版。 编译步骤以下:
- 将刚才你的proto文件放在你解压出来的目录与protoc.exe 、ProtoGen.exe、ProtoGen.exe.config放于一块儿。其余文件能够删除或者 备份。
- 仍是打开命令行,定位于对应的目录里面,你放proto文件的目录里面。
- 输入:protoc --descriptor_set_out=msg.protobin --include_imports msg.proto
- msg.protobin是要生成的prtobobin文件,能够使用这个bin文件生成cs文件
- 再输入protogen msg.protobin 使用该bin文件生成cs文件,这样你就能够获得该 msg.cs 的CSharp版文件了,同时在VS里面使用要引入Google.ProtocolBuffers.dll。为了方便你能够将其作成一个批处理文件代码以下:
echo on
protoc
--
descriptor_set_out
=
msg.protobin
--
include_imports msg.proto
protogen msg.protobin
将其另存为.bat文件便可
使用protobuf编译后的文件来进行socket链接
android 与PC
android 作为客户端向PC的Java服务端发送数据,服务端获得数据进行解析,并打印出来 。
客户端代码:
package net.testSocket;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import socket.exception.SmsClientException;
import socket.exception.SmsObjException;
import msginfo.Msg.CMsg;
import msginfo.Msg.CMsgHead;
import msginfo.Msg.CMsgReg;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.protobuf.InvalidProtocolBufferException;
//
客户端的实现
public
class
TestSocket extends Activity {
private
TextView text1;
private
Button but1;
Socket socket
=
null
;
public
void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//
Thread desktopServerThread=new Thread(new AndroidServer());
//
desktopServerThread.start();
setContentView(R.layout.main);
text1
=
(TextView) findViewById(R.id.text1);
but1
=
(Button) findViewById(R.id.but1);
but1.setOnClickListener(
new
Button.OnClickListener() {
@Override
public
void
onClick(View v) {
try
{
socket
=
new
Socket(
"
192.168.1.116
"
,
12345
);
//
获得发送消息的对象
CMsgHead head
=
CMsgHead.newBuilder().setMsglen(
5
)
.setMsgtype(
1
).setMsgseq(
3
).setTermversion(
41
)
.setMsgres(
5
).setTermid(
"
11111111
"
).build();
//
body
CMsgReg body
=
CMsgReg.newBuilder().setArea(
22
)
.setRegion(
33
).setShop(
44
).build();
//
Msg
CMsg msg
=
CMsg.newBuilder()
.setMsghead(head.toByteString().toStringUtf8())
.setMsgbody(body.toByteString().toStringUtf8())
.build();
//
向服务器发送信息
msg.writeTo(socket.getOutputStream());
//
接受服务器的信息
InputStream input
=
socket.getInputStream();
byte
[] by
=
recvMsg(input);
setText(CMsg.parseFrom(by));
input.close();
socket.close();
}
catch
(UnknownHostException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(Exception e) {
System.
out
.println(e.toString());
}
//
};
//
}.start();
}
});
}
/*
*
* 接收server的信息
*
* @return
* @throws SmsClientException
* @author fisher
*/
public
byte
[] recvMsg(InputStream inpustream) throws SmsObjException {
try
{
byte
len[]
=
new
byte
[
1024
];
int
count
=
inpustream.read(len);
byte
[] temp
=
new
byte
[count];
for
(
int
i
=
0
; i
<
count; i
++
) {
temp[i]
=
len[i];
}
return
temp;
}
catch
(Exception localException) {
throw
new
SmsObjException(
"
SmapObj.recvMsg() occur exception!
"
+
localException.toString());
}
}
/*
*
* 获得返回值添加到文本里面
*
* @param g
* @throws InvalidProtocolBufferException
*/
public
void
setText(CMsg g) throws InvalidProtocolBufferException {
CMsgHead h
=
CMsgHead.parseFrom(g.getMsghead().getBytes());
StringBuffer sb
=
new
StringBuffer();
if
(h.hasMsglen())
sb.append(
"
==len===
"
+
h.getMsglen()
+
"
\n
"
);
if
(h.hasMsgres())
sb.append(
"
==res===
"
+
h.getMsgres()
+
"
\n
"
);
if
(h.hasMsgseq())
sb.append(
"
==seq===
"
+
h.getMsgseq()
+
"
\n
"
);
if
(h.hasMsgtype())
sb.append(
"
==type===
"
+
h.getMsgtype()
+
"
\n
"
);
if
(h.hasTermid())
sb.append(
"
==Termid===
"
+
h.getTermid()
+
"
\n
"
);
if
(h.hasTermversion())
sb.append(
"
==Termversion===
"
+
h.getTermversion()
+
"
\n
"
);
CMsgReg bo
=
CMsgReg.parseFrom(g.getMsgbody().getBytes());
if
(bo.hasArea())
sb.append(
"
==area==
"
+
bo.getArea()
+
"
\n
"
);
if
(bo.hasRegion())
sb.append(
"
==Region==
"
+
bo.getRegion()
+
"
\n
"
);
if
(bo.hasShop())
sb.append(
"
==shop==
"
+
bo.getShop()
+
"
\n
"
);
if
(bo.hasRet())
sb.append(
"
==Ret==
"
+
bo.getRet()
+
"
\n
"
);
if
(bo.hasTermid())
sb.append(
"
==Termid==
"
+
bo.getTermid()
+
"
\n
"
);
text1.setText(sb.toString());
}
}
服务端代码:
package server;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import msginfo.Msg.CMsg;
import msginfo.Msg.CMsgHead;
import msginfo.Msg.CMsgReg;
public
class
AndroidServer implements Runnable {
public
void
run() {
try
{
System.
out
.println(
"
beign:
"
);
ServerSocket serverSocket
=
new
ServerSocket(
12345
);
while
(
true
) {
System.
out
.println(
"
等待接收用户链接:
"
);
//
接受客户端请求
Socket client
=
serverSocket.accept();
DataOutputStream dataOutputStream;
DataInputStream dataInputStream;
try
{
InputStream inputstream
=
client.getInputStream();
dataOutputStream
=
new
DataOutputStream(
client.getOutputStream());
byte
len[]
=
new
byte
[
1024
];
int
count
=
inputstream.read(len);
byte
[] temp
=
new
byte
[count];
for
(
int
i
=
0
; i
<
count; i
++
) {
temp[i]
=
len[i];
}
//
协议正文
//
CMsg msg
=
CMsg.parseFrom(temp);
//
//
CMsgHead head
=
CMsgHead.parseFrom(msg.getMsghead()
.getBytes());
System.
out
.println(
"
==len===
"
+
head.getMsglen());
System.
out
.println(
"
==res===
"
+
head.getMsgres());
System.
out
.println(
"
==seq===
"
+
head.getMsgseq());
System.
out
.println(
"
==type===
"
+
head.getMsgtype());
System.
out
.println(
"
==Termid===
"
+
head.getTermid());
System.
out
.println(
"
==Termversion===
"
+
head.getTermversion());
CMsgReg body
=
CMsgReg.parseFrom(msg.getMsgbody()
.getBytes());
System.
out
.println(
"
==area==
"
+
body.getArea());
System.
out
.println(
"
==Region==
"
+
body.getRegion());
System.
out
.println(
"
==shop==
"
+
body.getShop());
sendProtoBufBack(dataOutputStream);
inputstream.close();
}
catch
(Exception ex) {
System.
out
.println(ex.getMessage());
ex.printStackTrace();
}
finally
{
client.close();
System.
out
.println(
"
close
"
);
}
}
}
catch
(IOException e) {
System.
out
.println(e.getMessage());
}
}
public
static
void
main(String[] args) {
Thread desktopServerThread
=
new
Thread(
new
AndroidServer());
desktopServerThread.start();
}
private
byte
[] getProtoBufBack() {
//
head
CMsgHead head
=
CMsgHead.newBuilder().setMsglen(
5
)
.setMsgtype(
1
).setMsgseq(
3
).setTermversion(
41
)
.setMsgres(
5
).setTermid(
"
11111111
"
).build();
//
body
CMsgReg body
=
CMsgReg.newBuilder().setArea(
22
)
.setRegion(
33
).setShop(
44
).build();
//
Msg
CMsg msg
=
CMsg.newBuilder()
.setMsghead(head.toByteString().toStringUtf8())
.setMsgbody(body.toByteString().toStringUtf8())
.build();
return
msg.toByteArray();
}
private
void
sendProtoBufBack(DataOutputStream dataOutputStream) {
byte
[] backBytes
=
getProtoBufBack();
//
协议头部
try
{
dataOutputStream.write(backBytes,
0
, backBytes.length);
dataOutputStream.flush();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
最后获得的效果:
客户端:
服务端:
protobuf .net版的实现代码以下:
using
System;
using
System.IO;
using
System.Net;
using
System.Net.Sockets;
using
System.Threading;
using
Google.ProtocolBuffers;
using
msginfo;
using
System.Text;
using
System.Collections;
using
System.Collections.Generic;
namespace
protobuf_csharp_sport
{
class
Program
{
private
static
ManualResetEvent allDone
=
new
ManualResetEvent(
false
);
static
void
Main(
string
[] args)
{
beginProtocbuf();
}
private
static
void
beginProtocbuf()
{
//
启动服务端
TcpListener server
=
new
TcpListener(IPAddress.Parse(
"
127.0.0.1
"
),
12345
);
server.Start();
server.BeginAcceptTcpClient(clientConnected, server);
Console.WriteLine(
"
SERVER : 等待数据 ---
"
);
//
启动客户端
ThreadPool.QueueUserWorkItem(runClient);
allDone.WaitOne();
Console.WriteLine(
"
SERVER : 退出 ---
"
);
//
server.Stop();
}
//
服务端处理
private
static
void
clientConnected(IAsyncResult result)
{
try
{
TcpListener server
=
(TcpListener)result.AsyncState;
using
(TcpClient client
=
server.EndAcceptTcpClient(result))
{
using
(NetworkStream stream
=
client.GetStream())
{
//
获取
Console.WriteLine(
"
SERVER : 客户端已链接,数据读取中 ---
"
);
byte
[] myRequestBuffer
=
new
byte
[
1024
];
int
myRequestLength
=
0
;
do
{
myRequestLength
=
stream.Read(myRequestBuffer,
0
, myRequestBuffer.Length);
}
while
(stream.DataAvailable);
CMsg msg
=
CMsg.ParseFrom(myRequestBuffer.RemoveEmptyByte(myRequestLength));
CMsgHead head
=
CMsgHead.ParseFrom(Encoding.ASCII.GetBytes(msg.Msghead));
CMsgReg body
=
CMsgReg.ParseFrom(Encoding.ASCII.GetBytes(msg.Msgbody));
IDictionary
<
Google.ProtocolBuffers.Descriptors.FieldDescriptor,
object
>
d
=
head.AllFields;
foreach
(var item
in
d)
{
Console.WriteLine(item.Value.ToString());
}
d
=
body.AllFields;
Console.WriteLine(
"
===========================================
"
);
foreach
(var item
in
d)
{
Console.WriteLine(item.Value.ToString());
}
Console.WriteLine(
"
SERVER : 响应成功 ---
"
);
Console.WriteLine(
"
SERVER: 关闭链接 ---
"
);
stream.Close();
}
client.Close();
}
}
finally
{
allDone.Set();
}
}
//
客户端请求
private
static
void
runClient(
object
state)
{
try
{
CMsgHead head
=
CMsgHead.CreateBuilder()
.SetMsglen(
5
)
.SetMsgtype(
1
)
.SetMsgseq(
3
)
.SetTermversion(
4
)
.SetMsgres(
5
)
.SetTermid(
"
11111111
"
)
.Build();
CMsgReg body
=
CMsgReg.CreateBuilder().
SetArea(
22
)
.SetRegion(
33
)
.SetShop(
44
)
.Build();
CMsg msg
=
CMsg.CreateBuilder()
.SetMsghead(head.ToByteString().ToStringUtf8())
.SetMsgbody(body.ToByteString().ToStringUtf8())
.Build();
Console.WriteLine(
"
CLIENT : 对象构造完毕 ...
"
);
using
(TcpClient client
=
new
TcpClient())
{
//
client.Connect(new IPEndPoint(IPAddress.Parse("192.168.1.116"), 12345));
client.Connect(
new
IPEndPoint(IPAddress.Parse(
"
127.0.0.1
"
),
12345
));
Console.WriteLine(
"
CLIENT : socket 链接成功 ...
"
);
using
(NetworkStream stream
=
client.GetStream())
{
//
发送
Console.WriteLine(
"
CLIENT : 发送数据 ...
"
);
msg.WriteTo(stream);
//
关闭
stream.Close();
}
client.Close();
Console.WriteLine(
"
CLIENT : 关闭 ...
"
);
}
}
catch
(Exception error)
{
Console.WriteLine(
"
CLIENT ERROR : {0}
"
, error.ToString());
}
}
}
//
end class
public
static
class
ExtensionClass {
public
static
}
运行的效果:
这样就OK了,以后就能够把java 服务端的IP或端口改为C# IP和服务端的商品同样,或者反过来也是能够的。c++版本通过测试也是能够的。简直是一个爽字。