Guest User

Untitled

a guest
Jan 24th, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading;
  6. using System.Net;
  7. using System.Net.Sockets;
  8.  
  9. using Abricot.Sessions;
  10.  
  11. namespace Abricot.Network
  12. {
  13.     class ConnectionListener
  14.     {
  15.         private int listenPort;
  16.  
  17.         private Socket connection;
  18.         private Thread mainThread;
  19.  
  20.         private ManualResetEvent notify;
  21.  
  22.         public ConnectionListener(int port)
  23.         {
  24.             this.listenPort = port;
  25.             this.notify = new ManualResetEvent(false);
  26.  
  27.             IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, port);
  28.             this.connection = new Socket(endpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  29.             this.connection.Blocking = false;
  30.             this.connection.Bind(endpoint);
  31.             this.connection.Listen(10);
  32.  
  33.             this.mainThread = new Thread(new ThreadStart(listen));
  34.         }
  35.  
  36.         public void start()
  37.         {
  38.             this.mainThread.Start();
  39.         }
  40.  
  41.         private void listen()
  42.         {
  43.             while (true)
  44.             {
  45.                 notify.Reset();
  46.  
  47.                 this.connection.BeginAccept(new AsyncCallback(onAccept), this.connection);
  48.  
  49.                 notify.WaitOne();
  50.             }
  51.         }
  52.  
  53.         private void onAccept(IAsyncResult result)
  54.         {
  55.             notify.Set();
  56.         }
  57.     }
  58. }
Add Comment
Please, Sign In to add comment