android與PC,C#與Java 利用protobuf 進行無障礙通訊【Socket

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的消息文件,內容如下:

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;

}

 

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++版本經過測試也是可以的。簡直是一個爽字。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章