Gorang as the server, C (unity) as the client, Socket communication problems

Server datalen + message ----- > packagedata ----- > network transmission ([] byte) ---- > client ----- > unpackedata ----- > datalen + message

To write network program with Soket, you need to customize the protocol yourself.

{len+data} len parses the length of data, and data parses the data to be transmitted

Message on server

Message of client

It should be noted that the field naming of the client Message needs to be kept consistent with the json label of the server. Otherwise, the server will lose fields when parsing json (I found this bug one night plus one morning)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
[Serializable]
public class Message
{
    public string execute_type;
    public string struct_name;
    public string data;
}
class util
{
    /// <summary>
    /// Packaging data
    /// </summary>
    /// <param name="data"></param>
    /// <param name="exeCuteType"></param>
    /// <param name="className"></param>
    public static byte[] PackageData(object data,string exeCuteType,string className)
    {
        string jsonData = JsonConvert.SerializeObject(data);
        
        //byte[] byteData = Encoding.UTF8.GetBytes(jsonStr);
        Message message = new Message();
        message.struct_name = className;
        message.data = jsonData;
        message.execute_type = exeCuteType;

        
        byte[] byteData =Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message));
        Int32 len = byteData.Length;
        byte[] lenData = BitConverter.GetBytes(len);
        byte[] finalData = new byte[lenData.Length + byteData.Length];
        lenData.CopyTo(finalData, 0);
        byteData.CopyTo(finalData, 4);
        return finalData;
        
    }
    /// <summary>
    /// Split data
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static Message UnPackageData(byte[] data)
    {
        Int32 len = BitConverter.ToInt32(data, 0);
        System.Console.WriteLine(len);
        byte[] strByte = new byte[len];
        Array.Copy(data,4,strByte,0,len);
        Message message = JsonConvert.DeserializeObject<Message>(Encoding.UTF8.GetString(strByte));
        return message;

    }
}
//No, go??
//Packaging data
func  PackageData(data interface{},executeType string,structType string)[]byte{
    dataByte,err:=json.Marshal(&data)
    if err!=nil{
        config.Logger.Fatalln("Parsing error",err)
    }
    strJson:=string(dataByte)
    msg:=Message{}
    msg.ExecuteType=executeType
    msg.Data=strJson
    msg.StructName=structType
    finalData,err:=json.Marshal(&msg)
    if err!=nil{
        log.Fatalln("Resolution failure",err)
    }
    var lenByte =make([]byte,4)
    length:=uint32(len(finalData))
    binary.LittleEndian.PutUint32(lenByte,length)


    allData:=[][]byte{lenByte,finalData}
    newData:=bytes.Join(allData,[]byte(""))

    return newData

}
//Analytical data
func UnPackageData(data []byte)(Message, error){
    //println("Total length=:",len(data))
    mes:=Message{}
    //utf8.
    byteBuffer:=bytes.NewBuffer(data[0:4])
    var dataLen int32
    err:=binary.Read(byteBuffer,binary.LittleEndian,&dataLen)//binary.BigEndian.Uint32(data[:4])
    if err!=nil{
        config.Logger.Println(err)
    }
    err=json.Unmarshal(data[4:4+dataLen],&mes)
    if err!=nil{
        fmt.Println("Resolution failure",err)
    }

    return mes,err
}

Tags: PHP JSON encoding network

Posted on Mon, 04 Nov 2019 11:53:47 -0500 by fukas