On Feb 17, 10:36 am, "Mike Labosh" <mlabosh_at_hotmail_dot_com> wrote:
> > 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?
>
> Since the Socket is an instance of a class, it is always a reference. NOT a
> copy.
>
> If you want to get a copy into a second reference, you could use the
> MemberwiseClone() method.
> --
>
> Peace & happy computing,
>
> Mike Labosh, MCSD MCT
> Owner, vbSensei.Com
>
> "Escriba coda ergo sum." -- vbSensei
After looking more at the Socket Class I noticed that the EndReceive()
method returns a new Socket that handles the remote communication. So
I modified the method like so:
public void AddWorkerSocket(IAsyncResult ar)
{
Socket hold = (Socket)ar.AsyncState;
Socket work = hold.EndAccept(ar);
OnConnectionReceived(work);
AsyncCallback callMe = new AsyncCallback(AddWorkerSocket);
hold.BeginAccept(callMe, hold);
}
So from what I understand, that new socket should be passed to any
subscribers of the event. Is that correct? When I do the following
in another class, I run into a problem:
private void button6_Click(object sender, EventArgs e)
{
Listener listen = new Listener();
listen.ConnectionReceived += new
AcceptConnection(AcceptComplete);
listen.AcceptNewConnections();
}
private void AcceptComplete(Socket returnSock)
{
if (returnSock.Connected)
{
//now connected
DoWork();
}
}
I never get that Socket in the AcceptComplete() method. What am I
doing wrong?
Thanks for your time,
Chris
|