示例#1
0
        // This the call back function which will be invoked when the socket
        // detects any client writing of data on the stream
        public void OnDataReceived(IAsyncResult asyn)
        {
            SocketPacket socketData = (SocketPacket)asyn.AsyncState;

            try
            {
                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream
                // by the client
                int    iRx   = socketData.m_currentSocket.EndReceive(asyn);
                char[] chars = new char[iRx + 1];
                // Extract the characters as a buffer
                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);
                UpdateLogControl("Client " + socketData.m_clientNumber + ": " + szData);
                //Send back a reply to the client
                //string replyMsg = "";
                //UpdateLogControl("Server Reply: " + replyMsg);
                //Convert the reply to byte array
                //byte[] byData = System.Text.Encoding.ASCII.GetBytes(replyMsg);
                //Socket workerSocket = (Socket)socketData.m_currentSocket;
                //workerSocket.Send(byData);
                WaitForData(socketData.m_currentSocket, socketData.m_clientNumber);
            }
            catch (ObjectDisposedException)
            {
                System.Diagnostics.Debugger.Log(0, "1", "\nOnDataReceived: Socket has been closed\n");
            }
            catch (SocketException se)
            {
                CheckDisconnect(se, socketData.m_clientNumber);
            }
        }
示例#2
0
 public void WaitForData(System.Net.Sockets.Socket soc, int clientNumber)
 {
     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(soc, clientNumber);
         soc.BeginReceive(theSocPkt.dataBuffer, 0, theSocPkt.dataBuffer.Length, SocketFlags.None, pfnWorkerCallBack, theSocPkt);
     }
     catch (SocketException se)
     {
         CheckDisconnect(se, clientNumber);
     }
 }