Exemple #1
0
        public void StartListeningToPort(int port)
        {
            new Thread(() =>
            {
                Console.Out.WriteLine(" [TCP] SERVER IS RUNNING");

                var tcpListener = new TcpListenerEx(
                    localaddr: IPAddress.Parse(Localhost),
                    port: TcpServerListeningPort);

                try
                {
                    tcpListener.Start();

                    Console.WriteLine(" [TCP] The local End point is  :" + tcpListener.LocalEndpoint);
                    Console.WriteLine(" [TCP] Waiting for a connection.....");
                    Console.Out.WriteLine();

                    while (true) // is serving continuously
                    {
                        Socket workerTcpSocket = tcpListener.AcceptSocket();

                        Console.WriteLine($" [TCP] Connection accepted from: {{ {workerTcpSocket.RemoteEndPoint} }}");
                        Console.WriteLine($" [TCP] SoketWorker is bound to: {{ {workerTcpSocket.LocalEndPoint} }}");

                        TcpServerWorker.Instance
                        .Init(workerTcpSocket, tcpListener)
                        .StartWorking();

                        //// TODO Unchecked modification
                        //if (tcpListener.Inactive)
                        //{
                        //    tcpListener.Stop();
                        //}
                    }
                }
                catch (Exception e)
                {
                    Console.Out.WriteLine("[TCP] Grave error occured. Searver is dead.");
                    Console.Out.WriteLine($"e = {e.Message}");
                    Debug.WriteLine("[TCP] Grave error occured. Searver is dead.");
                    Debug.WriteLine($"e = {e.Message}");
                    Console.Out.WriteLine("[TCP] PRESS ANY KEY TO QUIT");
                    Console.ReadLine();

                    //throw; // TODO Unchecked modification
                }
                finally
                {
                    if (tcpListener.Active)
                    {
                        tcpListener.Stop();
                    }
                }
            }).Start();
        }
Exemple #2
0
        public void StartListening(int tcpServingPort, Action <Socket> handleRequestAction)
        {
            var tcpListener = new TcpListenerEx(IPAddress.Any, tcpServingPort);

            Console.Out.WriteLine($"TcpListener is active? [ {tcpListener.Active} ]");

            try
            {
                tcpListener.Start();
                Console.Out.WriteLine($"TcpListener is active? [ {tcpListener.Active} ]");
                Console.Out.WriteLine($"Listening at {IPAddress.Any}:{tcpServingPort}");

                Console.WriteLine(" [TCP] The local End point is  :" + tcpListener.LocalEndpoint);
                Console.WriteLine(" [TCP] Waiting for a connection.....\n");

                while (true) // is serving continuously
                {
                    Socket workerSoket = tcpListener.AcceptSocket();

                    Console.WriteLine($" [TCP] Connection accepted from: {{ {workerSoket.RemoteEndPoint} }}");
                    Console.WriteLine($" [TCP] SoketWorker is bound to: {{ {workerSoket.LocalEndPoint} }}");

                    new Thread(() => handleRequestAction(workerSoket)).Start();
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine("[TCP] Grave error occurred. Server is dead.");
                Console.Out.WriteLine($"e = {e.Message}");
                Debug.WriteLine("[TCP] Grave error occurred. Server is dead.");
                Debug.WriteLine($"e = {e.Message}");
                Console.Out.WriteLine("[TCP] PRESS ANY KEY TO QUIT");
                Console.ReadLine();
            }
            finally
            {
                if (tcpListener.Active)
                {
                    tcpListener.Stop();
                }
            }
        }
Exemple #3
0
        private void ReceiveDiscoveryResponseMessages(int discoveryTimeout)
        {
            Thread thread = new Thread(() =>
            {
                //Console.Out.WriteLine("Run the Receive Discovery Response SERVICE");
                Console.Out.WriteLine($"DISCOVERY SERVICE is running [ timeout: {discoveryTimeout} sec. ]");
                IPAddress ipAddress = Dns.GetHostAddresses(Dns.GetHostName()).FirstOrDefault();
#if DEBUG
                Console.Out.WriteLine($"Listening to {IPAddress.Any}:{_discoveryResponsePort}");
#endif

                var tcpListener = new TcpListenerEx(IPAddress.Any, _discoveryResponsePort);

                try
                {
                    tcpListener.Start();
#if DEBUG
                    Console.Out.WriteLine($" [TCP] The socket is active? {tcpListener.Active}");
                    Console.WriteLine(" [TCP] The local End point is  :" + tcpListener.LocalEndpoint);
                    Console.WriteLine(" [TCP] Waiting for a connection.....\n");
#endif

                    TimeSpan timeoutTimeSpan    = TimeSpan.FromSeconds(discoveryTimeout);
                    DateTime listeningStartTime = DateTime.Now;

                    // is serving continuously while timeout isn't reached
                    while (_discoveryIsActive &&
                           DateTime.Now.Subtract(listeningStartTime) < timeoutTimeSpan)
                    {
#if DEBUG
                        Console.Out.WriteLine("Before accepting....");
#endif

                        Socket workerSoket = tcpListener.AcceptSocket();

#if DEBUG
                        Console.WriteLine($" [TCP] Connection accepted from: {{ {workerSoket.RemoteEndPoint} }}");
                        Console.WriteLine($" [TCP] SocketWorker is bound to: {{ {workerSoket.LocalEndPoint} }}");
#endif
                        new Thread(() =>
                        {
#if DEBUG
                            Console.Out.WriteLine(
                                $"[TCP] >> SERVER WORKER IS TALKING TO {workerSoket.RemoteEndPoint}");
#endif

                            if (tcpListener.Inactive)
                            {
#if DEBUG
                                Console.Out.WriteLine("[TCP] >> DISCOVERY LISTENER IS DOWN. Closing connection...");
#endif
                                return;
                            }

                            byte[] buffer = new byte[Common.UnicastBufferSize];

                            int receivedBytes = workerSoket.Receive(buffer);

                            workerSoket.Close();

                            byte[] data = buffer.Take(receivedBytes).ToArray();

                            string xmlData = data.ToUtf8String();

#if DEBUG
                            Console.Out.WriteLine(xmlData);
#endif

                            DiscoveryResponseMessage responseMessage = xmlData
                                                                       .DeserializeTo <DiscoveryResponseMessage>();

#if DEBUG
                            Console.Out.WriteLine(" [TCP]   >> DISCOVERY LISTENER has finished job");
#endif

                            _discoveryResponseMessages.Add(responseMessage);
                        }).Start();
                    }
                }
                catch (Exception)
                {
                    Console.Out.WriteLine("DISCOVERY SERVICE has crashed.");
                }
                finally
                {
                    if (tcpListener.Active)
                    {
                        tcpListener.Stop();
                    }
                }
            });

            thread.Start();
        }