Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem with Asynchronous Receive in Sockets

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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

618 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