示例#1
0
        /// <summary>
        /// This function is "called" by the operating system when data is available on the socket
        /// </summary>
        /// <param name="ar"></param>
        private static void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                SocketState state = (SocketState)ar.AsyncState;

                int bytesRead = state.theSocket.EndReceive(ar);

                // If the socket is still open
                if (bytesRead > 0)
                {
                    string theMessage = Encoding.UTF8.GetString(state.messageBuffer, 0, bytesRead);
                    // Append the received data to the growable buffer.
                    // It may be an incomplete message, so we need to start building it up piece by piece
                    state.sb.Append(theMessage);

                    // We can't process the message directly, because different users of this library might have different
                    // processing needs.
                    // ProcessMessage(state);

                    // Instead, just invoke the client's delegate, so it can take whatever action it desires.
                    state.callMe(state);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Client has disconnected " + e);
            }
        }
            public static void ReceiveCallback(IAsyncResult stateAsArObject)
            {
                SocketState ss = (SocketState)stateAsArObject.AsyncState;

                int bytesRead = 0;

                try
                {
                    bytesRead = ss.theSocket.EndReceive(stateAsArObject);
                }
                catch (Exception e)
                { ss.theSocket.Close(); }

                // If the socket is still open
                if (bytesRead > 0)
                {
                    string theMessage = Encoding.UTF8.GetString(ss.messageBuffer, 0, bytesRead);
                    // Append the received data to the growable buffer.
                    // It may be an incomplete message, so we need to start building it up piece by piece
                    ss.sb.Append(theMessage);

                    ss.callMe(ss);
                }

                // Continue the "event loop" that was started on line 100.
                // Start listening for more parts of a message, or more new messages

                //   ss.theSocket.BeginReceive(ss.messageBuffer, 0, ss.messageBuffer.Length, SocketFlags.None, ReceiveCallback, ss);

                try
                {
                    ss.theSocket.BeginReceive(ss.messageBuffer, 0, ss.messageBuffer.Length, SocketFlags.None, ReceiveCallback, ss);
                }
                catch (Exception e)
                { ss.theSocket.Close(); }



                /**
                 * The ReceiveCallback method is called by the OS when new data arrives. This method should check to see how much data has arrived. If 0, the connection has been closed (presumably by the server).
                 * On greater than zero data, this method should get the SocketState object out of the IAsyncResult (just as above in 2), and call the callMe provided in the SocketState.
                 **/
            }
示例#3
0
        /// <summary>
        /// This function is "called" by the operating system when the remote site acknowledges connect request
        /// </summary>
        /// <param name="ar"></param
        private static void ConnectedCallback(IAsyncResult ar)
        {
            SocketState state = (SocketState)ar.AsyncState;

            try
            {
                // Complete the connection.
                state.theSocket.EndConnect(ar);
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Unable to connect to server. Error occured: " + e);
                return;
            }

            // Don't start an event loop to receive data from the server. The client might not want to do that.
            //state.theSocket.BeginReceive(state.messageBuffer, 0, state.messageBuffer.Length, SocketFlags.None, ReceiveCallback, state);

            // Instead, just invoke the client's delegate so it can take whatever action it desires.
            state.callMe(state);
        }