Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Net;
- using System.Net.Sockets;
- using Abricot.Sessions;
- namespace Abricot.Network
- {
- class ConnectionListener
- {
- private int listenPort;
- private Socket connection;
- private Thread mainThread;
- private ManualResetEvent notify;
- public ConnectionListener(int port)
- {
- this.listenPort = port;
- this.notify = new ManualResetEvent(false);
- IPEndPoint endpoint = new IPEndPoint(IPAddress.Any, port);
- this.connection = new Socket(endpoint.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- this.connection.Blocking = false;
- this.connection.Bind(endpoint);
- this.connection.Listen(10);
- this.mainThread = new Thread(new ThreadStart(listen));
- }
- public void start()
- {
- this.mainThread.Start();
- }
- private void listen()
- {
- while (true)
- {
- notify.Reset();
- this.connection.BeginAccept(new AsyncCallback(onAccept), this.connection);
- notify.WaitOne();
- }
- }
- private void onAccept(IAsyncResult result)
- {
- notify.Set();
- }
- }
- }
Add Comment
Please, Sign In to add comment