Пример #1
0
        public void StartTCPListener()
        {
            if (isRunning)
            {
                return;
            }
            ushort      port        = Server.Instance.Port;
            IPAddress   localAddr   = IPAddress.Parse("0.0.0.0");
            TcpListener tcpListener = null;

            try
            {
                tcpListener = new TcpListener(localAddr, port);
                tcpListener.Start();
                Server.Instance.Port = (ushort)((IPEndPoint)tcpListener.LocalEndpoint).Port; //get the port we are using.
                isRunning            = true;
                ProcessIcon.Instance.UpdateIcon();
                while (true)
                {
                    try
                    {
                        //Accept the pending client connection and return a TcpClient object initialized for communication.
                        TcpClient tcpClient = tcpListener.AcceptTcpClient();
                        TcpUser   user      = new TcpUser(tcpClient);
                        connections.Add(user);
                        Task.Factory.StartNew(() =>
                        {
                            ClientThread(user);
                        });
                        Server.Instance.UpdateHTML();
                    }
                    catch (SocketException e)
                    {
                        Console.WriteLine("Socket Exception: {0}", e);
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("Critical Error: Socket not started");
                Console.WriteLine("Socket Exception: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                tcpListener.Stop();
                isRunning = false;
                ProcessIcon.Instance.UpdateIcon();
            }
        }
Пример #2
0
        void ClientThread(TcpUser user)
        {
            TcpClient client = user._tcpclient; //less renaming
            bool      goturl = false;

            // Buffer for reading data
            Byte[]        bytes  = new Byte[1024];
            String        data   = null;
            NetworkStream stream = client.GetStream();

            try
            {
                client.ReceiveTimeout = 20000;
                data = null;

                // Get a stream object for reading and writing


                int i;

                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    //Console.WriteLine("Received: {0}", data);

                    //check header for request type
                    String   HttpRequestType = "";;
                    string[] Lines           = data.Replace("\r\n", "\n").Split('\n');
                    int      Ret;
                    //Extract requested URL
                    if (Lines.Length > 0)
                    {
                        //Parse the Http Request Type
                        Ret = Lines[0].IndexOf(' ');
                        if (Ret > 0)
                        {
                            HttpRequestType = Lines[0].Substring(0, Ret);
                            Lines[0]        = Lines[0].Substring(Ret).Trim();
                            if (!goturl)
                            {
                                user.SetUrl(Lines);
                            }
                        }

                        if (HttpRequestType.ToUpper().Equals("GET"))
                        {
                            int id = 0;
                            if (Lines[0].ToUpper().Contains("OUTPUT.HTML"))
                            {
                                id = 1;
                            }
                            SendWebpage(stream, id);
                        }
                        else if (HttpRequestType.ToUpper().Equals("POST"))
                        {
                            int    index      = data.IndexOf("\r\n\r\n");
                            String m_HttpPost = data.Substring(index + 4);
                            int    status     = Server.Instance.ParseCommand(m_HttpPost);

                            switch (status)
                            {
                            case 0:
                                SendRedirect(stream, user.Host);
                                break;

                            case -1:
                                SendBad(stream, user.Host, strError);
                                break;

                            case -2:
                                SendBad(stream, user.Host, strBadKey);
                                break;

                            default:
                                break;
                            }
                        }
                    }
                }

                // Shutdown and end connection
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            catch (System.IO.IOException e)
            {
                Console.WriteLine("IOException: {0}", e);
            }
            finally
            {
                bytes = null;
                data  = null;
                stream.Flush();
                stream.Dispose();
                // Stop listening for new clients.
                connections.Remove(user);
                client.Close();
                //Console.WriteLine("#CLOSED CONNECTION#");
                Server.Instance.UpdateHTML();
            }
        }