receiving thread

P

puzzlecracker

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
 
P

puzzlecracker

[...]
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.

I wait on event in order to avoid pointless cycles in the for
loop...Also, I don't want to use Receive because they might no data
coming in, hence i don't want use CPU.

Also, I want to have socket's Read and Write to be concurrent since
they're independent of each other, to some extent, and only share the
actual socket instance.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top