473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1791
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: STom | last post by:
I have a C# Winforms app that has 5 Winforms, lets say A through E. A: Data entry. When data is entered here in any field, values are updated on forms C, D, E.(Not B) B: Data entry form. When...
15
by: kode | last post by:
im having problems trying to understand the delegates in c#, does anybody know some link where i can find a good and simple explanation? thanks
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
4
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on...
14
by: Lior Amar | last post by:
Quick question about threads and delegates. I have the following scenario Thread A (CLASSA) spawns Thread B (CLASSB) and passes it a DelegateA to a callback Thread B Invokes a DelegateB...
8
by: Dominique | last post by:
In order to become more familiar with the Model-View-Controller pattern, I have written a demo where each View is a plugin and the plugins are loaded at startup from the plugins directory. When the...
4
by: Tim | last post by:
There are a set of clients who need to be notified of certain events. I have used events and delegates (publisher-Subscriber model) for the notification mechanism. All the clients register with...
7
by: Siegfried Heintze | last post by:
I'm studying the book "Microsoft Visual Basic.NET Language Reference" and I would like some clarify the difference between events and delegates. On page 156 I see a WinForms example of timer that...
9
by: Scott Gifford | last post by:
I have a question about concurrency and delegates. Say I have a class roughly like this: public class Class1 { public delegate void MyEventHandler(); private event MyEventHandler onMyEvent;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.