Exemplo n.º 1
0
 /// <summary>
 /// This function is referenced by the BeginConnect method above and is "called" by the OS when the
 /// socket connects to the server.
 ///
 /// Once a connection is established the "saved away" callbackFunction is called. This function
 /// is saved in the socket state, and was originally passed in to ConnectToServer.
 /// </summary>
 /// <param name="ar">Contains the "state" object saved in the ConnectToServer function.</param>
 private static void ConnectedToServer(IAsyncResult ar)
 {
     try
     {
         SocketState ss = (SocketState)ar.AsyncState;
         ss.theSocket.EndConnect(ar);
         ss.callMe(ss);
         ss.theSocket.BeginReceive(ss.messageBuffer, 0, 1024, SocketFlags.None, new AsyncCallback(Networking.ReceiveCallback), (object)ss);
     }
     catch (Exception)
     {
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// This function is called by the OS when new data arrives. This function checks 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="ar">Contains the "state" object saved in the ConnectToServer function.</param>
        private static void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                SocketState ss = (SocketState)ar.AsyncState;

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

                if (bytesRead > 0)
                {
                    string message = Encoding.UTF8.GetString(ss.messageBuffer, 0, bytesRead);

                    ss.sb.Append(message);
                    ss.callMe(ss);
                    GetData(ss);
                }
            }
            catch (Exception)
            {
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Callback function called when a new client has connected to the server.
        ///
        /// We begin receiving data from the client at this point.
        /// </summary>
        /// <param name="ar">The SocketState object.</param>
        public static void AcceptNewClient(IAsyncResult ar)
        {
            Console.WriteLine("A new client has connected to the server!");

            SocketState  ss          = (SocketState)ar.AsyncState;
            Socket       theSocket   = ss.theSocket;
            IAsyncResult asyncResult = ar;
            Socket       socket      = theSocket.EndAccept(asyncResult);

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Debug, true);

            SocketState ss1 = new SocketState();

            ss1.theSocket = socket;
            ss1.theSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Debug, true);
            ss.callMe(ss1);

            AsyncCallback callback = new AsyncCallback(Networking.AcceptNewClient);

            SocketState ss2 = ss;

            theSocket.BeginAccept(callback, (object)ss2);
        }