PC Review


Reply
Thread Tools Rate Thread

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

 
 
darthghandi@gmail.com
Guest
Posts: n/a
 
      17th Feb 2007
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

 
Reply With Quote
 
 
 
 
Mike Labosh
Guest
Posts: n/a
 
      17th Feb 2007
> 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


 
Reply With Quote
 
darthghandi@gmail.com
Guest
Posts: n/a
 
      17th Feb 2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pass object from model to view in event mp Microsoft C# .NET 4 19th Jan 2011 03:21 PM
How to pass different delegates to a function Marco Segurini Microsoft C# .NET 0 26th Oct 2010 11:14 AM
How to pass object to Process Class Exited() event? =?Utf-8?B?SmVmZg==?= Microsoft Dot NET 3 21st Sep 2007 11:32 PM
pass datatable in event or grab from property or pass in function? Raymond Lewallen Microsoft VB .NET 7 7th Jul 2004 10:59 PM
How do I control or pass data from a webForm object and an html object? Steve Westwood Microsoft C# .NET 1 17th Oct 2003 06:59 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:36 AM.