示例#1
0
 /// <summary>
 /// Start waiting for data from the client
 /// </summary>
 /// <param name="soc"></param>
 public void WaitForData(System.Net.Sockets.Socket soc)
 {
     try
     {
         if (pfnWorkerCallBack == null)
         {
             // Specify the call back function which is to be
             // invoked when there is any write activity by the
             // connected client
             pfnWorkerCallBack = new AsyncCallback(OnDataReceived);
         }
         SocketPacket theSocPkt = new SocketPacket();
         theSocPkt.m_currentSocket = soc;
         // Start receiving any data written by the connected client
         // asynchronously
         soc.BeginReceive(theSocPkt.dataBuffer, 0,
                          theSocPkt.dataBuffer.Length,
                          SocketFlags.None,
                          pfnWorkerCallBack,
                          theSocPkt);
     }
     catch (Exception ex)
     {
         program.errorreport(ex);
     }
 }
示例#2
0
        /// <summary>
        /// This call back will be executed when the server a client start transmiting data
        /// </summary>
        /// <param name="asyn"></param>
        public void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                SocketPacket socketData = (SocketPacket)asyn.AsyncState;

                int iRx = 0;

                iRx = socketData.m_currentSocket.EndReceive(asyn);
                char[] chars          = new char[iRx + 1];
                System.Text.Decoder d = System.Text.Encoding.UTF8.GetDecoder();
                int charLen           = d.GetChars(socketData.dataBuffer,
                                                   0, iRx, chars, 0);
                System.String szData = new System.String(chars);
                //    if (szData.Contains("ENDOFMESSAGE"))
                {
                    //  szData.Replace("ENDOFMESSAGE", "");


                    string res = this.ExecuteCommand(szData);



                    Object objData = res + "ENDOFMESSAGE";
                    byte[] byData  = System.Text.Encoding.UTF8.GetBytes(objData.ToString());
                    if (socketData.m_currentSocket != null)
                    {
                        socketData.m_currentSocket.Send(byData);

                        for (int i = 0; i < clientCount; i++)
                        {
                            if (m_workerSocket[i].RemoteEndPoint == socketData.m_currentSocket.RemoteEndPoint)
                            {
                                if (m_workerSocket[i] != null)
                                {
                                    if (m_workerSocket[i].Connected)
                                    {
                                        m_workerSocket[i].Send(byData);
                                    }
                                }
                            }
                        }
                    }
                }
                // Continue the waiting for data on the Socket
                WaitForData(socketData.m_currentSocket);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
            }
            catch (Exception ex)
            {
                program.errorreport(ex);
            }
        }