예제 #1
0
 /// <summary>
 /// Close client connection
 /// </summary>
 public void Close()
 {
     if (UseSSL)
     {
         SecureStream.Close();
     }
     else
     {
         TcpStream.Close();
     }
     Connected = false;
 }
예제 #2
0
        /// <summary>
        /// Disconnects this connection.
        /// </summary>
        public void Disconnect()
        {
            if (_disconnectCalls > 0)
            {
                return;
            }

            _disconnectCalls++;
            _writeDone.WaitOne();

            try
            {
                ClientDisconnected(this, EventArgs.Empty);
            }
            catch
            {
                // ignore
            }

            try
            {
#if !NET452
                RemoteClient.Dispose();
                SecureStream?.Dispose();
                NetworkStream?.Dispose();
#else
                RemoteClient.Close();
                SecureStream?.Close();
                NetworkStream?.Close();
#endif
            }
            catch
            {
                // ignored
            }
            finally
            {
                NetworkStream           = null;
                SecureStream            = null;
                RemoteClient            = null;
                ContinuousReadingThread = null;
            }
        }
예제 #3
0
        private static void Server()
        {
            var log  = new Logger("Server");
            var serv = new TcpListener(IPAddress.Any, 30000);

            serv.Start();
            log += "Started";

            var client = serv.AcceptTcpClient();

            log += "Accepted connection";

            var ss = new SecureStream(client.GetStream(), rsa, false);

            log += "Wrapped in SecureStream";

            ss.Read(log);
            // Test if the ciphertext is different each round (it is not)
            ss.Read(log);
            ss.Send("\nDear client\n" +
                    "\n" +
                    "I have succesfully received your message.\n" +
                    "Note that your message will be lost in the echo." +
                    "\n" +
                    "Best of wishes\n" +
                    "Server\n" +
                    "\n" +
                    "PS: Foobar ;)\n",
                    log);

            // Ensure it doesn't read 'too much' data and it correctly returns
            // the data
            var l = ss.Read(log, 5);

            Debug.Assert(l == 5);
            ss.Read(log);
            ss.ReadAsync(log).AsyncWaitHandle.WaitOne();

            ss.Close();
            log += "Closed SecureStream";
        }
예제 #4
0
        private static void Client()
        {
            var log = new Logger("Client");

            var client = new TcpClient("localhost", 30000);

            log += "Connected";

            var ss = new SecureStream(client.GetStream(), rsa, true);

            log += "Wrapped in SecureStream";

            ss.Send("Hello server!", log);
            ss.Send("Hello server!", log);
            ss.Read(log);
            ss.Send("Okay :) Cya!", log);
            ss.Send("Have a wonderful day!", log);

            ss.Close();
            log += "Closed SecureStream";
        }