// Use this for initialization void Start() { m_Socket = new TcpClient(); m_Socket.Connect("192.168.31.236", 8888); m_Stream = m_Socket.GetStream(); LoginC2S msg = new LoginC2S(); msg.name = "huashao"; byte[] dd = ProtobufTool.ProtoBufToBytes <LoginC2S>(msg); int len = dd.Length + 2; Debug.LogError(len); byte[] buf = new byte[len + 6]; buf[0] = (byte)(len & 0xff); buf[1] = (byte)(len >> 8 & 0xff); buf[2] = (byte)(len >> 16 & 0xff); buf[3] = (byte)(len >> 24 & 0xff); buf[4] = 1; buf[5] = 0; Array.Copy(dd, 0, buf, 6, dd.Length); m_Stream.Write(buf, 0, buf.Length); byte[] recv = new byte[1024]; int c = m_Stream.Read(recv, 0, 1024); int rlen = (recv[0] | recv[1] << 8 | recv[2] << 16 | recv[3] << 24); int rId = (recv[4] | recv[5] << 8); Debug.LogError(string.Format("recv len:{0} id:{1}", rlen, rId)); byte [] rbuf = new byte[rlen - 2]; Array.Copy(recv, 6, rbuf, 0, rlen - 2); LoginS2C rmsg = ProtobufTool.BytesToProtoBuf <LoginS2C>(rbuf); Debug.LogError(rmsg.playerId); m_Stream.Close(); m_Socket.Close(); }
void Start() { byte[] recv = new byte[16 * 1024]; connector = new TcpConnector("192.168.31.236", 8888); connector.On(SocketEvents.Message, (d) => { byte[] reData = d as byte[]; Debug.LogError(reData.Length); unpacker.Input(reData); int length; ushort msgId; while (unpacker.MoveNext(recv, out length, out msgId)) { LoginS2C lmsg = ProtobufTool.BytesToProtoBuf <LoginS2C>(recv, 0, length); Debug.LogError("recv msgID: " + msgId + " data:" + lmsg.playerId); } }); var wait = connector.Connect(); if (!Util.Wait(wait, 1000)) { connector.Dispose(); } }
// Use this for initialization void Start() { Person p = new Person(); p.id = 1; p.name = "张三"; p.email = "*****@*****.**"; byte [] array = ProtobufTool.ProtoBufToBytes <Person>(p); Person tp = ProtobufTool.BytesToProtoBuf <Person>(array); string pData = tp.id + " " + tp.name + " " + tp.email; print(pData); text.text = pData + '\n' + SystemInfo.deviceUniqueIdentifier; }