Ejemplo n.º 1
0
        /// <summary>
        /// Starts the socket connection from the TCP listener and stores the socket in the state, as well as the callback.
        /// </summary>
        /// <param name="ar"></param>
        public static void Accept_a_New_Client(IAsyncResult ar)
        {
            PreservedState listener_state = (ar.AsyncState as PreservedState);

            PreservedState pstate = new PreservedState();

            pstate.State_Socket   = listener_state.State_Socket.EndAccept(ar); // A new socket is "generated" here - every socket needs a new state
            pstate.State_Callback = listener_state.State_Callback;

            pstate.State_Callback(pstate); // Pass in the new state, let the server store it
            listener_state.State_Socket.BeginAccept(Accept_a_New_Client, listener_state);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// A method that ends the connection
        /// </summary>
        /// <param name="state"></param>
        private static void Connected_to_Server(IAsyncResult state)
        {
            PreservedState pstate = (state.AsyncState as PreservedState);

            try
            {
                pstate.State_Socket.EndConnect(state);
                pstate.State_Callback(pstate);
            }
            catch (Exception e)
            {
                //pstate.State_Socket.Shutdown(SocketShutdown.Both);
                pstate.State_Socket.Close();
                //pstate.State_Callback(pstate);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        ///A method that uses the IAsyncResult state to save our GUIcall back for future use, constantly looping to
        /// receive more data
        /// </summary>
        /// <param name="state"></param>
        private static void ReceiveCallback(IAsyncResult state)
        {
            PreservedState pstate = (state.AsyncState as PreservedState);

            try
            {
                int byt = pstate.State_Socket.EndReceive(state);
                lock (pstate)
                {
                    if (byt > 0)
                    {
                        pstate.data.Append(encode.GetString(pstate.buffer, 0, byt));
                        pstate.State_Callback(pstate);
                    }
                }
            }
            catch (Exception e)
            {
                //pstate.State_Socket.Shutdown(SocketShutdown.Both);
                pstate.State_Socket.Close();
                //pstate.State_Callback(pstate);
            }
        }