示例#1
0
    private void AcceptClient(IAsyncResult ir)
    {
        TcpListener listener  = ir.AsyncState as TcpListener;
        TcpClient   tcpClient = listener.EndAcceptTcpClient(ir);
        //4.创建一个流用来收发数据
        NetworkStream stream = tcpClient.GetStream();

        //读入,也就是接受一个数据
        byte[]          data = new byte[1024];
        Login.RespLogin rsp  = new Login.RespLogin();
        rsp.isWin = true;
        rsp.level = 5;
        MemoryStream ms   = new MemoryStream();
        ByteBuffer   buff = new ByteBuffer();

        ProtoBuf.Meta.RuntimeTypeModel.Default.Serialize(ms, rsp);
        data = ms.ToArray();
        buff.WriteShort((ushort)ms.Length);
        buff.WriteShort((ushort)1002);
        buff.WriteBytes(data);
        stream.Write(buff.ToBytes(), 0, buff.ToBytes().Length);
        ms.Close();
        //5.关闭相应的流和监听器
        stream.Close();
        listener.BeginAcceptTcpClient(AcceptClient, listener);
    }
示例#2
0
 private void Handler(object o)
 {
     Login.RespLogin rsp = o as Login.RespLogin;
     Debug.Log("isWin----" + rsp.isWin + "---level---" + rsp.level);
 }