コード例 #1
0
        public void isPending(Object source, System.Timers.ElapsedEventArgs e, TcpListener server, Timer timer)
        {
            //Check to see if any clients are waiting to connect to the server
            if (!server.Pending())
            {
                Console.Write(".");
            }
            else
            {
                try
                {
                    // Buffer for reading data
                    Byte[] bytes    = new Byte[256];
                    String response = "Server received data.";
                    //Enter the listening loop.
                    while (true)
                    {
                        Console.Write("Listening for a connection");

                        // Perform a blocking call to accept requests.
                        TcpClient client = server.AcceptTcpClient();
                        Console.WriteLine("Connected!");

                        // Get a stream object for reading and writing
                        NetworkStream stream = client.GetStream();

                        // Loop to receive all the data sent by the client.
                        while ((stream.Read(bytes, 0, bytes.Length)) != 0)
                        {
                            // Translate data bytes to a ASCII string.
                            object request = ClientSocket.Deserialize(bytes) as object;
                            Console.WriteLine("Server Received: " + request.ToString());

                            try
                            {
                                Console.WriteLine("Processing Request...");
                                response = "The request was processed successfully";

                                // Process the request sent by the client.
                                ProcessRequest(request);
                            }
                            catch (Exception ErrorProcessRequest)
                            {
                                response = "The request failed to be processed. Error details: " + ErrorProcessRequest;
                            }

                            byte[] msg = System.Text.Encoding.ASCII.GetBytes(response);

                            // Send back a response.
                            stream.Write(msg, 0, msg.Length);
                            Console.WriteLine("Server Sent: " + response);
                        }

                        Console.WriteLine("Server has exited while loop.");
                        // Shutdown and end connection

                        // When closing server for good, the timer should be stopped and/or
                        // * disposed if the server is completely stopped.
                        stream.Close();
                        client.Close();
                        Console.WriteLine("Server Connection Closed.");
                    }
                }
                finally
                {
                    // Stop listening for new clients.
                    server.Stop();
                }
            }
        }