Пример #1
0
        private static void RunClientInSynchronousMode()
        {
            int timer = 1000;

            byte[] buffer = new byte[1024];
            int    receivedBytes;

            X509Certificate2 certificate = new X509Certificate2("./certs/client.pfx");
            SecureSocket     socket      = SecureSocketFactory.CreateSecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, certificate);

            Console.WriteLine("Connecting to server in {0} seconds", timer / 1000D);
            Thread.Sleep(timer);
            socket.Connect("127.0.0.1", 9999);
            Console.WriteLine("Connected to server");
            while (socket.Connected)
            {
                // Leitura
                Console.ForegroundColor = ConsoleColor.White;
                Console.Write("> ");
                string line = Console.ReadLine();
                socket.Send(Encoding.UTF8.GetBytes(line));

                // Escrita
                Console.ForegroundColor = ConsoleColor.Yellow;
                do
                {
                    receivedBytes = socket.Receive(buffer);
                    byte[] temp = new byte[receivedBytes];
                    Array.Copy(buffer, 0, temp, 0, receivedBytes);
                    Console.WriteLine("< {0}", Encoding.UTF8.GetString(temp));
                }while (receivedBytes < buffer.Length && socket.Available > 0);
                Thread.Sleep(50);
            }
            Console.WriteLine("Disconnected. Finishing...");
        }
Пример #2
0
        static void Main(string[] args)
        {
            byte[] buffer = new byte[1024];
            int    receivedBytes;

            X509Certificate2 certificate = new X509Certificate2("./certs/server.pfx");
            SecureSocket     socket      = SecureSocketFactory.CreateSecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, certificate);

            socket.Bind(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999));
            socket.Listen(10);
            Console.WriteLine("Socket started");
            Console.WriteLine("Waiting connections...");
            while (true)
            {
                var scli = socket.Accept();
                Console.WriteLine("Connection received.");
                while (scli.Connected)
                {
                    Console.WriteLine("Reading data...");
                    do
                    {
                        receivedBytes = scli.Receive(buffer);
                        byte[] temp = new byte[receivedBytes];
                        Array.Copy(buffer, 0, temp, 0, receivedBytes);
                        Console.WriteLine("Data read: \"{0}\"", Encoding.UTF8.GetString(temp));
                        scli.Send(Encoding.UTF8.GetBytes(string.Format("ACK: \"{0}\"", Encoding.UTF8.GetString(temp))));
                    }while (receivedBytes < buffer.Length && scli.Available > 0);

                    Thread.Sleep(50);
                }
                Console.WriteLine("Disconnected. Waiting new connection...");
            }
        }