예제 #1
0
        private void AsServer()
        {
            DisableBtn();
            server = new TCPListener()
            {
                Port = 8009
            };
            server.AcceptCompleted     += Server_AcceptCompleted;
            server.SendCompleted       += Server_SendCompleted;
            server.ReceiveCompleted    += Server_ReceiveCompleted;
            server.DisconnectCompleted += Server_DisconnectCompleted;
            server.Start();
            var t = new Thread(() => {
                while (true)
                {
                    Thread.Sleep(2000);
                    int nowCount = server.Count;
                    if (nowCount != lastCount)
                    {
                        AppendText($"客户端数量:{nowCount}");
                        lastCount = nowCount;
                    }
                    ;
                }
            })
            {
                IsBackground = true
            };

            t.Start();
        }
예제 #2
0
파일: FTPServer.cs 프로젝트: iam7roni/FTP
        private void  Run()
        {
            Thread ClientThread;

            FTPCommandListner = new TCPListener(21);
            FTPCommandListner.Start();
            try
            {
                while (true)
                {
                    ClientSocket = FTPCommandListner.Accept();
                    ClientThread = new Thread(new ThreadStart(FTPClientThread));
                    ClientIP     = ClientSocket.RemoteEndpoint.ToString();
                    //Raise Event
                    ClientID++;
                    onMsgArrived(ClientID, ClientIP);
                    Connected clientConnected = new Connected(Console.Connected);
                    clientConnected(ClientThread, ClientIP);
                    ClientThread.Start();
                }
            }
            catch (ThreadInterruptedException e)
            {
                Thread.CurrentThread.Abort();
            }
            catch (ThreadStopException e)
            {
                System.Console.WriteLine("Thread killed");
            }
        }
예제 #3
0
파일: server.cs 프로젝트: Lockey-J/wodsoft
        private void server_Load(object sender, EventArgs e)
        {
            listener = new TCPListener();

            listener.Port              = 1314;
            listener.ReceiveCompleted += Listener_ReceiveCompleted;
            listener.Start();
        }
예제 #4
0
파일: server.cs 프로젝트: Lockey-J/wodsoft
 private void Button2_Click(object sender, EventArgs e)
 {
     try
     {
         listener.Start();
     }
     catch (Exception)
     {
     }
 }
예제 #5
0
 public void Start()
 {
     if (listener != null)
     {
         listener.Start();
     }
     else
     {
         Console.WriteLine("Initialise the TCPListener first");
     }
 }
예제 #6
0
 public void Start()
 {
     //listener = new TCPListener(12345, 10, 1024, 1024);
     listener.OnRecive = OnRecive;
     thread            = new Thread(() => {
         listener.Start();
         mutex.WaitOne();
     });
     listener.OnCloseToken += new EventHandler <TCPSocketToken>(CloseToken);
     thread.Start();
 }
예제 #7
0
 public bool StartServer()
 {
     try
     {
         Listener = new TCPListener(2211);
         Listener.SocketAccepted += new TCPListener.SocketAcceptedHandler(Listener_SocketAccepted);
         Listener.Start();
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
예제 #8
0
        public void StartTest()
        {
            int         port   = 8001;
            TCPListener server = new TCPListener();
            TCPClient   client = new TCPClient();

            server.Port = port;
            server.Start();
            client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port));
            client.Send(Encoding.UTF8.GetBytes("Hello"));
            bool flag = false;

            server.ReceiveCompleted += (s, e) => { Debug.WriteLine(e.Data);
                                                   flag = true;
                                                   Assert.AreEqual(e.Data, "Hello"); };
            while (!flag)
            {
                ;
            }
        }
        /// <summary>
        ///
        /// </summary>
        static void TcpDemo()
        {
            TCPListener listener = new TCPListener();
            TCPClient   client   = new TCPClient("127.0.0.1");

            listener.Start();

            client.Start();

            client.Send("Hello World");

            for (int i = 0; i < 100; i++)
            {
                client.Send(getRandomCoords());
            }

            Console.ReadKey();

            client.Close();
            listener.Close();
        }
예제 #10
0
파일: FTPServer.cs 프로젝트: iam7roni/FTP
        //Passive Data MODE
        private int PassiveModePort(ref TCPListener clientDataListner)
        {
            Thread oThread = Thread.CurrentThread;

            lock (oThread)
            {
                int  intPort = 0;
                bool done    = true;
                while (done)
                {
                    intPort = Port();
                    try
                    {
                        if (clientDataListner != null)
                        {
                            clientDataListner.Stop();
                        }
                        clientDataListner = new TCPListener(intPort);
                        clientDataListner.Start();
                        //This Port is free
                        done = false;
                    }
                    catch (Exception e)
                    {
                        //done=false;
                    }
                }
                return(intPort);

                /*
                 * strIPAddress = CStr(wscControl.LocalIP)
                 * strSend = "PORT " & Replace(strIPAddress, ".", ",")
                 * strSend = strSend & "," & intPort \ 256 & "," & (intPort Mod 256)
                 * strSend = strSend & vbCrLf
                 * wscControl.SendData strSend
                 */
            }
        }
예제 #11
0
 public void Start()
 {
     Listener.Start();
 }
예제 #12
0
        protected void Reload()
        {
            Encryption.RNG.Reload();
            // some logic in configuration updated the config when saving, we need to read it again
            _config = Configuration.Load();
            Configuration.Process(ref _config);

            NLogConfig.LoadConfiguration();

            logger.Info($"WPF Localization Extension|Current culture: {LocalizeDictionary.CurrentCulture}");

            // set User-Agent for httpClient
            try
            {
                if (!string.IsNullOrWhiteSpace(_config.userAgentString))
                {
                    httpClient.DefaultRequestHeaders.Add("User-Agent", _config.userAgentString);
                }
            }
            catch
            {
                // reset userAgent to default and reapply
                Configuration.ResetUserAgent(_config);
                httpClient.DefaultRequestHeaders.Add("User-Agent", _config.userAgentString);
            }

            privoxyRunner = privoxyRunner ?? new PrivoxyRunner();

            _pacDaemon = _pacDaemon ?? new PACDaemon(_config);
            _pacDaemon.PACFileChanged      += PacDaemon_PACFileChanged;
            _pacDaemon.UserRuleFileChanged += PacDaemon_UserRuleFileChanged;
            _pacServer = _pacServer ?? new PACServer(_pacDaemon);
            _pacServer.UpdatePACURL(_config); // So PACServer works when system proxy disabled.

            GeositeUpdater.ResetEvent();
            GeositeUpdater.UpdateCompleted += PacServer_PACUpdateCompleted;
            GeositeUpdater.Error           += PacServer_PACUpdateError;

            _tcpListener?.Stop();
            _udpListener?.Stop();
            StopPlugins();

            // don't put PrivoxyRunner.Start() before pacServer.Stop()
            // or bind will fail when switching bind address from 0.0.0.0 to 127.0.0.1
            // though UseShellExecute is set to true now
            // http://stackoverflow.com/questions/10235093/socket-doesnt-close-after-application-exits-if-a-launched-process-is-open
            privoxyRunner.Stop();
            try
            {
                var strategy = GetCurrentStrategy();
                strategy?.ReloadServers();

                StartPlugin();
                privoxyRunner.Start(_config);

                TCPRelay tcpRelay = new TCPRelay(this, _config);
                tcpRelay.OnInbound  += UpdateInboundCounter;
                tcpRelay.OnOutbound += UpdateOutboundCounter;
                tcpRelay.OnFailed   += (o, e) => GetCurrentStrategy()?.SetFailure(e.server);

                UDPRelay udpRelay = new UDPRelay(this);
                _tcpListener = new TCPListener(_config, new List <IStreamService>
                {
                    tcpRelay,
                    _pacServer,
                    new PortForwarder(privoxyRunner.RunningPort),
                });
                _tcpListener.Start();
                _udpListener = new UDPListener(_config, new List <IDatagramService>
                {
                    udpRelay,
                });
                _udpListener.Start();
            }
            catch (Exception e)
            {
                // translate Microsoft language into human language
                // i.e. An attempt was made to access a socket in a way forbidden by its access permissions => Port already in use
                if (e is SocketException se)
                {
                    if (se.SocketErrorCode == SocketError.AddressAlreadyInUse)
                    {
                        e = new Exception(I18N.GetString("Port {0} already in use", _config.localPort), e);
                    }
                    else if (se.SocketErrorCode == SocketError.AccessDenied)
                    {
                        e = new Exception(I18N.GetString("Port {0} is reserved by system", _config.localPort), e);
                    }
                }
                logger.LogUsefulException(e);
                ReportError(e);
            }

            ConfigChanged?.Invoke(this, new EventArgs());
            UpdateSystemProxy();
        }