欢迎分享,转载请注明来源:内存溢出
一、Protobuf的使用首先来简单介绍一下Protobuf的使用,这里以windows下java开发为例,几个步骤:编写Socket; import comMsgCMsg; import comMsgCMsgHead; import comMsgCMsgReg; public class ProtoClient { public static void main(String[] args) { ProtoClient pc=new ProtoClient(); Systemoutprintln("beign:"); pcrunget(); } public void runget() { Socket socket = null; try { //socket = new Socket("localhost", 12345); socket = new Socket("19216885152", 12345); // head CMsgHead head = CMsgHeadnewBuilder()setMsglen(5)setMsgtype(1) setMsgseq(3)setTermversion(41)setMsgres(5) setTermid("Client:head")build(); // body CMsgReg body = CMsgRegnewBuilder()setArea(11)setRegion(22) setShop(33)setRet(44)setTermid("Clent:body")build(); // Msg CMsg msg = CMsgnewBuilder() setMsghead(headtoByteString()toStringUtf8()) setMsgbody(bodytoByteString()toStringUtf8())build(); // 向服务器发送信息 Systemoutprintln("sendMsg"); msgwriteTo(socketgetOutputStream()); // 接受服务器的信息 InputStream input = socketgetInputStream(); Systemoutprintln("recvMsg:"); byte[] by = recvMsg(input); printMsg(CMsgparseFrom(by)); inputclose(); socketclose(); } catch (Exception e) { Systemoutprintln(etoString()); } } public void printMsg(CMsg g) { try { CMsgHead h = CMsgHeadparseFrom(ggetMsghead()getBytes()); StringBuffer sb = new StringBuffer(); if (hhasMsglen()) sbappend("==msglen===" + hgetMsglen() + "\n"); if (hhasMsgres()) sbappend("==msgres===" + hgetMsgres() + "\n"); if (hhasMsgseq()) sbappend("==msgseq===" + hgetMsgseq() + "\n"); if (hhasMsgtype()) sbappend("==msgtype===" + hgetMsgtype() + "\n"); if (hhasTermid()) sbappend("==termid===" + hgetTermid() + "\n"); if (hhasTermversion()) sbappend("==termversion===" + hgetTermversion() + "\n"); CMsgReg bo = CMsgRegparseFrom(ggetMsgbody()getBytes()); if (bohasArea()) sbappend("==area==" + bogetArea() + "\n"); if (bohasRegion()) sbappend("==region==" + bogetRegion() + "\n"); if (bohasShop()) sbappend("==shop==" + bogetShop() + "\n"); if (bohasRet()) sbappend("==ret==" + bogetRet() + "\n"); if (bohasTermid()) sbappend("==termid==" + bogetTermid() + "\n"); Systemoutprintln(sbtoString()); } catch (Exception e) { eprintStackTrace(); } } public byte[] recvMsg(InputStream inpustream) { byte[] temp = null; try { byte len[] = new byte[1024]; int count = inpustreamread(len); temp = new byte[count]; for (int i = 0; i < count; i++) { temp[i] = len[i]; } return temp; } catch (Exception e) { Systemoutprintln(etoString()); return temp; } } }netty v394 websocket连接建立前,客户端需要与服务器进行握手(>
赞
(0)
打赏
微信扫一扫
支付宝扫一扫
云服务器跟普通服务器有啥区别吗
上一篇
2023-05-09
怎样打开1080端口代理上QQ
下一篇
2023-05-09
评论列表(0条)