Exemple #1
0
        public XXPSocketsModel XXPSendMessage(string ip, XXPSocketsModel Request, int port, int iTimeout = 10000, int size = 2048)
        {
            try
            {
                LogHelper.WriteMethodLog(true);
                //1.发送数据
                TcpClient     client = new TcpClient(ip, port);
                NetworkStream stream = client.GetStream();


                byte[] bytRequest = ByteHelper.ObjectToBytes <XXPSocketsModel>(Request);

                byte[] bylength = BitConverter.GetBytes(bytRequest.Length);
                stream.Write(bylength, 0, bylength.Length);

                //byte[] tmp = new byte[bylength.Length + bytRequest.Length];
                //System.Buffer.BlockCopy(bylength, 0, tmp, 0, bylength.Length);
                //System.Buffer.BlockCopy(bytRequest, 0, tmp, bylength.Length, bytRequest.Length);
                if (bytRequest.Length > 2048)
                {
                    int iTimes = bytRequest.Length / 2048;
                    int iLast  = bytRequest.Length % 2048;
                    for (int i = 0; i < iTimes; i++)
                    {
                        stream.Write(bytRequest, i * 2048, 2048);
                        Thread.Sleep(10);
                    }
                    if (iLast != 0)
                    {
                        stream.Write(bytRequest, iTimes * 2048, iLast);
                    }
                }
                else
                {
                    stream.Write(bytRequest, 0, bytRequest.Length);
                }



                //2.接收状态,长度<1024字节
                byte[] bytes = new Byte[size];
                stream.ReadTimeout = iTimeout;
                stream.Read(bytes, 0, bytes.Length);

                //3.关闭对象
                stream.Close();
                client.Close();

                XXPSocketsModel refRequest = ByteHelper.BytesToObject <XXPSocketsModel>(bytes);

                return(ByteHelper.BytesToObject <XXPSocketsModel>(bytes));
            }
            catch (Exception ex)
            {
                LogHelper.WriteErrorLog(ex.Message);

                XXPSocketsModel Response = new XXPSocketsModel();
                Response.Type  = XXPCoinMsgType.Exception;
                Response.Value = ex.Message;


                return(Response);
            }
            finally
            {
                LogHelper.WriteMethodLog(false);
            }
        }
Exemple #2
0
        public bool XXPStartReceiveMsg(int port)
        {
            this.server = new TcpListener(IPAddress.Any, port);
            this.server.Start();

            Task.Run(() => {
                while (true)
                {
                    try
                    {
                        if (this.server == null)
                        {
                            break;
                        }

                        //2.1 收到请求
                        TcpClient client     = this.server.AcceptTcpClient(); //停在这等待连接请求
                        NetworkStream stream = client.GetStream();

                        byte[] byLength = new byte[4];
                        int retLength   = stream.Read(byLength, 0, byLength.Length);


                        int iReqLength = BitConverter.ToInt32(byLength, 0);
                        byte[] Request = new byte[iReqLength];
                        if (iReqLength > 2048)
                        {
                            int itotalLength = 0;
                            while (itotalLength != iReqLength)
                            {
                                int length    = stream.Read(Request, itotalLength, 2048);
                                itotalLength += length;
                            }
                        }
                        else
                        {
                            int length = stream.Read(Request, 0, Request.Length);
                        }

                        XXPSocketsModel reqModel = ByteHelper.BytesToObject <XXPSocketsModel>(Request);

                        string strTemp     = client.Client.RemoteEndPoint.ToString();
                        reqModel.IpAddress = strTemp.Substring(0, strTemp.IndexOf(":"));
                        if (this.XXPSocketsExecuteCallBack != null)
                        {
                            reqModel = this.XXPSocketsExecuteCallBack(reqModel);
                        }

                        byte[] Response = ByteHelper.ObjectToBytes <XXPSocketsModel>(reqModel);

                        //2.3 返回状态
                        stream.Write(Response, 0, Response.Length);
                        //2.4 关闭客户端
                        stream.Close();
                    }
                    catch (Exception ex)
                    {
                        LogHelper.WriteErrorLog(ex.Message);
                    }
                }
            });

            return(true);
        }