D
darthghandi
I am creating a listener class that listens for incoming connections.
When I get an incoming connection, I want to signal an event to
happen and pass that connected socket to a different thread for the
real work. I want to continue to listen for more connections. So is
the socket I pass with the delegate a reference or a value? I thought
this might work:
public delegate void AcceptConnection(Socket sock);
public class Listener
{
private Socket m_listener;
public event AcceptConnection ConnectionReceived;
public Listener()
{
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any,
12345);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}
public Listener(int port)
{
m_hasSocketsReady = false;
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, port);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}
public void AcceptNewConnections()
{
AsyncCallback callme = new AsyncCallback(AddWorkerSocket);
m_listener.BeginAccept(callme, m_listener);
}
public void AddWorkerSocket(IAsyncResult ar)
{
Socket hold = (Socket)ar.AsyncState;
OnConnectionReceived(hold);
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
public void OnConnectionReceived(Socket ret)
{
//ret = m_workers.Dequeue();
if (ConnectionReceived != null)
{
ConnectionReceived(ret);
}
}
}
Is there a better way? Am I going about this the wrong way?
Thanks for your time,
Chris
When I get an incoming connection, I want to signal an event to
happen and pass that connected socket to a different thread for the
real work. I want to continue to listen for more connections. So is
the socket I pass with the delegate a reference or a value? I thought
this might work:
public delegate void AcceptConnection(Socket sock);
public class Listener
{
private Socket m_listener;
public event AcceptConnection ConnectionReceived;
public Listener()
{
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any,
12345);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}
public Listener(int port)
{
m_hasSocketsReady = false;
IPEndPoint theEnd = new IPEndPoint(IPAddress.Any, port);
m_listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
m_listener.Bind(theEnd);
m_listener.Listen(20);
}
public void AcceptNewConnections()
{
AsyncCallback callme = new AsyncCallback(AddWorkerSocket);
m_listener.BeginAccept(callme, m_listener);
}
public void AddWorkerSocket(IAsyncResult ar)
{
Socket hold = (Socket)ar.AsyncState;
OnConnectionReceived(hold);
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
public void OnConnectionReceived(Socket ret)
{
//ret = m_workers.Dequeue();
if (ConnectionReceived != null)
{
ConnectionReceived(ret);
}
}
}
Is there a better way? Am I going about this the wrong way?
Thanks for your time,
Chris