?
Solved

Problem with Asynchronous Receive in Sockets

Posted on 2002-07-22
4
Medium Priority
?
246 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 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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