コード例 #1
0
 public HttpClientObject(TcpClient client, HttpConnectInfo info)
 {
     Client = client;
     Info   = info;
 }
コード例 #2
0
        public static Thread StartServer()
        {
            Thread tt = new Thread(() =>
            {
                //sockets();
                //return;
                try
                {
                    listener = new TcpListener(IPAddress.Any, Port);
                    Console.WriteLine("Http server was started, port: " + Port);
                    listener.Start();
                    Log("Connections waiting...");

                    while (true)
                    {
                        try
                        {
                            TcpClient client = listener.AcceptTcpClient();
                            var addr         = (client.Client.RemoteEndPoint as IPEndPoint).Address;
                            var ip           = addr.ToString();
                            Console.WriteLine("connected: " + ip);
                            Log("connected: " + ip);
                            if (!FilterIp || (AllowedIps.Contains(ip)))
                            {
                                var cinf = new HttpConnectInfo()
                                {
                                    Ip = addr.ToString()
                                };

                                /*HttpServer.Infos.Add();
                                 *
                                 * while (HttpServer.Infos.Count > 100)
                                 * {
                                 *  HttpServer.Infos.RemoveAt(0);
                                 * }*/

                                var clientObject = new HttpClientObject(client, cinf);

                                Thread clientThread = new Thread(clientObject.Process);

                                clientThread.Start();
                                Connects++;
                            }
                            else
                            {
                                client.Close();
                            }
                        }
                        catch (Exception ex)
                        {
                            Log(ex.Message);
                            Console.WriteLine(ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log(ex.Message);
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    if (listener != null)
                    {
                        listener.Stop();
                        Log("listener stopping..");
                    }
                }
            });

            tt.IsBackground = true;
            tt.Start();
            return(tt);
        }