Ejemplo n.º 1
0
        /// <summary>
        /// This function attempts to connect to the server via a provided hostname.
        /// It saves the callback function (in a state object) for use when data arrives.
        /// </summary>
        /// <param name="callback"> A function inside the View to be called when a connection is made </param>
        /// <param name="hostname"> The name of the server to connect to </param>
        public static Socket Connect_to_Server(Delegate callback, string hostname)
        {
            Socket socket = null;
            // Connect to server
            IPHostEntry ipHostInfo = Dns.Resolve(hostname);
            IPAddress   ipAddress  = ipHostInfo.AddressList[0];
            IPEndPoint  hostep     = new IPEndPoint(ipAddress, 11000);

            // Create socket to connect
            socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // Make new game state
            PreservedState NewPreserved = new PreservedState(socket, callback);

            socket.BeginConnect(hostep, new AsyncCallback(Connected_to_Server), NewPreserved);
            return(socket);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 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 call the callback function provided above
        /// </summary>
        /// <param name="state_in_an_ar_object"></param>
        public static void ReceiveCallback(IAsyncResult state_in_an_ar_object)
        {
            // Get the state back from prameter
            PreservedState TheState = (PreservedState)state_in_an_ar_object.AsyncState;

            lock (TheState.sb)
            {
                // Get number of bytes recieved and end the receive
                int BytesRead = TheState.TheSocket.EndReceive(state_in_an_ar_object);

                string ConvertedString = encoding.GetString(TheState.Buffer);

                TheState.sb.Append(ConvertedString);

                TheState.Callback.DynamicInvoke(TheState);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Connects to the cerver
        /// </summary>
        /// <param name="state_in_an_ar_object"></param>
        public static void Connected_to_Server(IAsyncResult state_in_an_ar_object)
        {
            // Grab state object from param
            PreservedState TheState = (PreservedState)state_in_an_ar_object.AsyncState;

            try
            {
                // End the connection
                TheState.TheSocket.EndConnect(state_in_an_ar_object);

                TheState.Callback.DynamicInvoke(TheState);
                // Start receiving state
                TheState.TheSocket.BeginReceive(TheState.Buffer, 0, TheState.MAXBUFFERSIZE, 0, new AsyncCallback(ReceiveCallback), TheState);
            }
            catch (Exception e)
            {
                TheState.sb.Append("Could not connect!");
                TheState.Callback.DynamicInvoke(TheState);
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Helper function that the client View code will call whenever it wants more data.
 /// Note: the client will probably want more data every time it gets data
 /// </summary>
 public static void i_want_more_data(PreservedState state)
 {
     state.TheSocket.BeginReceive(state.Buffer, 0, state.MAXBUFFERSIZE, 0, new AsyncCallback(ReceiveCallback), state);
 }