You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
1012 B
47 lines
1012 B
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Net;
|
|
using System.Net.Sockets;
|
|
|
|
namespace KnockKnock.Lib
|
|
{
|
|
internal class Listener
|
|
{
|
|
public delegate void ConnectedEvent(Listener sender, Socket socket);
|
|
public event ConnectedEvent Connected;
|
|
|
|
int port;
|
|
Socket listener;
|
|
|
|
|
|
public Listener(int port)
|
|
{
|
|
this.port = port;
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
|
listener.Bind(new IPEndPoint(IPAddress.Loopback, this.port));
|
|
listener.Listen();
|
|
listener.BeginAccept(this.ListenerAcceptCallback, null);
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
listener.Close();
|
|
listener = null;
|
|
}
|
|
|
|
protected void ListenerAcceptCallback(IAsyncResult result)
|
|
{
|
|
if (listener == null) return;
|
|
Socket client = listener.EndAccept(result);
|
|
listener.BeginAccept(this.ListenerAcceptCallback, null);
|
|
this.Connected(this, client);
|
|
}
|
|
}
|
|
}
|
|
|