D
darthghandi
I've created a class to listen to all interfaces and do a
BeginAccept(). Once it gets a connection, it passes the connected
socket off and stores it in a List. Next, it continues to listen for
more incoming connections and does the BeginAccpet() again. It does
an infinite loop this way. My question is: What is the best way to
stop this? I thought about putting a boolean in there, but then what
if it's still waiting for an incoming connection? Can I just close
the socket?
Here is my code for reference:
public class Listener
{
private Socket m_listener;
private ConnectionCallback m_callBack;
public Listener(ConnectionCallback callme)
{
m_callBack = callme;
}
public void AcceptNewConnections()
{
this.AcceptNewConnections(2200);
}
public void AcceptNewConnections(int port)
{
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, port);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
Console.WriteLine("Binding to " +
theEnd.Address.ToString() + " on port " + theEnd.Port.ToString());
m_listener.Listen(20); //TODO: make this
configurable
Console.WriteLine("Now listening");
AsyncCallback callme = new AsyncCallback(AddWorkerSocket);
m_listener.BeginAccept(callme, m_listener);
}
private void AddWorkerSocket(IAsyncResult ar)
{
Console.WriteLine("Got a Connection");
Socket hold = (Socket)ar.AsyncState;
Socket work = hold.EndAccept(ar);
if (work.Connected)
{
Console.WriteLine("Accepted a connection from " +
work.RemoteEndPoint.ToString());
m_callBack(work);
}
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
}
BeginAccept(). Once it gets a connection, it passes the connected
socket off and stores it in a List. Next, it continues to listen for
more incoming connections and does the BeginAccpet() again. It does
an infinite loop this way. My question is: What is the best way to
stop this? I thought about putting a boolean in there, but then what
if it's still waiting for an incoming connection? Can I just close
the socket?
Here is my code for reference:
public class Listener
{
private Socket m_listener;
private ConnectionCallback m_callBack;
public Listener(ConnectionCallback callme)
{
m_callBack = callme;
}
public void AcceptNewConnections()
{
this.AcceptNewConnections(2200);
}
public void AcceptNewConnections(int port)
{
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, port);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
Console.WriteLine("Binding to " +
theEnd.Address.ToString() + " on port " + theEnd.Port.ToString());
m_listener.Listen(20); //TODO: make this
configurable
Console.WriteLine("Now listening");
AsyncCallback callme = new AsyncCallback(AddWorkerSocket);
m_listener.BeginAccept(callme, m_listener);
}
private void AddWorkerSocket(IAsyncResult ar)
{
Console.WriteLine("Got a Connection");
Socket hold = (Socket)ar.AsyncState;
Socket work = hold.EndAccept(ar);
if (work.Connected)
{
Console.WriteLine("Accepted a connection from " +
work.RemoteEndPoint.ToString());
m_callBack(work);
}
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
}