By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,045 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

What is wrong with BeginReceive?

P: 1
Hi all,
I have a problem with .NET sockets...
The code snippet bellow was working fine with .NET 2.0. After I installed .NET 3.0 it didn't work properly.

Expand|Select|Wrap|Line Numbers
  1. private static void BeginReceiving()
  2.         {
  3.             //client is an instance of 'Socket'
  4.             try
  5.             {
  6.                 Monitor.Enter(client);
  7.                 // Create the state object.
  8.                 StateObject state = new StateObject();
  9.  
  10.                 // Begin receiving the data from the remote server.
  11.                 client.BeginReceive(state.buffer, 0, StateObject.BUFFER_SIZE, SocketFlags.None/*0*/,
  12.                     new AsyncCallback(ContinueReceiving), state);
  13.             }
  14.  
  15.             catch (System.Exception ex)
  16.             {
  17.                       //code clipped
  18.             }
  19.             finally
  20.             {
  21.                 if (client != null) Monitor.Exit(client);
  22.             }
  23.         }
  24. //---------------------------------------------------------------------
  25.         private static void ContinueReceiving(IAsyncResult ar)
  26.         {
  27.             try
  28.             {
  29.                 // Retrieve the state object from the asynchronous state object.
  30.                 StateObject state = (StateObject)ar.AsyncState;
  31.  
  32.                 // Read data from the remote device.
  33.                 int bytesRead = client.EndReceive(ar);
  34.                 if (bytesRead > 0)
  35.                 {
  36.                     //
  37.                     // There might be more data, so store the data received so far.
  38.                                state.reveivedData.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));
  39. //ETX is a pre-defined constant -- by it, I know I've received a complete message from the server
  40.                     if (state.reveivedData.ToString().IndexOf(Globals.ETX) > -1)//I reached the end of sent data
  41.                     {
  42.  
  43.                        //start parsing the message
  44.                        //code clipped
  45.  
  46.                     }
  47.                     else //there still more data in the current message received from the server
  48.                     {
  49.                         // Get the rest of the data.
  50. /*THE EXTREME BEHAVIOR HAPPENS HERE */
  51.                         client.BeginReceive(state.buffer, 0, StateObject.BUFFER_SIZE,  SocketFlags.None/*0*/,
  52.                             new AsyncCallback(ContinueReceiving), state);
  53.                     }
  54.                 }
  55.             }
  56.             catch (System.Exception ex)
  57.             {
  58.                 //code clipped
  59.             }
  60.             finally
  61.             {
  62.             }
  63.         }
  64. //--------------------------------------------------------------
  65. // State object for receiving data from server.
  66. public class StateObject
  67. {
  68.     // Size of receive buffer.
  69.     public const int BUFFER_SIZE = 256;
  70.     // Receiving buffer.
  71.     public byte[] buffer = new byte[BUFFER_SIZE];
  72.     // Received data string.
  73.     public StringBuilder reveivedData = new StringBuilder();
  74. }
  75.  
- when the message is smaller the BUFFER_SIZE, it works properly
- The problem happens when the message's length is greater than the BUFFER_SIZE. When I debugged the code I found that the code doesn't work properly when I want to receive more data to complete the message (i.e. I am waiting for the ETX). In this case I send the state object [which contains the data received so far] with the AsyncCallback to appending more data to it.
Unfortunately, reveivedData (which is a StringBuilder member in the StateObject to concatenate data received in it) found empty the next cycle i received data (although it was working properly when I was installing .NET 2.0 only)

-------------------------------------
Any help will be greatly appreciated...
thanks in advance
Jan 28 '08 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.