示例#1
0
        public void Start(int port, int messageSize, ApnsNotificationReceivedDelegate received)
        {
            this.Port        = port;
            this.MessageSize = messageSize;
            this.Received    = received;

            // Data buffer for incoming data.
            //byte[] bytes = new Byte[1024];

            // Establish the local endpoint for the socket.
            // The DNS name of the computer
            // running the listener is "host.contoso.com".

            var localEndPoint = new IPEndPoint(IPAddress.Any, port);

            // Create a TCP/IP socket.
            listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(100);


                while (!cancelServerTokenSource.IsCancellationRequested)
                {
                    // Set the event to nonsignaled state.
                    allDone.Reset();

                    try
                    {
                        // Start an asynchronous socket to listen for connections.
                        //Console.WriteLine("Waiting for a connection...");
                        listener.BeginAccept(
                            new AsyncCallback(AcceptCallback),
                            listener);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Listener Failed: " + ex);
                    }

                    allDone.WaitOne();

                    // Wait until a connection is made before continuing.
                    //while (!allDone.WaitOne(1000))
                    //{
                    //if (cancelServerTokenSource.IsCancellationRequested)
                    //break;
                    //}
                }

                waitServer.Set();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            Console.WriteLine("Wrapping up listener...");
        }
示例#2
0
		public void Start(int port, int messageSize, ApnsNotificationReceivedDelegate received)
		{
			this.Port = port;
			this.MessageSize = messageSize;
			this.Received = received;

			// Data buffer for incoming data.
			//byte[] bytes = new Byte[1024];

			// Establish the local endpoint for the socket.
			// The DNS name of the computer
			// running the listener is "host.contoso.com".
			
			var localEndPoint = new IPEndPoint(IPAddress.Any, port);

			// Create a TCP/IP socket.
			listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

			// Bind the socket to the local endpoint and listen for incoming connections.
			try
			{
				listener.Bind(localEndPoint);
				listener.Listen(100);

				
				while (!cancelServerTokenSource.IsCancellationRequested)
				{
					// Set the event to nonsignaled state.
					allDone.Reset();

					try
					{
						// Start an asynchronous socket to listen for connections.
						//Console.WriteLine("Waiting for a connection...");
						listener.BeginAccept(
							new AsyncCallback(AcceptCallback),
							listener);

					}
					catch (Exception ex)
					{
						Console.WriteLine("Listener Failed: " + ex);
						
					}

					allDone.WaitOne();

					// Wait until a connection is made before continuing.
					//while (!allDone.WaitOne(1000))
					//{
					//if (cancelServerTokenSource.IsCancellationRequested)
					//break;
					//}
				}

				waitServer.Set();

			}
			catch (Exception e)
			{
				Console.WriteLine(e.ToString());
			}

			Console.WriteLine("Wrapping up listener...");
		}