protobuf 是什么?
Protocol buffers是一种编码方法构造的一种有效而可扩展的格式的数据。
谷歌使用其内部几乎RPC协议和文件格式的所有协议缓冲区。
参考文档
http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview.html
API的 参考文档
protobuf 适用的语言
正宗(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,方面读者做参考。
使用protobuf协议
定义protobuf协议
定义protobuf协议必须创建一个以.proto为后缀的文件,以本篇为例,本篇创建了一个叫msg.proto的消息文件,内容如下:
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
;
}
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
- cmd 打开命令工具
- 目录下,先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 使用的编译方法:
官方站点:http://code.google.com/p/protobuf-net/
官方站点:http://code.google.com/p/protobuf-csharp-port/ 写法上跟java上的使用极其相似,比较遵循Google 的原生态写法,所以做跨平台还是选择第二版本吧。因为你会发现几乎和java的写法没啥两样,本篇也是使用这个版本。
编译步骤如下:
- 、ProtoGen.exe、ProtoGen.exe.config放于一起。其他文件可以删除或者 备份。
- 还是打开命令行,定位于对应的目录里面,你放proto文件的目录里面。
- msg.protobin是要生成的prtobobin文件,可以使用这个bin文件生成cs文件
- 使用该bin文件生成cs文件,这样你就可以得到该 msg.cs 的CSharp版文件了,同时在VS里面使用要引入Google.ProtocolBuffers.dll。为了方便你可以将其做成一个批处理文件代码如下:
on
protoc -- descriptor_set_out = msg.protobin -- include_imports msg.proto
protogen msg.protobin
将其另存为.bat文件即可
android 与PC
。
客户端代码:
服务端代码:
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) {
//
edit1.setText("");
//
Log.e("dddd", "sent id");
//
new Thread() {
//
public void run() {
try
{
//
socket=new Socket("192.168.1.102",54321);
//
socket = new Socket("192.168.1.110", 10527);
socket
=
new
Socket(
"
192.168.1.116
"
,
12345
);
//
得到发送消息的对象
//
SmsObj smsobj = new SmsObj(socket);
//
设置消息头和消息体并存入消息里面
//
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();
//
PrintWriter out = new PrintWriter(new BufferedWriter(
//
new OutputStreamWriter(socket.getOutputStream())),
//
true);
//
out.println(m.toString());
//
out.println(m.toByteString().toStringUtf8());
//
向服务器发送信息
msg.writeTo(socket.getOutputStream());
//
byte[] b = msg.toByteArray();
//
smsobj.sendMsg(b);
//
System.out.println("====msg==="
//
+ m.toByteString().toStringUtf8());
//
byte[] backBytes = smsobj.recvMsg();
//
//
接受服务器的信息
InputStream input
=
socket.getInputStream();
//
DataInputStream dataInput=new DataInputStream();
//
byte[] by = smsobj.recvMsg(input);
byte
[] by
=
recvMsg(input);
setText(CMsg.parseFrom(by));
//
BufferedReader br = new BufferedReader(
//
new InputStreamReader(socket.getInputStream()));
//
String mstr = br.readLine();
//
if (!str .equals("")) {
//
text1.setText(str);
//
} else {
//
text1.setText("数据错误");
//
}
//
out.close();
//
br.close();
input.close();
//
smsobj.close();
socket.close();
}
catch
(UnknownHostException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
catch
(Exception e) {
System.
out
.println(e.toString());
}
//
};
//
}.start();
}
});
}
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());
}
}
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
{
//
接受客户端信息
//
BufferedReader in = new BufferedReader(
//
new InputStreamReader(client.getInputStream()));
//
String str = in.readLine();
//
System.out.println("read length: " + str.length());
//
System.out.println("read: " + str);
//
InputStream inputstream = client.getInputStream();
//
byte[] buffer = new byte[1024 * 4];
//
int temp = 0;
//
while ((temp = inputstream.read(buffer)) != -1) {
//
str = new String(buffer, 0, temp);
//
System.out.println("===str===" + str);
//
File file = new File("user\\log\\login.log");
//
appendLog(file, str);
InputStream inputstream
=
client.getInputStream();
dataOutputStream
=
new
DataOutputStream(
client.getOutputStream());
//
dataInputStream = new DataInputStream(inputstream);
//
byte[] d = new BufferedReader(new InputStreamReader(
//
dataInputStream)).readLine().getBytes();
//
byte[] bufHeader = new byte[4];
//
dataInputStream.readFully(bufHeader);
//
int len = BytesUtil.Bytes4ToInt(bufHeader);
//
System.out.println(d.length);
//
System.out.println(dataInputStream.readLine().toString());
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];
}
//
协议正文
//
byte[] sendByte = new byte[30];
//
//
dataInputStream.readFully(sendByte);
//
for (byte b : sendByte) {
//
System.out.println(""+b);
//
}
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());
//
PrintWriter out = new PrintWriter(new BufferedWriter(
//
new OutputStreamWriter(client.getOutputStream())),
//
true);
//
out.println("return " +msg.toString());
//
in.close();
//
out.close();
sendProtoBufBack(dataOutputStream);
inputstream.close();
//
dataInputStream.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();
//
协议头部
//
Integer len2 = backBytes.length;
//
前四个字节,标示协议正文长度
//
byte[] cmdHead2 = BytesUtil.IntToBytes4(len2);
try
{
//
dataOutputStream.write(cmdHead2, 0, cmdHead2.length);
dataOutputStream.write(backBytes,
0
, backBytes.length);
dataOutputStream.flush();
}
catch
(IOException e) {
e.printStackTrace();
}
}
}