예제 #1
0
        /// <summary>
        /// Waiting for the data from server
        /// </summary>
        /// <param name="Soc"></param>
        private void WaitForData(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);
                }

                CommObject theSocketPckt = new CommObject();

                theSocketPckt.m_CurrentSocket = Soc;

                // Start receiving any data written by the connected client asynchronously

                Soc.BeginReceive(theSocketPckt.dataBuffer,
                                 0, theSocketPckt.dataBuffer.Length,
                                 SocketFlags.None,
                                 pfnWorkerCallBack,
                                 theSocketPckt);
            }
            catch (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }
예제 #2
0
        /// <summary>
        ///  This the call back function which will be invoked when the socket
        ///  detects any client writing of data on the stream
        /// </summary>
        /// <param name="asyn"></param>
        private void OnDataReceived(IAsyncResult asyn)
        {
            try
            {
                CommObject SocketData = (CommObject)asyn.AsyncState;

                int iRx = 0;

                // Complete the BeginReceive() asynchronous call by EndReceive() method
                // which will return the number of characters written to the stream
                // by the client

                iRx = SocketData.m_CurrentSocket.EndReceive(asyn);
                char[] chars = new char[iRx + 1];

                // Decoder d = Encoding.UTF8.GetDecoder();
                Decoder d = Encoding.ASCII.GetDecoder();

                int charLen = d.GetChars(SocketData.
                                         dataBuffer,
                                         0,
                                         iRx,
                                         chars,
                                         0);

                string szData = new string(chars);

                // There  might be more data, so store the data received so far.
                SocketData.sb.Append(Encoding.UTF8.GetString(
                                         SocketData.dataBuffer, 0, iRx));

                string textq = SocketData.sb.ToString().Trim('\0');
                Logger.Info(textq);
                string text = System.Text.Encoding.ASCII.GetString(SocketData.dataBuffer);
                Logger.Info(textq);
                //rtbRcvMessage.Text = rtbRcvMessage.Text + szData;

                byte[] bytes = Encoding.ASCII.GetBytes(textq);

                char[] charsm = Encoding.ASCII.GetChars(bytes);
                Logger.Info(charsm.ToString());

                SetRcvText(szData + "\n");


                // 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 (SocketException se)
            {
                MessageBox.Show(se.Message);
            }
        }