日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

go语言socket通信初试

發布時間:2025/6/15 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 go语言socket通信初试 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

聽說go的在系統性能方面有很大的優勢,最近對go語言產生了極大的興趣,相對現有的項目用go改造,原有的項目用的ace框架編寫的通信的框架,在目前的移動的通信網中忙時有的時候處理不過來,于是先研究試圖測試一下socket。由于對go剛剛入門,有些不正確的地方還請高人指點。

由于我們系統通常是不同語言之間通信(之前系統是客戶端和服務端都用c++),這里客戶端采用java+mina編寫,服務端采用go編寫,最初設計,像借用go語言中的gob進行編解碼,但是經過測試后發現行不通,經過和網友以及一些高人的指點,gob其實針對go語言之間的編解碼的,跨語言還真不靈光。有同事建議我用protocolbuffer這個,我一看這個又是定義類似idl文件(之前做了幾年的corba技術,對這樣的東西有點抵觸了,因為有的時候項目合作方已經定下的方案或者已經完成的項目很難配合你用一種新的技術或者新協議重新修改),沒有辦法我只能采用硬編碼的方式實現了,下一步我會采用protocolbuffer技術實現跨語言之間的通信,如果您有剛好的方式,希望能交流。下面我把我的部分代碼貼一下,僅供參考,如果需要全部可以測試程序可以留下方式。

客戶端主要代碼:

主入口類:

public class Client {

?? ?/**
?? ? * @param args
?? ? */
?? ?public static void main(String[] args) {
?? ??? ?// TODO Auto-generated method stub

?? ??? ?MinaClient client = new MinaClient(); ?
?? ??? ?if (client.connect()) { ?
?? ??? ??? ?System.out.println("連接服務器成功!");
?? ??? ??? ?//client.send("連接服務器成功!"); ?
?? ??? ??? ?//Scanner scanner = new Scanner(System.in);
?? ??? ??? ?boolean flag =false;
?? ??? ??? ?int i = 0;
?? ??? ??? ?while (!flag) { ?
?? ??? ??? ??? ?i++;
?? ??? ???????? //client.send("hello world "+i);
?? ??? ??? ??? ?Ss7LspMsg msg = new Ss7LspMsg();
?? ??? ??? ??? ?msg.setSeq(231115);
?? ??? ??? ??? ?msg.setProtocoltype(1);
?? ??? ??? ??? ?msg.setTime(System.currentTimeMillis());
?? ??? ??? ??? ?msg.setLsp(123);
?? ??? ??? ??? ?msg.setLen(20);
?? ??? ??? ??? ?byte[] bytes = new byte[20];
?? ??? ??? ??? ?for(int j=0;j<20;j++){
?? ??? ??? ??? ??? ?bytes[i] = (byte) j;
?? ??? ??? ??? ?}
?? ??? ??? ??? ?msg.setBytes(bytes);
?? ??? ??? ??? ?//client.send("helloworld"+i);
?? ??? ??? ??? ?client.send(msg);
?? ??? ???????? if(i==1)
?? ??? ??????? ??? ?flag = true;
?? ??? ???????? /*try {
?? ??? ??? ??? ??? ?Thread.sleep(1000);
?? ??? ??? ??? ?} catch (InterruptedException e) {
?? ??? ??? ??? ??? ?// TODO Auto-generated catch block
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}*/
?? ??? ??? ?} ?
?? ??? ?} ?
?? ??? ?System.out.println("OVER!");
?? ??? ?client.close();
?? ?}

}

MinaClient類:

public class MinaClient {
?? ?

?? ?private SocketConnector connector; ?
?? ?private ConnectFuture future; ?
?? ?private IoSession session; ?


?? ?public boolean connect() { ?
?? ??? ?// 創建一個socket連接 ?
?? ??? ?connector = new NioSocketConnector(); ?
?? ??? ?// 設置鏈接超時時間 ?
?? ??? ?connector.setConnectTimeoutMillis(3000); ?
?? ??? ?// 獲取過濾器鏈 ?
?? ??? ?DefaultIoFilterChainBuilder filterChain = connector.getFilterChain(); ?
?? ??? ?// 添加編碼過濾器 處理亂碼、編碼問題 ?
?? ??? ?filterChain.addLast("codec", new ProtocolCodecFilter(new CharsetCodecFactory()));?
?? ?
?? ??? ?// 消息核心處理器 ?
?? ??? ?connector.setHandler(new ClientMessageHandlerAdapter()); ?
?? ?? ?
?? ??? ?// 連接服務器,知道端口、地址 ?
?? ??? ?future = connector.connect(new InetSocketAddress("127.0.0.1",22345)); ?
?? ???????? // 等待連接創建完成 ?
?? ??? ?future.awaitUninterruptibly(); ?
?? ??? ?// 獲取當前session ?
?? ??? ?session = future.getSession(); ?
?? ??? ?return true; ?
?? ?} ?

?? ?public void setAttribute(Object key, Object value) { ?
?? ??? ?session.setAttribute(key, value); ?
?? ?} ?
?? ?
?? ?public void send(String message) { ?
?? ??? ?session.write(message); ?
?? ?} ?
?? ?
?? ?public void send(Ss7LspMsg message) { ?
?? ??? ?session.write(message); ?
?? ?}
?? ?
?? ?public boolean close() { ?
?? ??? ?CloseFuture future = session.getCloseFuture(); ?
?? ??? ?future.awaitUninterruptibly(1000); ?
?? ??? ?connector.dispose(); ?
?? ??? ?return true; ?
?? ?} ?
?? ?
?? ?public SocketConnector getConnector() { ?
?? ??? ?return connector; ?
?? ?} ?
?? ?
?? ?public IoSession getSession() { ?
?? ??? ?return session; ?
?? ?}?
消息基礎類(消息頭)

public abstract class MsgHeader implements Serializable{
?? ?/**
?? ? *
?? ? */
?? ?private static final long serialVersionUID = 1L;
?? ?//消息長度,包括頭長度
?? ?//頭長:8
?? ?int length = 20;
?? ?//協議類型
?? ?int protocoltype;
?? ?//序列號
?? ?int? seq;
?? ?//時間戳
?? ?long time;


?? ?
?? ?public MsgHeader() {
?? ??? ?super();
?? ??? ?// TODO Auto-generated constructor stub
?? ?}

?? ?public MsgHeader(int length, int protocoltype, int seq) {
?? ??? ?super();
?? ??? ?this.length = length;
?? ??? ?this.protocoltype = protocoltype;
?? ??? ?this.seq = seq;
?? ??? ?this.time = System.currentTimeMillis();
?? ?}

?? ?public void encodeHeader(IoBuffer buf) {
??????? // The total Length will be set later.
?? ??? ?buf.putInt(seq);
?? ??? ?buf.putInt(protocoltype);
??????? buf.putInt(length);
??????? System.out.println("len is "+length);
??????? buf.putLong(time);
??? }

??? public void decodeHeader(IoBuffer buf) {
?? ??? ?seq = buf.getInt();
??????? protocoltype = buf.getInt();
?? ??? ?length = buf.getInt();
?? ??? ?time = buf.getLong();
??? }

??? public abstract boolean encodeBody(IoBuffer bt);

??? public abstract boolean decodeBody(byte[] body);
?? ?
?? ?
?? ?public int getLength() {
?? ??? ?return length;
?? ?}
?? ?public void setLength(int length) {
?? ??? ?this.length += length;
?? ?}
?? ?public int getProtocoltype() {
?? ??? ?return protocoltype;
?? ?}
?? ?public void setProtocoltype(int protocoltype) {
?? ??? ?this.protocoltype = protocoltype;
?? ?}
?? ?public int getSeq() {
?? ??? ?return seq;
?? ?}
?? ?public void setSeq(int seq) {
?? ??? ?this.seq = seq;
?? ?}
?? ?



?? ?public byte[] strToBytes(int len,String str){
?? ??? ?byte[] bytes = new byte[len];
?? ??? ?for(int i=0;i<len;i++){
?? ??? ??? ?bytes[i] = (byte) 0xff;
?? ??? ?}
?? ??? ?String tmpstr = null;
?? ??? ?int tmplen = 0;
?? ??? ?if(str.trim().length()>len){
?? ??? ??? ?tmpstr = str.substring(0, 15);
?? ??? ??? ?tmplen = 16;
?? ??? ?}else{
?? ??? ??? ?tmpstr = str;
?? ??? ??? ?tmplen = str.length();
?? ??? ?}
?? ??? ?byte[] tmpbytes = tmpstr.getBytes();
?? ??? ?for(int i=0;i<tmplen;i++){
?? ??? ??? ?bytes[i] = tmpbytes[i];
?? ??? ?}
?? ??? ?return bytes;
?? ?}
?? ?
?? ?public? String decOctetString(byte[] bt) {
??????? int b = 0;
??????? int e = 0;

??????? // find the begin non 0 position;
??????? for (int i = 0; i < bt.length; i++) {
??????????? if (bt[i] != 0) {
??????????????? b = i;
??????????????? break;
??????????? }
??????? }

??????? // find the end non 0 position;
??????? for (int i = bt.length - 1; i > 0; i--) {
??????????? if (bt[i] != 0) {
??????????????? e = i;
??????????????? break;
??????????? }
??????? }

??????? return new String(bt, b, e - b + 1);
?? ?}

?? ?public long getTime() {
?? ??? ?return time;
?? ?}

?? ?public void setTime(long time) {
?? ??? ?this.time = time;
?? ?}

?? ?
?


?? ?/**
??? * 字符串ip轉換為long
??? * @param 字符串ip
??? * @return
??? */
?? public static long getStringIpToLong(String ip) {
?????? String[] ips = ip.trim().split("[.]");
?????? long num =? 16777216L*Long.parseLong(ips[0]) + 65536L*Long.parseLong(ips[1]) + 256*Long.parseLong(ips[2]) + Long.parseLong(ips[3]);
?????? return num;
?? }
?? ?
?? /**
??? * 長整型ip轉換為string
??? * @param long型ip
??? * @return
??? */
?? public static String getLongIpToString(long ipLong) { ?
???? ?
?????? long mask[] = {0x000000FF,0x0000FF00,0x00FF0000,0xFF000000};
?????? long num = 0;
?????? StringBuffer ipInfo = new StringBuffer();
?????? for(int i=0;i<4;i++){
?????????? num = (ipLong & mask[i])>>(i*8);
?????????? if(i>0) ipInfo.insert(0,".");
?????????? ipInfo.insert(0,Long.toString(num,10));
?????? }
?????? return ipInfo.toString();
?? }

}

抽象消息類(請求類、反饋類)

public abstract class BaseReq extends MsgHeader {

?? ?/**
?? ? *
?? ? */
?? ?private static final long serialVersionUID = 1L;

?? ?
}

public abstract class BaseRsp extends MsgHeader {

?? ?/**
?? ? *
?? ? */
?? ?private static final long serialVersionUID = 1L;
?? ?protected int result;
?? ?protected int reason;
?? ?public int getResult() {
?? ??? ?return result;
?? ?}
?? ?public void setResult(int result) {
?? ??? ?this.result = result;
?? ?}
?? ?public int getReason() {
?? ??? ?return reason;
?? ?}
?? ?public void setReason(int reason) {
?? ??? ?this.reason = reason;
?? ?}

?? ?
}

測試消息類

public class Ss7LspMsg extends BaseReq {
?? ?private int lsp;
?? ?private int len;
?? ?private byte[] bytes;
?? ?
?? ?public int getLsp() {
?? ??? ?return lsp;
?? ?}

?? ?public void setLsp(int lsp) {
?? ??? ?this.lsp = lsp;
?? ?}

?? ?public int getLen() {
?? ??? ?return len;
?? ?}

?? ?public void setLen(int len) {
?? ??? ?this.len = len;
?? ?}

?? ?public byte[] getBytes() {
?? ??? ?return bytes;
?? ?}

?? ?public void setBytes(byte[] bytes) {
?? ??? ?this.bytes = bytes;
?? ?}

?? ?@Override
?? ?public boolean encodeBody(IoBuffer bt) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?this.setLength(len+8);
?? ??? ?encodeHeader(bt);
?? ??? ?bt.putInt(len);
?? ??? ?bt.putInt(lsp);
?? ??? ?bt.put(bytes);
?? ??? ?return true;
?? ?}

?? ?@Override
?? ?public boolean decodeBody(byte[] body) {
?? ??? ?// TODO Auto-generated method stub
?? ??? ?return false;
?? ?}

}


服務端代碼:

負責通信的go文件

package main

import ( ????"fmt" ????//"github.com/bbangert/toml" ????"bytes" ????"encoding/binary" ????"encoding/gob" ????"io" ????"net" ) //常量定義 const ( ????VERSION = "0.1.0" ????TCP = "tcp" ????UDP = "udp" ????RECV_BUF_LEN = 1024 ) type IpTransType struct { ????Type string //網絡類型tcp/udp ????Addr string //ip地址 默認 127.0.0.1 ????Port int32 } func InitServer(transType IpTransType) (err error) { ????if transType.Addr == "" { ????????err = fmt.Errorf("transType.Addr is nil,please check the configuration file") ????????return ????} ????if transType.Port < 1 || transType.Port > 65535 { ????????err = fmt.Errorf("transType.Port must be in (1 ~ 65535") ????????return ????} ????if !(transType.Type == TCP || transType.Type == UDP) { ????????err = fmt.Errorf("transType.Type only be 'tcp' or 'udp' ") ????????return ????} ????listener, err := net.Listen(transType.Type, "127.0.0.1:22345") ????defer listener.Close() ????for { ????????conn, err := listener.Accept() ????????if err != nil { ????????????continue ????????} ????????fmt.Println("conn is coming") ????????go Receiver(conn) ????} ????return } type LspMsg struct { ????seq int32 ????protocol int32 ????length int32 ????times int64 ????lens int32 ????lsp int32 ????bytes [20]byte ????//bytes := make([]byte,20) ????//bytes *[]byte } type LspMsgBig struct { ????Seq int32 ????Protocol int32 ????Length int32 ????Times int64 ????Lens int32 ????Lsp int32 ????Bytes [20]byte ????//bytes := make([]byte,20) ????//bytes *[]byte } func Decode(data []byte, to interface{}) error { ????buf := bytes.NewBuffer(data) ????dec := gob.NewDecoder(buf) ????return dec.Decode(to) } func BytesToInt32(bytes []byte) int32 { ????return int32(binary.BigEndian.Uint32(bytes)) } func BytesToInt8(bytes []byte) int8 { ????return int8(bytes[0]) } func BytesToInt16(bytes []byte) int16 { ????return int16(binary.BigEndian.Uint16(bytes)) } func BytesToInt64(bytes []byte) int64 { ????return int64(binary.BigEndian.Uint64(bytes)) } func Receiver(conn net.Conn) (err error) { ????buf := make([]byte, RECV_BUF_LEN) ????//buf bytes.Buffer ????defer conn.Close() ????for { ????????n, err1 := conn.Read(buf) ????????switch err1 { ????????case nil: ????????????//n, _ := conn.Write(buf[0:n]) ????????????var out LspMsg ????????????//Decode(b, &out) ????????????var outout LspMsgBig ????????????if err := Decode(buf, &outout); err != nil { ????????????????fmt.Println("decode fail: " + err.Error()) ????????????} ????????????fmt.Println("outout is ", outout) ????????????fmt.Println("Byte2Int32 is ", BytesToInt32(buf[0:4])) ????????????fmt.Println("length is ", buf[0:n]) ????????????fmt.Println("length is ", buf[0:4]) ????????????fmt.Println("length is ", BytesToInt8(buf[1:4])) ????????????out.seq = BytesToInt32(buf[0:4]) ????????????out.protocol = BytesToInt32(buf[4:8]) ????????????out.length = BytesToInt32(buf[8:12]) ????????????out.times = BytesToInt64(buf[12:20]) ????????????out.lens = BytesToInt32(buf[20:24]) ????????????out.lsp = BytesToInt32(buf[24:28]) ????????????bytes := out.bytes[0:20] ????????????copy(bytes, buf[28:n]) ????????????//out.bytes = &(buf[28:n]) ????????????fmt.Println(out.bytes) ????????????/* ????????????????for j := 0; j < 20; j++ { ????????????????????out.bytes[j] = buf[j+28] ????????????????} ????????????*/ ????????????fmt.Println("length is ", out) ????????case io.EOF: //當對方斷開連接時觸發該方法 ????????????fmt.Printf("Warning: End of data: %s \n", err1) ????????????err = err1 ????????????return ????????default: //當對方斷開連接時觸發該方法 ????????????fmt.Printf("Error: Reading data: %s \n", err1) ????????????err = err1 ????????????return ????????} ????} ????return } 程序主入口:

package main

import ( ????"fmt" ????//"net" ????"bytes" ????"encoding/gob" ????//"C" ) type P struct { ????X, Y, Z int ????Name string } type Q struct { ????X, Y *int32 ????Name string } func main() { ????//C.puts(C.CString("Hello, world\n")) ????var network bytes.Buffer // Stand-in for a network connection ????enc := gob.NewEncoder(&network) // Will write to network. ????dec := gob.NewDecoder(&network) // Will read from network. ????err := enc.Encode(P{3, 4, 5, "Pythagoras"}) ????if err != nil { ????????fmt.Println("encode error:", err) ????} ????var q Q ????err = dec.Decode(&q) ????fmt.Println("ENC IS ", enc) ????fmt.Println("dec IS ", dec) ????//fmt.Println("network IS ", network.String()) ????if err != nil { ????????fmt.Println("decode error:", err) ????} ????fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y) ????fmt.Println("Hello World!") ????var transType IpTransType ????transType.Addr = "127.0.0.1" ????transType.Port = 12345 ????transType.Type = TCP ????InitServer(transType) }


測試結果:

conn is coming 48?decode fail: EOF?outout is? {0 0 0 0 0 0 [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]}?Byte2Int32 is? 231115?length is? [0 3 134 203 0 0 0 1 0 0 0 48 0 0 1 63 38 140 96 48 0 0 0 20 0 0 0 123 0 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]?length is? [0 3 134 203]?length is? 3?[0 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]?length is? {231115 1 48 1370741301296 20 123 [0 19 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]}?FUCK22 ?FUCK22? 0?Warning: End of data: EOF ?


到目前為止我還木有找到一種go夸語言通信編解碼的問題,所以能硬編解碼了。

轉載于:https://my.oschina.net/goldwave/blog/136709

總結

以上是生活随笔為你收集整理的go语言socket通信初试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲激情网 | 国产精品羞羞答答在线观看 | ass极品水嫩小美女ass | 韩国日本中文字幕 | 91成人免费版 | 国产成人午夜精品无码区久久 | 超碰97av| 亚洲丁香 | 无码精品a∨在线观看中文 福利片av | 打屁股视频网站 | 男人天堂资源 | 97插插插 | 毛片一二三区 | 91蝌蚪九色| 91国产丝袜在线播放 | 中文字幕3 | 在线观看成人小视频 | 操www| 韩漫动漫免费大全在线观看 | 少妇太爽了 | 97久久综合 | 欧美日韩中文在线 | 亚欧美精品 | 日韩黄色片子 | av网址在线播放 | 视色视频在线观看 | 丁香久久久 | 精品www久久久久久奶水 | 中文字幕观看av | 午夜在线播放 | 91黄色免费网站 | 黄色av毛片| www.国产在线观看 | 国产精品秘入口18禁麻豆免会员 | 成人久久一区二区 | 有码在线 | 啪啪啪毛片| 天天干在线观看 | 玖玖999 | 99视频只有精品 | 97国产精品久久久 | 天天干精品 | 国产成人无码av在线播放dvd | 男人插入女人下面的视频 | 久久综合中文 | 超碰在线公开免费 | 欧美激情国产一区 | 亚洲色图一区二区三区 | 国内精品久久久久久 | 精品aaa | 日韩国产在线观看 | 免费精品无码AV片在线观看黄 | 成人aⅴ视频 | 久久精品亚洲精品 | y11111少妇 | 四虎黄色网址 | 一级特黄肉体裸片 | 精品在线播放 | 97精品人人a片免费看 | 久久久久亚洲av片无码v | 操伊人 | 手机在线看片日韩 | 国产精品系列在线观看 | 国产永久免费 | 中文字幕乱码人妻无码久久 | 亚洲综合视频在线观看 | 亚洲国产成人精品激情在线 | 麻豆黄色网址 | 欧美成人r级一区二区三区 中文无码一区二区三区在线观看 | 九九热视频免费 | 97精品一区二区视频在线观看 | 一本色道久久综合亚洲精品图片 | 国产成人免费在线观看 | 日日碰碰 | 亚洲精品乱码久久久久 | 毛片视频网站 | 涩涩综合 | 视频二区在线 | 久久精品国产亚洲av蜜臀色欲 | 亚洲福利国产 | 日韩成人区| 波多野结衣av一区二区全免费观看 | 欧美日韩国产一区二区在线观看 | 蜜桃久久久aaaa成人网一区 | h毛片| 东北少妇不带套对白 | 成人性爱视频在线观看 | 色94色欧美 | 一卡二卡精品 | 超碰97av | 午夜亚洲AV永久无码精品蜜芽 | 两个人做羞羞的视频 | 伊人22综合| 老太脱裤让老头玩ⅹxxxx | 一区免费在线 | 超碰免费看 | 国产一级理论片 | 久久这里有 | 狠狠干伊人网 |