当前位置: 首页>>代码示例>>C#>>正文


C# TcpListener.EndAcceptSocket方法代码示例

本文整理汇总了C#中System.Net.Sockets.TcpListener.EndAcceptSocket方法的典型用法代码示例。如果您正苦于以下问题:C# TcpListener.EndAcceptSocket方法的具体用法?C# TcpListener.EndAcceptSocket怎么用?C# TcpListener.EndAcceptSocket使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.Sockets.TcpListener的用法示例。


在下文中一共展示了TcpListener.EndAcceptSocket方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: CreateConnection

 public static TestConnection CreateConnection()
 {
     var listener = new TcpListener(IPAddress.Loopback, 0);
     listener.Start();
     var ar = listener.BeginAcceptSocket(null, null);
     var s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     s.Connect(IPAddress.Loopback, ((IPEndPoint) listener.LocalEndpoint).Port);
     var server = listener.EndAcceptSocket(ar);
     return new TestConnection {Client = s, Server = server};
 }
开发者ID:2594636985,项目名称:griffin.networking,代码行数:10,代码来源:SocketTestTools.cs

示例2: Main

        static void Main(string[] args)
        {
            GMSKeys.Initialize();

            {
                // Quick test
                ITEMS = new Dictionary<short, ItemEquip>();
                ITEMS.Add(1, new ItemEquip(1113017)
                {
                    StatusFlags = 0x0714,
                    Potential1 = 40356,
                    Potential2 = 30041,
                    Potential3 = 30044,
                    Potential4 = 12011,
                    Potential5 = 2014,
                    Potential6 = 2014,
                    SocketState = 0x00FF,
                    Nebulite2 = 1001,
                    Nebulite1 = 2001,
                    Nebulite3 = 3400,

                });

                //File.WriteAllText("import.xml", ITEMS.Serialize());

                if (File.Exists("import.xml"))
                {
                    ITEMS = File.ReadAllText("import.xml").Deserialize<Dictionary<short, ItemEquip>>();
                }
            }

            if (ITEMS == null)
                ITEMS = new Dictionary<short, ItemEquip>();

            {
                TcpListener listener = new TcpListener(System.Net.IPAddress.Any, 8484);
                listener.Start();

                AsyncCallback EndAccept = null;
                EndAccept = (a) =>
                {
                    new Client(listener.EndAcceptSocket(a));

                    Console.WriteLine("accepted");

                    listener.BeginAcceptSocket(EndAccept, null);
                };

                listener.BeginAcceptSocket(EndAccept, null);
            }



            Console.ReadLine();
        }
开发者ID:diamondo25,项目名称:mapler.me,代码行数:55,代码来源:Program.cs

示例3: clientConnect

 private void clientConnect(IAsyncResult ar)
 {
     try
     {
         listener = (TcpListener)ar.AsyncState;
         client = listener.EndAcceptSocket(ar);
     }
     catch (Exception e) {
         throw e;
     }
 }
开发者ID:aa43a,项目名称:eklink,代码行数:11,代码来源:TCPiaSer.cs

示例4: GetConnectedSocket

        public Socket GetConnectedSocket()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);
            listener.Start();
            IAsyncResult res = listener.BeginAcceptSocket(null, null);
            _socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            _socket.Connect(IPAddress.Loopback, listener.LocalEndpoint.As<IPEndPoint>().Port);
            Socket clientSocket = listener.EndAcceptSocket(res);

            StartRead();
            return clientSocket;
        }
开发者ID:jmptrader,项目名称:griffin,代码行数:12,代码来源:TcpClientHelper.cs

示例5: Create

        public static ClientServerHelper Create()
        {
            var listener = new TcpListener(IPAddress.Loopback, 0);
            listener.Start();
            var ar = listener.BeginAcceptSocket(null, null);

            var helper = new ClientServerHelper();
            helper.Client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            helper.Client.Connect(IPAddress.Loopback, ((IPEndPoint)listener.LocalEndpoint).Port);
            helper.Server = listener.EndAcceptSocket(ar);
            listener.Stop();
            return helper;
        }
开发者ID:GitItInTheHub,项目名称:Griffin.Framework,代码行数:13,代码来源:ClientServerHelper.cs

示例6: Upload

		void Upload ()
		{
			TcpListener listener = new TcpListener (new IPEndPoint (IPAddress.Loopback, 0));
			listener.Start ();
			
			Console.WriteLine ("Listening on: {0}", listener.LocalEndpoint);
			
			uploader = new NativeUploader ();
			uploader.UploadStream ("http://127.0.0.1:" + ((IPEndPoint) listener.LocalEndpoint).Port.ToString (), 1000, () =>
			{
				Console.WriteLine ("Upload completed.");
			});
			
			listener.BeginAcceptSocket ((IAsyncResult res) =>
			{
				ThreadPool.QueueUserWorkItem ((v) => 
				{
					using (var socket = listener.EndAcceptSocket (res)) {
						byte [] buffer = new byte[1024];
						int read;
						
						// receive headers
						read = socket.Receive (buffer);
						BeginInvokeOnMainThread (() => { status.Caption = "Received headers..."; dvc.ReloadData (); });
						Console.WriteLine ("\n" + System.Text.ASCIIEncoding.ASCII.GetString (buffer, 0, read));
						
						// send 100 Continue
						socket.Send (System.Text.ASCIIEncoding.ASCII.GetBytes (@"HTTP/1.1 100 Continue\r\n\r\n"));
						
						// receive data
						read = socket.Receive (buffer);
						BeginInvokeOnMainThread (() => { status.Caption = "Received data"; dvc.ReloadData (); });
						Console.WriteLine ("\n" + System.Text.ASCIIEncoding.ASCII.GetString (buffer, 0, read));
						
						// send 200 OK
						socket.Send (System.Text.ASCIIEncoding.ASCII.GetBytes (@"HTTP/1.1 200 OK\r\n\r\n"));
					}
					
					listener.Stop ();
				});
			}, null);
		}
开发者ID:BoogieMAN2K,项目名称:monotouch-samples,代码行数:42,代码来源:AppDelegate.cs

示例7: ServerLoop

        public static void ServerLoop()
        {
            AcceptDone = new ManualResetEvent(false);
            Clients = new List<GhostClient>();

            int PORT = ServerConstants.Game_Port;

            try
            {
                TcpListener Listener = new TcpListener(IPAddress.Any, PORT);
                Listener.Start();
                Log.Success("角色伺服器 {0} 已經上線。", Listener.LocalEndpoint);
                IsAlive = true;

                while (IsAlive)
                {
                    AcceptDone.Reset();
                    Listener.BeginAcceptSocket((iar) =>
                    {
                        new GhostClient(Listener.EndAcceptSocket(iar), ServerType.GameServer);
                        AcceptDone.Set();
                    }, null);
                    AcceptDone.WaitOne();
                }

                Listener.Stop();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                Console.Read();
            }
        }
开发者ID:york8817612,项目名称:GhostServer,代码行数:37,代码来源:GameServer.cs

示例8: Test_ConnectionManager

        public void Test_ConnectionManager()
        {
            for (int i = 0; i < 4; i++)
            {
                TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));
                listener.Start();
                var listenerAcceptSocket = listener.BeginAcceptSocket(null, null);

                TcpClient client = new TcpClient();
                client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));

                var server = listener.EndAcceptSocket(listenerAcceptSocket);
                listener.Stop();

                var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager);
                var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager);

                List<ConnectionManager> connectionManagers = new List<ConnectionManager>();

                {
                    ConnectionManager serverConnectionManager;
                    ConnectionManager clientConnectionManager;

                    Node serverNode = null;
                    Node clientNode = null;

                    byte[] serverSessionId = null;
                    byte[] clientSessionId = null;

                    {
                        var id = new byte[32];
                        _random.NextBytes(id);
                        var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" };

                        serverNode = new Node(id, uris);
                    }

                    {
                        var id = new byte[32];
                        _random.NextBytes(id);
                        var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" };

                        clientNode = new Node(id, uris);
                    }

                    {
                        serverSessionId = new byte[32];
                        _random.NextBytes(serverSessionId);
                    }

                    {
                        clientSessionId = new byte[32];
                        _random.NextBytes(clientSessionId);
                    }

                    serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager);
                    clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager);

                    Thread serverThread = new Thread(new ThreadStart(() =>
                    {
                        serverConnectionManager.Connect();
                    }));

                    Thread clientThread = new Thread(new ThreadStart(() =>
                    {
                        clientConnectionManager.Connect();
                    }));

                    serverThread.Start();
                    clientThread.Start();

                    serverThread.Join();
                    clientThread.Join();

                    Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1");
                    Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2");

                    Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1");
                    Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2");

                    connectionManagers.Add(serverConnectionManager);
                    connectionManagers.Add(clientConnectionManager);
                }

                connectionManagers.Randomize();

                {
                    var queue = new WaitQueue<PullNodesEventArgs>();

                    var receiverConnection = connectionManagers[0];
                    var senderConnection = connectionManagers[1];

                    receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) =>
                    {
                        queue.Enqueue(e);
                    };

                    List<Node> nodes = new List<Node>();

                    for (int j = 0; j < 32; j++)
//.........这里部分代码省略.........
开发者ID:networkelements,项目名称:Library,代码行数:101,代码来源:Test_Library_Net_Amoeba.cs

示例9: Test_ConnectionManager

        public void Test_ConnectionManager()
        {
            for (int i = 0; i < 4; i++)
            {
                TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));
                listener.Start();
                var listenerAcceptSocket = listener.BeginAcceptSocket(null, null);

                TcpClient client = new TcpClient();
                client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));

                var server = listener.EndAcceptSocket(listenerAcceptSocket);
                listener.Stop();

                var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager);
                var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Amoeba.MaxReceiveCount, _bufferManager);

                List<ConnectionManager> connectionManagers = new List<ConnectionManager>();

                {
                    ConnectionManager serverConnectionManager;
                    ConnectionManager clientConnectionManager;

                    Node serverNode = null;
                    Node clientNode = null;

                    byte[] serverSessionId = null;
                    byte[] clientSessionId = null;

                    {
                        var id = new byte[32];
                        _random.NextBytes(id);
                        var uris = new string[] { "tcp:localhost:9000", "tcp:localhost:9001", "tcp:localhost:9002" };

                        serverNode = new Node(id, uris);
                    }

                    {
                        var id = new byte[32];
                        _random.NextBytes(id);
                        var uris = new string[] { "tcp:localhost:9000", "tcp:localhost:9001", "tcp:localhost:9002" };

                        clientNode = new Node(id, uris);
                    }

                    {
                        serverSessionId = new byte[32];
                        _random.NextBytes(serverSessionId);
                    }

                    {
                        clientSessionId = new byte[32];
                        _random.NextBytes(clientSessionId);
                    }

                    serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager);
                    clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager);

                    var serverTask = Task.Run(() => serverConnectionManager.Connect());
                    var clientTask = Task.Run(() => clientConnectionManager.Connect());

                    Task.WaitAll(serverTask, clientTask);

                    Assert.IsTrue(CollectionUtils.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1");
                    Assert.IsTrue(CollectionUtils.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2");

                    Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1");
                    Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2");

                    connectionManagers.Add(serverConnectionManager);
                    connectionManagers.Add(clientConnectionManager);
                }

                connectionManagers.Randomize();

                {
                    var queue = new WaitQueue<PullNodesEventArgs>();

                    var receiverConnection = connectionManagers[0];
                    var senderConnection = connectionManagers[1];

                    receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) =>
                    {
                        queue.Enqueue(e);
                    };

                    List<Node> nodes = new List<Node>();

                    for (int j = 0; j < 32; j++)
                    {
                        Node node = null;

                        {
                            var id = new byte[32];
                            _random.NextBytes(id);
                            var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" };

                            node = new Node(id, uris);
                        }

//.........这里部分代码省略.........
开发者ID:Alliance-Network,项目名称:Library,代码行数:101,代码来源:Test_Library_Net_Amoeba.cs

示例10: Run

        public void Run(Action<Request> handler)
        {
            var listener = new TcpListener (EndPoint);
            listener.Start ();
            listener.BeginAcceptSocket (ar => {
                Socket = listener.EndAcceptSocket (ar);
                Log ("GOT SOCKET");

                Stream = new NetworkStream (Socket);

                Reader = new StreamReader (Stream);
                Writer = new StreamWriter (Stream);

                while (true) {
                    var request = new Request (this);
                    handler (request);
                }
            }, null);
        }
开发者ID:baulig,项目名称:Provcon-Faust,代码行数:19,代码来源:Server.cs

示例11: Test_ConnectionManager

        public void Test_ConnectionManager()
        {
            for (int i = 0; i < 4; i++)
            {
                TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));
                listener.Start();
                var listenerAcceptSocket = listener.BeginAcceptSocket(null, null);

                TcpClient client = new TcpClient();
                client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));

                var server = listener.EndAcceptSocket(listenerAcceptSocket);
                listener.Stop();

                var tcpClient = new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager);
                var tcpServer = new BaseConnection(new SocketCap(server), null, Test_Library_Net_Outopos.MaxReceiveCount, _bufferManager);

                List<ConnectionManager> connectionManagers = new List<ConnectionManager>();

                {
                    ConnectionManager serverConnectionManager;
                    ConnectionManager clientConnectionManager;

                    Node serverNode = null;
                    Node clientNode = null;

                    byte[] serverSessionId = null;
                    byte[] clientSessionId = null;

                    {
                        var id = new byte[32];
                        _random.NextBytes(id);
                        var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" };

                        serverNode = new Node(id, uris);
                    }

                    {
                        var id = new byte[32];
                        _random.NextBytes(id);
                        var uris = new string[] { "net.tcp://localhost:9000", "net.tcp://localhost:9001", "net.tcp://localhost:9002" };

                        clientNode = new Node(id, uris);
                    }

                    {
                        serverSessionId = new byte[32];
                        _random.NextBytes(serverSessionId);
                    }

                    {
                        clientSessionId = new byte[32];
                        _random.NextBytes(clientSessionId);
                    }

                    serverConnectionManager = new ConnectionManager(tcpServer, serverSessionId, serverNode, ConnectDirection.In, _bufferManager);
                    clientConnectionManager = new ConnectionManager(tcpClient, clientSessionId, clientNode, ConnectDirection.Out, _bufferManager);

                    Thread serverThread = new Thread(new ThreadStart(() =>
                    {
                        serverConnectionManager.Connect();
                    }));

                    Thread clientThread = new Thread(new ThreadStart(() =>
                    {
                        clientConnectionManager.Connect();
                    }));

                    serverThread.Start();
                    clientThread.Start();

                    serverThread.Join();
                    clientThread.Join();

                    Assert.IsTrue(CollectionUtilities.Equals(serverConnectionManager.SesstionId, clientSessionId), "ConnectionManager SessionId #1");
                    Assert.IsTrue(CollectionUtilities.Equals(clientConnectionManager.SesstionId, serverSessionId), "ConnectionManager SessionId #2");

                    Assert.AreEqual(serverConnectionManager.Node, clientNode, "ConnectionManager Node #1");
                    Assert.AreEqual(clientConnectionManager.Node, serverNode, "ConnectionManager Node #2");

                    connectionManagers.Add(serverConnectionManager);
                    connectionManagers.Add(clientConnectionManager);
                }

                connectionManagers.Randomize();

                {
                    var queue = new WaitQueue<PullNodesEventArgs>();

                    var receiverConnection = connectionManagers[0];
                    var senderConnection = connectionManagers[1];

                    receiverConnection.PullNodesEvent += (object sender, PullNodesEventArgs e) =>
                    {
                        queue.Enqueue(e);
                    };

                    List<Node> nodes = new List<Node>();

                    for (int j = 0; j < 32; j++)
//.........这里部分代码省略.........
开发者ID:networkelements,项目名称:Library,代码行数:101,代码来源:Test_Library_Net_Outopos.cs

示例12: Test_CompressConnection

        public void Test_CompressConnection()
        {
            TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));
            listener.Start();
            var listenerAcceptSocket = listener.BeginAcceptSocket(null, null);

            TcpClient client = new TcpClient();
            client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));

            var server = listener.EndAcceptSocket(listenerAcceptSocket);
            listener.Stop();

            using (var compressClient = new CompressConnection(new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Connection.MaxReceiveCount, _bufferManager), Test_Library_Net_Connection.MaxReceiveCount, _bufferManager))
            using (var compressServer = new CompressConnection(new BaseConnection(new SocketCap(server), null, Test_Library_Net_Connection.MaxReceiveCount, _bufferManager), Test_Library_Net_Connection.MaxReceiveCount, _bufferManager))
            {
                var clientConnectTask = compressClient.ConnectAsync(new TimeSpan(0, 0, 20));
                var serverConnectTask = compressServer.ConnectAsync(new TimeSpan(0, 0, 20));

                Task.WaitAll(clientConnectTask, serverConnectTask);

                using (MemoryStream stream = new MemoryStream())
                {
                    var buffer = new byte[1024 * 8];
                    _random.NextBytes(buffer);

                    stream.Write(buffer, 0, buffer.Length);
                    stream.Seek(0, SeekOrigin.Begin);

                    var clientSendTask = compressClient.SendAsync(stream, new TimeSpan(0, 0, 20));
                    var serverReceiveTask = compressServer.ReceiveAsync(new TimeSpan(0, 0, 20));

                    Task.WaitAll(clientConnectTask, serverReceiveTask);

                    using (var returnStream = serverReceiveTask.Result)
                    {
                        var buff2 = new byte[(int)returnStream.Length];
                        returnStream.Read(buff2, 0, buff2.Length);

                        Assert.IsTrue(CollectionUtils.Equals(buffer, buff2), "CompressConnection #1");
                    }
                }

                using (MemoryStream stream = new MemoryStream())
                {
                    var buffer = new byte[1024 * 8];
                    _random.NextBytes(buffer);

                    stream.Write(buffer, 0, buffer.Length);
                    stream.Seek(0, SeekOrigin.Begin);

                    var serverSendTask = compressServer.SendAsync(stream, new TimeSpan(0, 0, 20));
                    var clientReceiveTask = compressClient.ReceiveAsync(new TimeSpan(0, 0, 20));

                    Task.WaitAll(serverSendTask, clientReceiveTask);

                    using (var returnStream = clientReceiveTask.Result)
                    {
                        var buff2 = new byte[(int)returnStream.Length];
                        returnStream.Read(buff2, 0, buff2.Length);

                        Assert.IsTrue(CollectionUtils.Equals(buffer, buff2), "CompressConnection #2");
                    }
                }
            }

            client.Close();
            server.Close();
        }
开发者ID:Alliance-Network,项目名称:Library,代码行数:68,代码来源:Test_Library_Net_Connection.cs

示例13: Test_SecureConnection

        public void Test_SecureConnection()
        {
            for (int i = 0; i < 8; i++)
            {
                TcpListener listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));
                listener.Start();
                var listenerAcceptSocket = listener.BeginAcceptSocket(null, null);

                TcpClient client = new TcpClient();
                client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 60000));

                var server = listener.EndAcceptSocket(listenerAcceptSocket);
                listener.Stop();

                DigitalSignature clientDigitalSignature = null;
                DigitalSignature serverDigitalSignature = null;

                if (_random.Next(0, 100) < 50)
                {
                    clientDigitalSignature = new DigitalSignature("NickName1", DigitalSignatureAlgorithm.Rsa2048_Sha256);
                }

                if (_random.Next(0, 100) < 50)
                {
                    serverDigitalSignature = new DigitalSignature("NickName2", DigitalSignatureAlgorithm.Rsa2048_Sha256);
                }

                SecureConnectionVersion clientVersion;
                SecureConnectionVersion serverVersion;

                {
                    clientVersion = SecureConnectionVersion.Version3;
                    serverVersion = SecureConnectionVersion.Version3;
                }

                {
                    //SecureConnectionVersion clientVersion = 0;
                    //SecureConnectionVersion serverVersion = 0;

                    //for (; ; )
                    //{
                    //    switch (_random.Next(0, 3))
                    //    {
                    //        case 0:
                    //            clientVersion = SecureConnectionVersion.Version2;
                    //            break;
                    //        case 1:
                    //            clientVersion = SecureConnectionVersion.Version3;
                    //            break;
                    //        case 2:
                    //            clientVersion = SecureConnectionVersion.Version2 | SecureConnectionVersion.Version3;
                    //            break;
                    //    }

                    //    switch (_random.Next(0, 3))
                    //    {
                    //        case 0:
                    //            serverVersion = SecureConnectionVersion.Version2;
                    //            break;
                    //        case 1:
                    //            serverVersion = SecureConnectionVersion.Version3;
                    //            break;
                    //        case 2:
                    //            serverVersion = SecureConnectionVersion.Version2 | SecureConnectionVersion.Version3;
                    //            break;
                    //    }

                    //    if ((clientVersion & serverVersion) != 0) break;
                    //}
                }

                //var TcpClient = new BaseConnection(client.Client, Test_Library_Net_Connection.MaxReceiveCount, _bufferManager);
                using (var secureClient = new SecureConnection(clientVersion, SecureConnectionType.Connect, new BaseConnection(new SocketCap(client.Client), null, Test_Library_Net_Connection.MaxReceiveCount, _bufferManager), clientDigitalSignature, _bufferManager))
                using (var secureServer = new SecureConnection(serverVersion, SecureConnectionType.Accept, new BaseConnection(new SocketCap(server), null, Test_Library_Net_Connection.MaxReceiveCount, _bufferManager), serverDigitalSignature, _bufferManager))
                {
                    try
                    {
                        var clientConnectTask = secureClient.ConnectAsync(new TimeSpan(0, 0, 30));
                        var serverConnectTask = secureServer.ConnectAsync(new TimeSpan(0, 0, 30));

                        Task.WaitAll(clientConnectTask, serverConnectTask);

                        if (clientDigitalSignature != null)
                        {
                            if (secureServer.Certificate.ToString() != clientDigitalSignature.ToString()) throw new Exception();
                        }

                        if (serverDigitalSignature != null)
                        {
                            if (secureClient.Certificate.ToString() != serverDigitalSignature.ToString()) throw new Exception();
                        }

                        using (MemoryStream stream = new MemoryStream())
                        {
                            var buffer = new byte[1024 * 8];
                            _random.NextBytes(buffer);

                            stream.Write(buffer, 0, buffer.Length);
                            stream.Seek(0, SeekOrigin.Begin);

//.........这里部分代码省略.........
开发者ID:Alliance-Network,项目名称:Library,代码行数:101,代码来源:Test_Library_Net_Connection.cs

示例14: RunWebApplication

        public static int RunWebApplication(StartOptions options)
        {
            var redirectEvent = new ManualResetEvent(false);
            var terminateEvent = new ManualResetEvent(false);
            int connections = 0;

            bool[] closed = { false };
            var listener = new TcpListener(IPAddress.Loopback, options.Port.Value);
            listener.Start();

            AsyncCallback onAccept = null;
            onAccept = ar =>
            {
                try
                {
                    Interlocked.Increment(ref connections);
                    if (closed[0])
                    {
                        return;
                    }
                    Socket socket = listener.EndAcceptSocket(ar);
                    Console.Write("+");
                    listener.BeginAcceptSocket(onAccept, null);
                    redirectEvent.WaitOne();

                    socket.Send(Encoding.UTF8.GetBytes(string.Format(@"HTTP/1.0 301 Awful Hack
Location: http://localhost:{0}/
Pragma: no-cache
Cache-Control: private
Connection: close

", options.Port.Value)));
                    while (socket.Receive(new byte[1024]) != 0)
                    {
                        // waiting for disconnect
                    }
                    socket.Close();
                }
                catch (Exception)
                {
                    // Console.WriteLine(ex.Message);
                }
            };
            listener.BeginAcceptSocket(onAccept, null);

            DateTime patience = DateTime.UtcNow.AddSeconds(45);
            Console.Write("Waiting for debugger");
            while ((!Debugger.IsAttached || connections < 2) &&
                patience > DateTime.UtcNow)
            {
                Console.Write(".");
                Thread.Sleep(750);
            }
            Console.WriteLine(Debugger.IsAttached ? "!" : "?");
            var timer = new System.Timers.Timer(750);
            timer.Elapsed += (a, b) =>
            {
                if (!Debugger.IsAttached)
                {
                    terminateEvent.Set();
                }
            };
            if (Debugger.IsAttached)
            {
                timer.Start();
            }

            string boot;
            if (!options.Settings.TryGetValue("boot", out boot)
                || string.IsNullOrWhiteSpace(boot))
            {
                options.Settings["boot"] = "Domain";
            }

            string verbosity;
            if (!options.Settings.TryGetValue("traceverbosity", out verbosity)
                || string.IsNullOrWhiteSpace(verbosity))
            {
                options.Settings["traceverbosity"] = "1";
            }

            string directory;
            if (options.Settings.TryGetValue("directory", out directory) &&
                !string.IsNullOrWhiteSpace(directory))
            {
                // TODO: remove 
                Directory.SetCurrentDirectory(directory);
            }

            closed[0] = true;
            listener.Stop();
            using (WebApp.Start(options))
            {
                ThreadPool.QueueUserWorkItem(_ =>
                {
                    Console.ReadLine();
                    terminateEvent.Set();
                });
                redirectEvent.Set();
                terminateEvent.WaitOne();
//.........这里部分代码省略.........
开发者ID:tkggand,项目名称:katana,代码行数:101,代码来源:Program.cs


注:本文中的System.Net.Sockets.TcpListener.EndAcceptSocket方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。