Reading the UDP datagram

Feb 19 2009 7:07 AM
Dear All,

I have an application which is reading a datagram from the client.

If the bytes to be read is greater that are equal to the byte sent, then it is working fine.

But my requirement is to read the data by 10 bytes at a time or specific bytes configured by the user.

I don't know how to achieve this.

The code I tried was

private void OnReceive(IAsyncResult ar)
        {
            try
            {
                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint epSender = (EndPoint)ipeSender;
 
                int aCount = serverSocket.EndReceive(ar); // serverSocket.EndReceiveFrom(ar, ref epSender);

                Data msgReceived = new Data(byteData);

                string aReadData = System.Text.ASCIIEncoding.ASCII.GetString(byteData, 0, aCount);

                int aPacketEnd = aReadData.LastIndexOf("|");

                if (aPacketEnd == -1)
                {
                    aPacketEnd = aReadData.LastIndexOf('\0');
                }
                if (aPacketEnd != -1)
                {
                    string aValidData = System.String.Empty;
                    string aPacketData = System.String.Empty;

            // Process the data
                    if (myBuffer.Length > 0)
                    {
                        aValidData = System.Text.ASCIIEncoding.ASCII.GetString(myBuffer);
                    }

                    if (aPacketEnd < aCount - 1)
                    {
                        myBuffer = new byte[aCount - (aPacketEnd + 1)];
                        System.Buffer.BlockCopy(byteData, aPacketEnd + 1, myBuffer, 0, aCount - (aPacketEnd + 1));
                    }
                    else
                    {
                        myBuffer = new byte[0];
                    }

                    System.Threading.Thread.Sleep(100);
                    byte[] message =  new byte[10];
            // Frame the Acknowledgement data
                    serverSocket.BeginSendTo(message, 0, message.Length, SocketFlags.None, epSender,
                        new AsyncCallback(OnSend), epSender);
                    byte []byteData = new byte[10];
                    serverSocket.BeginReceiveFrom(byteData , 0, byteData.Length, SocketFlags.None, ref epSender,
                        new AsyncCallback(OnReceive), epSender);
                }
                else
                {
                    byte[] aTempArray = new byte[myBuffer.Length];
                    System.Buffer.BlockCopy(myBuffer, 0, aTempArray, 0, myBuffer.Length);
                    myBuffer = new byte[aTempArray.Length + (aCount)];
                    System.Buffer.BlockCopy(aTempArray, 0, myBuffer, 0, aTempArray.Length);
                    System.Buffer.BlockCopy(byteData, 0, myBuffer, aTempArray.Length, aCount);
                    byteData = new byte[0];

                    byteData = new byte[10];
                    serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
                        new AsyncCallback(OnReceive), epSender);
                }

               
            }
            catch (Exception ex)
            {
                IPEndPoint ipeSender = new IPEndPoint(IPAddress.Any, 0);
                EndPoint epSender = (IPEndPoint)ipeSender;
                byteData = new byte[10];
                serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref epSender,
                        new AsyncCallback(OnReceive), epSender);
            }

        }

As the datagram sent may be very larger too, so that time I have to limit my reading.

Another important point is to send an ACK when the data transfer is completed.  The start and end of the transmission is identified by the character "|".

Can any one help me to get rid of this?

Thanks a lot in advance.

Answers (2)