コード例 #1
0
        public static ToThreadedTcpListenerInfo ToThreadedTcpListener(this int port, StreamAction handler)
        {
            var ret = new ToThreadedTcpListenerInfo();

            var t = new Thread(
                delegate()
            {
                var r = new TcpListener(IPAddress.Loopback, port);

                ret.Listener = r;

                r.TryStart(
                    delegate
                {
                    while (true)
                    {
                        // http://stackoverflow.com/questions/365370/proper-way-to-stop-tcplistener
                        // +		$exception	{"A blocking operation was interrupted by a call to WSACancelBlockingCall"}	System.Exception {System.Net.Sockets.SocketException}

                        var c = r.AcceptTcpClient();

                        // we wont be able to stop
                        // this loop with current implementation

                        var s = c.GetStream();

                        new Thread(
                            delegate()
                        {
                            handler(s);
                        }
                            )
                        {
                            IsBackground = true,
                        }.Start();
                    }
                }
                    );
            }
                )
            {
                IsBackground = true,
            };

            t.Start();

            ret.Thread = t;

            return(ret);
        }
コード例 #2
0
		public static ToThreadedTcpListenerInfo ToThreadedTcpListener(this int port, StreamAction handler)
		{
			var ret = new ToThreadedTcpListenerInfo();

			var t = new Thread(
				delegate()
				{
					var r = new TcpListener(IPAddress.Loopback, port);

					ret.Listener = r;

					r.TryStart(
						delegate
						{


							while (true)
							{
								// http://stackoverflow.com/questions/365370/proper-way-to-stop-tcplistener
								// +		$exception	{"A blocking operation was interrupted by a call to WSACancelBlockingCall"}	System.Exception {System.Net.Sockets.SocketException}

								var c = r.AcceptTcpClient();

								// we wont be able to stop
								// this loop with current implementation

								var s = c.GetStream();

								new Thread(
									delegate()
									{
										handler(s);
									}
								)
								{
									IsBackground = true,
								}.Start();
							}
						}
					);
				}
			)
			{
				IsBackground = true,
			};

			t.Start();

			ret.Thread = t;

			return ret;
		}