471,594 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

can you use delegates to pass a copy of an object when an event happens?

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

Feb 17 '07 #1
2 1711
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
Feb 17 '07 #2
On Feb 17, 10:36 am, "Mike Labosh" <mlabosh_at_hotmail_dot_comwrote:
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

Feb 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by STom | last post: by
15 posts views Thread by kode | last post: by
1 post views Thread by Natalia DeBow | last post: by
14 posts views Thread by Lior Amar | last post: by
8 posts views Thread by Dominique | last post: by
4 posts views Thread by Tim | last post: by
7 posts views Thread by Siegfried Heintze | last post: by
9 posts views Thread by Scott Gifford | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.