Solved

Problem with Asynchronous Receive in Sockets

Posted on 2002-07-22
4
243 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

821 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