Exemple #1
0
        static void Main(string[] args)
        {
            Console.CancelKeyPress += Console_CancelKeyPress;
            AssemblyLoadContext.Default.Unloading += Default_Unloading;

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            var confRoot = builder.Build();

            var hostConfig = HostConfiguration.FromConfiguration(confRoot);

            _socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            Console.WriteLine($"Entry point ThreadId: {Thread.CurrentThread.ManagedThreadId}");

            Console.Write($"Waiting for an accepting connection...");
            _socketClient.Connect(new IPEndPoint(IPAddress.Parse(hostConfig.Address), hostConfig.Port));
            Console.WriteLine($"connected");


            string data = null;

            byte[] bytes;

            while (true)
            {
                Console.Write("Say something: ");
                try
                {
                    data  = Console.ReadLine();
                    bytes = Encoding.UTF8.GetBytes(data);

                    using (var sArgs = new SocketAsyncEventArgs())
                    {
                        sArgs.SetBuffer(bytes);
                        _socketClient.SendAsync(sArgs);
                    }
                }
                catch (ArgumentNullException e)
                {
                    Console.WriteLine("Ctrl + C pressed.");
                    return;
                }
            }
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            Console.CancelKeyPress += Console_CancelKeyPress;

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", true, true);

            var confRoot = builder.Build();

            var hostConf = HostConfiguration.FromConfiguration(confRoot);


            _socketListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socketListener.Bind(new IPEndPoint(IPAddress.Parse(hostConf.Address), hostConf.Port));
            _socketListener.Listen(10);
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(
                $"[Thread-{Thread.CurrentThread.ManagedThreadId}][System]: Entry point ThreadId: {Thread.CurrentThread.ManagedThreadId}");
            Console.WriteLine(
                $"[Thread-{Thread.CurrentThread.ManagedThreadId}][System]: Listening interface: {hostConf.Address}:{hostConf.Port}...");
            Console.ResetColor();

            try
            {
                while (true)
                {
                    var clientSocket = _socketListener.Accept();
                    HandleNewConnection(clientSocket);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
            finally
            {
                Console.WriteLine("Server ends");
                _socketListener.Shutdown(SocketShutdown.Both);
            }
        }