Esempio n. 1
0
        public void reconnSocket(ref SOCK_INFO stSrvSock, ref SOCK_INFO stCliSock)
        {
            double dTimeGap;

            dTimeGap = (DateTime.UtcNow.Ticks - stCliSock.lnResetTime) / 10000.0f;
            //Console.WriteLine("Timer Ticks : {0}", DateTime.UtcNow.Ticks);
            if (dTimeGap > 12000)
            {
                Console.WriteLine("Client Disconnected! TimeGap : {0} milli-second", dTimeGap);
                lock (stCliSock.objLock)
                {
                    stCliSock.bConn = false;
                }
            }
            //Console.WriteLine("Session Flag : {0}", stCliSock.bConn == true ? "TRUE" : "FALSE");
            if (stCliSock.bConn == false)
            {
                lock (stCliSock.objLock)
                {
                    closeSocket(ref stCliSock);
                    Console.WriteLine("Now wait client connection...");
                    stCliSock.objSock = stSrvSock.objSock.Accept();
                }
                stCliSock.bConn = true;
                Console.WriteLine("Socket re-accept SUCCESS!");
            }
            //stCliSock.lnResetTime = DateTime.UtcNow.Ticks;
            //return (int)ENUM_VALUE_SOCKETMGR.SUCCESS;
            return;
        }
Esempio n. 2
0
        //public void closeSocket(ref Socket objSocket, ref object objLock)
        public void closeSocket(ref SOCK_INFO stSock)
        {
            //stSock.objSock != null ?  : Console.WriteLine("Try close null socket!");
            if (stSock.objSock != null)
            {
                lock (stSock.objSock)
                {
                    //stSock.isConn = false;
                    stSock.objSock.Close();
                }
            }

            return;
        }
Esempio n. 3
0
 public byte[] recvByteMsg(ref SOCK_INFO stSock, ref int nMsgLength)
 {
     byte[] szRcvBuff = new byte[1024];
     try
     {
         nMsgLength = stSock.objSock.Receive(szRcvBuff);
     }
     catch (Exception eCmn)
     {
         Thread.Sleep(500);
         Console.WriteLine("Exception!! Source : {0}, Message : {1}",
                           eCmn.Source, eCmn.Message);
     }
     return(szRcvBuff);
 }
Esempio n. 4
0
        //public void SocketMgr(ref Socket objSrvSock, ref Socket objCliSock)
        //public void connSocket(ref Socket objSrvSock, ref Socket objCliSock)
        public void connSocket(ref SOCK_INFO stSrvSock, ref SOCK_INFO stCliSock)
        {
            IPEndPoint objEp = null;

            SOCKET_MGR objSocketMgr = new SOCKET_MGR();

            //ArrayList lstListen = new ArrayList();
            //ArrayList lstAccept = new ArrayList();

            int    nBackLog       = 5;
            int    lnPortNum      = 7007;
            string strLocalIpAddr = "192.168.0.191";

            //stSrvSock.bConn = false;
            stCliSock.bConn = false;

            if ((stSrvSock.objSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) == null)
            {
                return;
            }

            if ((objEp = new IPEndPoint(IPAddress.Parse(strLocalIpAddr), lnPortNum)) == null)
            {
                return;
            }

            stSrvSock.objSock.Bind(objEp);

            stSrvSock.objSock.Listen(nBackLog);

            //Socket.Select(lstListen, null, null, 50000);
            Console.WriteLine("Now waiting CLIENT connect!");
            stCliSock.objSock = stSrvSock.objSock.Accept();

            // up to connection flag
            stCliSock.bConn = true;
            stSrvSock.bConn = true;

            stCliSock.lnResetTime = DateTime.UtcNow.Ticks;
            stSrvSock.lnResetTime = DateTime.UtcNow.Ticks;
            //stCliSock.lnResetTime = new DateTime.
            Console.WriteLine("Socket accept SUCCESS! {0}", stCliSock.lnResetTime);
            return;
        }
Esempio n. 5
0
        public void recvMsg(ref SOCK_INFO stSock)
        {
            __RECOG_RESULT stRecogResult = new __RECOG_RESULT();
            __STAT_INFO    stStatInfo    = new __STAT_INFO();

            RECOGRESULT objRecogResult = new RECOGRESULT();
            STATINFO    objStatInfo    = new STATINFO();

            int nRecvLength = 0;

            byte[] szRcvBuff = new byte[1024];
            string strBuff;

            if (stSock.bConn == false)
            {
                //return (int)ENUM_VALUE_SOCKETMGR.FAILED_NULL_SOCKET;
                Thread.Sleep(500);              // just for TEST
                //Console.WriteLine("Now disconnected to client!");
                return;
            }
            //stSock.lnResetTime = DateTime.UtcNow.Ticks; // just for TEST
            try
            {
                nRecvLength = stSock.objSock.Receive(szRcvBuff);
            }
            catch (Exception eCmn)
            {
                Thread.Sleep(500);
                Console.WriteLine("Exception!! Source : {0}, Message : {1}",
                                  eCmn.Source, eCmn.Message);
                return;
            }
            if (nRecvLength <= 0)
            {
                Thread.Sleep(500);
                Console.WriteLine("Receive NO-DATA{0}", nRecvLength);
                //return (int)ENUM_VALUE_SOCKETMGR.RECV_NO_DATA;
                return;
            }
            else
            {
                strBuff = Encoding.ASCII.GetString(szRcvBuff, 0, nRecvLength);
                Console.WriteLine(strBuff);
                if (szRcvBuff[nRecvLength - 1] != 0x03)
                {
                    Console.WriteLine("Invalid ETX in receive message!");
                }
                else if (strBuff.Contains("LPRS|"))
                {
                    objRecogResult.getRecogResult(ref stRecogResult, ref szRcvBuff);
                }
                else if (strBuff.Contains("ST|"))
                {
                    // re-up coonnection check flag
                    lock (stSock.objLock)
                    {
                        stSock.lnResetTime = DateTime.UtcNow.Ticks;
                        stSock.bConn       = true;
                    }

                    objStatInfo.getStatInfo(ref stStatInfo, ref szRcvBuff);
                }
                else
                {
                    Console.WriteLine("Invalid Instruction!");
                }
                //return (int)ENUM_VALUE_SOCKETMGR.SUCCESS;
                return;
                //nRecvLength = 0;
            }
        }