Solved

Problem with Asynchronous Receive in Sockets

Posted on 2002-07-22
4
245 Views
Last Modified: 2012-08-14
Hi I have a problem with the code I wrote below. I wrote the AsyncCallback to repeat calling the ReceiveData method whenever there is more data to receive. However whenthere is no more data to receive from the client, it doesn't attempt to call the ReceiveData method, instead it just halts and wait for more incoming data. Does anyone know what is wrong with my code or if there is a better solution in making sure I have read all the data from the client.

CODE:
public void ReceiveData(IAsyncResult ar)
{
  ........
  int read = aSocket.EndReceive(ar);
  if (read > 0)
  {
    aStringB.Append(Encoding.ASCII.GetString(aBuffer,0,read));
    IAsyncResult ias = temp.BeginReceive(aBuffer,0,aBuffer.Length,0,new AsyncCallback(ReceiveData),so);
  }
  else
  {
    if(aStringB.Length>1)
    {
      ProcessMessage(so.sb.ToString(), so.workingSocket);
    }
                   
  }
}
0
Comment
Question by:denz_1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 7170382
have you defined a protocol?

eg the interaction between you and the client.

a lot of people prepend the data with an int that says now many bytes are to follow.

what you do at the receiving end is read the int first and then read the number of bytes it says are to follow. once you have read all your bytes you have your first message and then you read the next int and the number of bytes it says are to follow.
0
 

Author Comment

by:denz_1
ID: 7171680
Hi ShaunWilde
i am actually using the socket in conjunction with the TCPListener class.. I actually took the code from the sample in the documentation. I am not quite sure why it does not work. What I am trying to do is receive data from a client using TCP. Do you have any sample code that you can give to me since this one is taken from the documentation and does not work.

Thanks in advance.
0
 

Author Comment

by:denz_1
ID: 7171687
Also. The number of bytes the client send is unpredictable. And I actually did read the number of int the message is to follow and keep reading it until there is no more message.

The problem with the code is that when there is no more data to read, it waits for incoming data from the client which is not what it is supposed to do. It is just supposed to return a 0 bytes when I call the EndReceive.

I am lost.
0
 
LVL 9

Accepted Solution

by:
ShaunWilde earned 100 total points
ID: 7172745
hi denz - I haven't got the time today to create a sample for you however to get you going you may find this useful

http://www.codeproject.com/csharp/TestingSocketServers.asp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question