Solved

Problem with Asynchronous Receive in Sockets

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now