Exemple #1
0
        public void Run(byte[] binData)
        {
            IPAddress ip           = IPAddress.Parse("127.0.0.1");
            Socket    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            try
            {
                clientSocket.Connect(new IPEndPoint(ip, 8081)); //zsummerX/example/frameStressTest server default port
                Console.WriteLine("connect Success.");
            }
            catch
            {
                Console.WriteLine("connect Failed");
                return;
            }
            do
            {
                var sendData = new System.Collections.Generic.List <byte>();

                NetHeader head = new NetHeader();
                head.packLen = (UInt32)(4 + 2 + 2 + binData.Length);
                head.protoID = Proto4z.EchoPack.getProtoID();

                sendData.AddRange(head.__encode());
                sendData.AddRange(binData);
                clientSocket.Send(sendData.ToArray());



                var       recvBytes = new byte[2000];
                uint      curLen    = 0;
                uint      needLen   = 4 + 2 + 2;
                uint      recvLen   = 0;
                NetHeader recvHead  = new NetHeader();
                do
                {
                    recvLen = (uint)clientSocket.Receive(recvBytes, (int)curLen, (int)needLen, System.Net.Sockets.SocketFlags.None);
                    if (recvLen == 0)
                    {
                        return;
                    }
                    curLen  += recvLen;
                    needLen -= recvLen;
                    if (needLen == 0 && curLen == 4 + 2 + 2) //head already read finish
                    {
                        int pos = 0;
                        recvHead.__decode(recvBytes, ref pos);
                        needLen = recvHead.packLen - 4 - 2 - 2;
                    }
                    else if (needLen == 0)
                    {
                        if (recvHead.protoID == Proto4z.EchoPack.getProtoID())
                        {
                            Proto4z.EchoPack result = new Proto4z.EchoPack();
                            int pos = 4 + 2 + 2;
                            result.__decode(recvBytes, ref pos);
                            //System.Console.WriteLine("echo =" + result.text.val);
                        }
                        break;
                    }
                    recvLen = 0;
                } while (true);
            } while (true);
        }
Exemple #2
0
        public void Run(byte[] binData)
        {

            IPAddress ip = IPAddress.Parse("127.0.0.1");
            Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            try
            {
                clientSocket.Connect(new IPEndPoint(ip, 8081)); //zsummerX/example/frameStressTest server default port 
                Console.WriteLine("connect Success.");
            }
            catch
            {
                Console.WriteLine("connect Failed");
                return;
            }
            do
            {
                var sendData = new System.Collections.Generic.List<byte>();

                NetHeader head = new NetHeader();
                head.packLen = (UInt32)(4 + 2 + 2 + binData.Length);
                head.protoID = Proto4z.EchoPack.getProtoID();

                sendData.AddRange(head.__encode());
                sendData.AddRange(binData);
                clientSocket.Send(sendData.ToArray());


                
                
                var recvBytes = new byte[2000];
                uint curLen = 0;
                uint needLen = 4 + 2 + 2;
                uint recvLen = 0;
                NetHeader recvHead = new NetHeader();
                do
                {
                    recvLen = (uint)clientSocket.Receive(recvBytes, (int)curLen, (int)needLen, System.Net.Sockets.SocketFlags.None);
                    if (recvLen == 0)
                    {
                        return;
                    }
                    curLen += recvLen;
                    needLen -= recvLen;
                    if (needLen == 0 && curLen == 4 + 2 + 2) //head already read finish
                    {
                        int pos = 0;
                        recvHead.__decode(recvBytes, ref pos);
                        needLen = recvHead.packLen - 4 - 2 - 2;
                    }
                    else if (needLen == 0)
                    {
                        if (recvHead.protoID == Proto4z.EchoPack.getProtoID())
                        {
                            Proto4z.EchoPack result = new Proto4z.EchoPack();
                            int pos = 4+2+2;
                            result.__decode(recvBytes, ref pos);
                            //System.Console.WriteLine("echo =" + result.text.val);
                        }
                        break;
                    }
                    recvLen = 0;
                } while (true);


            } while (true);

        }