473,395 Members | 1,460 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,395 software developers and data experts.

receiving thread

Problem:

An Application server send messages to the client (and I am
developing the client) based on the client request:

client [Do something]=====================================>server
client <============== [response with message in the
buffer[1024]] server
client <============== [response with message in the
buffer[1024]] server
client [Do something]=====================================>server
client [Do something]=====================================>server
client <============== [response with message in the
buffer[1024]] server
As you can see, server doesn't have to send just one message back,
though all messages are equal in size.

So what I am trying to do is to create a separate thread that will
always listen to the responses from the server and then delegate the
response to appropriate handler. However, I don't want thread to
constantly run in the while(true) but only be awaken when a response
actually arrives. Here is my thread function thus far:

private void ThreadReceiverProc()
{

while (IsTermnaeteReceiver = false)
{
lock (connLock)
{
try
{
client.BeginReceive(buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
receiveDone.WaitOne()
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}

Is this correct approach from the design and efficiency point of view?

I would all the tips, for I am a noob to csharp
Thanks
Oct 21 '08 #1
1 1246
On Tue, 21 Oct 2008 12:43:36 -0700, puzzlecracker <ir*********@gmail.com>
wrote:
[...]
private void ThreadReceiverProc()
{

while (IsTermnaeteReceiver = false)
{
lock (connLock)
{
try
{
client.BeginReceive(buffer, 0,
StateObject.BufferSize, 0,
new AsyncCallback(ReceiveCallback), state);
receiveDone.WaitOne()
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}

Is this correct approach from the design and efficiency point of view?
No. First, if you want to loop, don't call the asynchronous method. Just
call the blocking Receive() method. Second, calling the asynchronous
BeginReceive() method and then waiting on an event handle until the
receive has completed is particularly pointless.

If you have only a small number of connections (no more than a few
hundred, for example), dedicating one thread to each connection and
calling the blocking Receive() method should be fine. If you want to
scale up to a much larger number of connections, you should use the
asynchronous BeginReceive() method (and the other asynchronous Begin...()
methods too), and in that case there is no point to having a specific
thread for each connection at all. Just call the asynchronous method from
whatever thread is appropriate and convenient and leave it at that.

Pete
Oct 21 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Stephan Steiner | last post by:
Hi I have a small program listening to UDP broadcast datagrams that are periodically sent out. It will stop listening for a certain period if either a sufficient number of packets has been...
4
by: Stephan Steiner | last post by:
Hi I'm having some weird threading issues.. almost at random, if I dare change a line of my code, the shutdown sequence gets messed up. I'm using a thread to receive data from the network, that...
2
by: Terry | last post by:
I've got a strange problem receiving multicast packets in a C# application. What's strange is that it works *sometimes* but not always. I create a socket, call bind(), set the multicast socket...
0
by: Erwin Hill | last post by:
Hi guys, I have a weird problem. I wrote an application that has 2 UI threads both windows are top-level. When I insert my USB disk on key into computer I receive several WM_DEVICECHANGE which...
5
by: LS | last post by:
Can a WebMethod return an Interface type? Can we pass an interface parameter ? Example : public interface IEntity { long Id { get; set; } string Name { get; set; } }
1
by: greenxiar | last post by:
My code is below and the platform is Win2K3 R2: using System; using System.Net; using System.Net.Sockets; using System.Threading; namespace TechUDPBroadcast { class Program { static void...
1
by: Subrato | last post by:
Hi, I am very new to xml and I have this piece of code which I took off a website. The situation is that on of the website, user files up a form and it is submitted. On submission, the page should...
0
by: zelov | last post by:
Hi, I am creating an interface which contains the listbox(C# .net micro framework). When the listbox item is seletected, it will send a character to the serial port and show a "send command"...
3
by: Sarah | last post by:
Hi - Please be gentle. I am quite new to visual basic, but I have been going through tutorials and reading up. I found a code snippet on the internet that I wanted to see if I could re-purpose...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.