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


C# TcpListener.Listen方法代码示例

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


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

示例1: NotNullListener

 public static void NotNullListener()
 {
     var t = new TcpListener();
     Assert.Throws<ArgumentNullException>(() => new TcpListener(null));
     Assert.Throws<ArgumentNullException>(() => t.Bind(null));
     Assert.Throws<ArgumentNullException>(() => t.Bind(null as string, 8000));
     Assert.Throws<ArgumentNullException>(() => t.Bind(null as IPAddress, 8000));
     Assert.Throws<ArgumentNullException>(() => t.Listen(null));
     t.Close();
 }
开发者ID:carlokok,项目名称:LibuvSharp,代码行数:10,代码来源:TcpFixture.cs

示例2: OneSideClose

        public void OneSideClose(IPEndPoint ep)
        {
            int close_cb_called = 0;
            int cl_send_cb_called = 0;
            int cl_recv_cb_called = 0;
            int sv_send_cb_called = 0;
            int sv_recv_cb_called = 0;

            var server = new TcpListener();
            server.Bind(ep);
            server.Listen();
            server.Connection += () => {
                var socket = server.Accept();
                socket.Resume();
                socket.Read(Encoding.ASCII, (str) => {
                    sv_recv_cb_called++;
                    Assert.AreEqual("PING", str);
                    socket.Write(Encoding.ASCII, "PONG", (s) => { sv_send_cb_called++; });
                    socket.Close(() => { close_cb_called++; });
                    server.Close(() => { close_cb_called++; });
                });
            };

            Tcp client = new Tcp();
            client.Connect(ep, (_) => {
                client.Read(Encoding.ASCII, (str) => {
                    cl_recv_cb_called++;
                    Assert.AreEqual("PONG", str);
                });

                client.Complete += () => {
                    close_cb_called++;
                };
                client.Resume();
                client.Write(Encoding.ASCII, "PING", (s) => { cl_send_cb_called++; });
            });

            Assert.AreEqual(0, close_cb_called);
            Assert.AreEqual(0, cl_send_cb_called);
            Assert.AreEqual(0, cl_recv_cb_called);
            Assert.AreEqual(0, sv_send_cb_called);
            Assert.AreEqual(0, sv_recv_cb_called);

            Loop.Default.Run();

            Assert.AreEqual(3, close_cb_called);
            Assert.AreEqual(1, cl_send_cb_called);
            Assert.AreEqual(1, cl_recv_cb_called);
            Assert.AreEqual(1, sv_send_cb_called);
            Assert.AreEqual(1, sv_recv_cb_called);

            #if DEBUG
            Assert.AreEqual(1, UV.PointerCount);
            #endif
        }
开发者ID:GamingAtheist,项目名称:LibuvSharp,代码行数:55,代码来源:TcpFixture.cs

示例3: TakenPort

		public void TakenPort()
		{
			TcpListener s1 = new TcpListener();
			TcpListener s2 = new TcpListener();

			s1.Bind(IPAddress.Any, Default.Port);
			s1.Listen();
			s2.Bind(IPAddress.Any, Default.Port);

			Assert.Throws<UVException>(() => s2.Listen());

			s1.Close();
			s2.Close();
		}
开发者ID:lrdcasimir,项目名称:LibuvSharp,代码行数:14,代码来源:TcpFixture.cs

示例4: TakenPort

        public static void TakenPort()
        {
            TcpListener s1 = new TcpListener();
            TcpListener s2 = new TcpListener();

            s1.Bind(IPAddress.Any, Default.Port);
            s1.Listen((_) => {});
            s2.Bind(IPAddress.Any, Default.Port);

            Assert.Throws<SocketException>(() => s2.Listen((_) => {}), "Address already in use");

            s1.Close();
            s2.Close();
        }
开发者ID:carlokok,项目名称:LibuvSharp,代码行数:14,代码来源:TcpFixture.cs

示例5: Stress

        public static void Stress(IPEndPoint ep)
        {
            for (int j = 0; j < 10; j++) {
                int times = 10;

                int close_cb_called = 0;
                int cl_send_cb_called = 0;
                int cl_recv_cb_called = 0;
                int sv_send_cb_called = 0;
                int sv_recv_cb_called = 0;

                var server = new TcpListener();
                server.Bind(ep);
                server.Listen((socket) => {
                    socket.Resume();
                    socket.Read(Encoding.ASCII, (str) => {
                        sv_recv_cb_called++;
                        Assert.AreEqual(Times("PING", times), str);
                        for (int i = 0; i < times; i++) {
                            socket.Write(Encoding.ASCII, "PONG", (s) => { sv_send_cb_called++; });
                        }
                        socket.Close(() => { close_cb_called++; });
                        server.Close(() => { close_cb_called++; });
                    });
                });

                Tcp.Connect(ep, (_, client) => {
                    client.Resume();
                    for (int i = 0; i < times; i++) {
                        client.Write(Encoding.ASCII, "PING", (s) => { cl_send_cb_called++; });
                    }
                    client.Read(Encoding.ASCII, (str) => {
                        cl_recv_cb_called++;
                        Assert.AreEqual(Times("PONG", times), str);
                        client.Close(() => { close_cb_called++; });
                    });
                });

                Assert.AreEqual(0, close_cb_called);
                Assert.AreEqual(0, cl_send_cb_called);
                Assert.AreEqual(0, cl_recv_cb_called);
                Assert.AreEqual(0, sv_send_cb_called);
                Assert.AreEqual(0, sv_recv_cb_called);

                Loop.Default.Run();

                Assert.AreEqual(3, close_cb_called);
                Assert.AreEqual(times, cl_send_cb_called);
                Assert.AreEqual(1, cl_recv_cb_called);
                Assert.AreEqual(times, sv_send_cb_called);
                Assert.AreEqual(1, sv_recv_cb_called);

            #if DEBUG
                Assert.AreEqual(1, UV.PointerCount);
            #endif
            }
        }
开发者ID:carlokok,项目名称:LibuvSharp,代码行数:57,代码来源:TcpFixture.cs

示例6: Simple

        public static void Simple(IPEndPoint ep)
        {
            int close_cb_called = 0;
            int cl_send_cb_called = 0;
            int cl_recv_cb_called = 0;
            int sv_send_cb_called = 0;
            int sv_recv_cb_called = 0;

            var server = new TcpListener();
            server.Bind(ep);
            server.Listen((socket) => {
                socket.Resume();
                socket.Read(Encoding.ASCII, (str) => {
                    sv_recv_cb_called++;
                    Assert.AreEqual("PING", str);
                    socket.Write(Encoding.ASCII, "PONG", (s) => { sv_send_cb_called++; });

                    socket.Close(() => { close_cb_called++; });
                    server.Close(() => { close_cb_called++; });
                });
            });

            Tcp.Connect(Loop.Default, ep, (_, client) => {
                client.Resume();
                client.Write(Encoding.ASCII, "PING", (s) => { cl_send_cb_called++; });
                client.Read(Encoding.ASCII, (str) => {
                    cl_recv_cb_called++;
                    Assert.AreEqual("PONG", str);
                    client.Close(() => { close_cb_called++; });
                });
            });

            Assert.AreEqual(0, close_cb_called);
            Assert.AreEqual(0, cl_send_cb_called);
            Assert.AreEqual(0, cl_recv_cb_called);
            Assert.AreEqual(0, sv_send_cb_called);
            Assert.AreEqual(0, sv_recv_cb_called);

            Loop.Default.Run();

            Assert.AreEqual(3, close_cb_called);
            Assert.AreEqual(1, cl_send_cb_called);
            Assert.AreEqual(1, cl_recv_cb_called);
            Assert.AreEqual(1, sv_send_cb_called);
            Assert.AreEqual(1, sv_recv_cb_called);

            #if DEBUG
            Assert.AreEqual(1, UV.PointerCount);
            #endif
        }
开发者ID:carlokok,项目名称:LibuvSharp,代码行数:50,代码来源:TcpFixture.cs

示例7: PeerAndSockname

        public static void PeerAndSockname()
        {
            Tcp client = null;
            Tcp server = null;

            bool called = false;
            var l = new TcpListener();
            l.Bind(IPAddress.Any, 8000);

            Action check = () => {
                if (client == null || server == null) {
                    return;
                }

                Assert.AreEqual(client.Sockname, server.Peername);
                Assert.AreEqual(client.Peername, server.Sockname);
                Assert.AreEqual(server.Sockname.Port, 8000);

                client.Shutdown();
                server.Shutdown();
                l.Close();

                called = true;
            };

            l.Listen((tcp) => {
                server = tcp;
                check();
            });

            Tcp.Connect("127.0.0.1", 8000, (e, tcp) => {
                client = tcp;
                check();
            });

            Loop.Default.Run();

            Assert.IsTrue(called);
        }
开发者ID:carlokok,项目名称:LibuvSharp,代码行数:39,代码来源:TcpFixture.cs

示例8: RemoteAndLocalAddress

		public void RemoteAndLocalAddress()
		{
			Tcp client = null;
			Tcp server = null;

			bool called = false;
			var l = new TcpListener();
			l.Bind(IPAddress.Any, 8000);

			Action check = () => {
				if (client == null || server == null) {
					return;
				}

				Assert.Equal(client.LocalAddress, server.RemoteAddress);
				Assert.Equal(client.RemoteAddress, server.LocalAddress);
				Assert.Equal(server.LocalAddress.Port, 8000);

				client.Shutdown();
				server.Shutdown();
				l.Close();

				called = true;
			};

			l.Listen();
			l.Connection += () => {
				server = l.Accept();
				check();
			};

			Tcp t = new Tcp();
			t.Connect("127.0.0.1", 8000, (e) => {
				client = t;
				check();
			});

			Loop.Default.Run();

			Assert.True(called);
		}
开发者ID:lrdcasimir,项目名称:LibuvSharp,代码行数:41,代码来源:TcpFixture.cs


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