コード例 #1
0
ファイル: SocketWrapper.cs プロジェクト: wpmyj/OF2.0
        public COMMUNICATERESULT SendAndReceive(byte[] SendData, int _BytesToRec, out byte[] RetData)
        {
            //COMMUNICATERESULT ret;
            readcount = 0;
            //int RSendNumber = 0;

            //while (true)
            //{
            RetData = null;
            if (socketconnectstate != SocketConnectState.SendFail)
            {
                if (Write(SendData))
                {
                    if (_BytesToRec > 0)
                    {
                        //读取数据
                        RetData = Read(_BytesToRec, out readcount);
                        if (RetData == null)
                        {
                            return(COMMUNICATERESULT.DataRecFail);
                        }
                        else
                        {
                            socketconnectstate = SocketConnectState.ConnectSuccess;
                            return(COMMUNICATERESULT.OK);
                        }
                    }
                    else
                    {
                        return(COMMUNICATERESULT.OK);
                    }
                }
                else
                {
                    return(COMMUNICATERESULT.DataSendFail);
                }
            }
            else
            {
                return(COMMUNICATERESULT.DataSendFail);
            }
            //判断是否成功读取数据

            /* if (ret == COMMUNICATERESULT.OK)
             * {
             *   return ret;
             * }
             * else
             * {
             *   RSendNumber++;
             *   if (RSendNumber >= 3)
             *   {
             *       //如果连续多次读取不成功,则清空超时次数
             *       RSendNumber = 0;
             *       return ret;
             *   }
             *
             * }*/
            //}
        }
コード例 #2
0
ファイル: SocketWrapper.cs プロジェクト: wpmyj/OF2.0
 private bool Write(byte[] data)
 {
     //-------------------
     //调试用于输出到输出窗口
     //string result = String.Join(",", Array.ConvertAll(data, (Converter<byte, string>)Convert.ToString));
     //DebugHelper.StringOut.OutputWindowStringWithTime(result);
     //----------------
     sendcount = 0;
     try
     {
         sendcount = this.socket.Send(data);
         if (sendcount == data.Length)
         {
             return(true);
         }
         else
         {
             SockErrorStr = null;
             return(false);
         }
     }
     catch (Exception ee)
     {
         SockErrorStr = ee.ToString();
         //DebugHelper.StringOut.OutputWindowStringWithTime(SockErrorStr);
         socketconnectstate = SocketConnectState.SendFail;
         return(false);
     }
 }
コード例 #3
0
ファイル: SocketWrapper.cs プロジェクト: wpmyj/OF2.0
        private void ConnectCallBack(IAsyncResult ar)
        {
            Socket socket = ar.AsyncState as Socket;

            IsconnectCallBack = true;
            IsconnectSuccess  = false;
            //DebugHelp.WriteToFile.WriteToFileWithBG2313("ConnectCallBack", filename);
            try
            {
                socket.EndConnect(ar);
                IsconnectSuccess   = true;
                socketconnectstate = SocketConnectState.ConnectSuccess;
                //DebugHelp.WriteToFile.WriteToFileWithBG2313("socketconnectstate = SocketConnectState.ConnectSuccess;", filename);
            }
            catch (Exception ex)
            {
                string jj = "insideclose" + ex.ToString();
                //DebugHelp.WriteToFile.WriteToFileWithBG2313(jj, filename);
                socketconnectstate = SocketConnectState.connectfail;
                SockErrorStr       = ex.ToString();
            }
            finally
            {
                TimeoutObject.Set();
            }
        }
コード例 #4
0
 private void OnSocketStatus(object param)
 {
     if (param is SocketConnectState)
     {
         SocketConnectState p = param as SocketConnectState;
         if (p.socketState == SocketState.CONNECTED)
         {
             Debug.LogErrorFormat("开始登录 !!!socketState = {0}", p.socketState);
             Debug.LogErrorFormat("开始登录 !!!account = {0}  passed = {1}", base.viewModel.account_text.Value, base.viewModel.passed_text.Value);
         }
     }
 }
コード例 #5
0
ファイル: SocketWrapper.cs プロジェクト: wpmyj/OF2.0
        private byte[] Read(int length, out int ReadCount)
        {
            byte[] returndata = new byte[length];

            Array.Clear(data, 0, data.Length);
            try
            {
                ReadCount = this.socket.Receive(data);
                //--------------
                //调试用于输出到输出窗口
                // string result = String.Join(",", Array.ConvertAll(data, (Converter<byte, string>)Convert.ToString));
                //////DebugHelper.StringOut.OutputWindowStringWithTime(result);
                //------------------------
                if (ReadCount >= length)
                {
                    Array.Copy(data, returndata, length);
                    return(returndata);
                }
                else if (ReadCount == 9)//9为错误响应的长度
                {
                    Array.Copy(data, returndata, 9);
                    return(returndata);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception ee)
            {
                socketconnectstate = SocketConnectState.RecFail;
                SockErrorStr       = ee.ToString();
                //--------------
                //调试用于输出到输出窗口
                //DebugHelper.StringOut.OutputWindowStringWithTime(SockErrorStr);
                //------------------------
                ReadCount = 0;
                return(null);
            }
        }
コード例 #6
0
ファイル: SocketWrapper.cs プロジェクト: wpmyj/OF2.0
        //string filename = "socketconncetdebug.txt";
        public bool Connect()
        {
            TimeoutObject.Reset();
            comunicate++;
            //string outputstring="判断,socketconnectstate="+socketconnectstate.ToString()+"IsconnectCallBack="+IsconnectCallBack.Equals(true).ToString();
            //DebugHelp.WriteToFile.WriteToFileWithBG2313(outputstring, filename);

            if ((!ConnectState() && IsconnectCallBack) || comunicate > 20)
            {
                comunicate       = 0;
                IsconnectSuccess = false;
                try
                {
                    this.socket.Disconnect(true);
                }
                catch
                {
                    System.Diagnostics.Debug.WriteLine("socket");
                }
                try
                {
                    //DebugHelp.WriteToFile.WriteToFileWithBG2313("beginsocket", filename);
                    ar = this.socket.BeginConnect(ip, BeginConnectAsynCallback, socket);
                    IsconnectCallBack = false;
                    //DebugHelp.WriteToFile.WriteToFileWithBG2313("IsconnectCallBack = false", filename);
                }
                catch (Exception err)
                {
                    //出现套接字已释放的异常则重新创建套接字
                    if (err is System.ObjectDisposedException)
                    {
                        CreatSocket(TCPPara);
                    }
                    SockErrorStr       = err.ToString();
                    socketconnectstate = SocketConnectState.connectfail;
                    return(false);
                }
                if (TimeoutObject.WaitOne(1000, false))
                {
                    if (IsconnectSuccess)
                    {
                        socketconnectstate = SocketConnectState.ConnectSuccess;
                        return(true);
                    }
                    else
                    {
                        socketconnectstate = SocketConnectState.connectfail;
                        return(false);
                    }
                }
                else
                {
                    SockErrorStr = "Time Out";
                    return(false);
                }
            }
            else if (ConnectState())
            {
                return(true);
            }
            else
            {
                SockErrorStr = "套接字没有赋值";
                return(false);
            }
        }