Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
  string[] splitString = client.Client.LocalEndPoint.ToString().Split(':'); 
            IPEndPoint local = new IPEndPoint(IPAddress.Parse(splitString[0]),Int32.Parse(splitString[1]));
            receiveUDPClient = new UdpClient(local);
            IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0);
            int recAllLength = 0;
            while (true)
            {
              
                try
                {
                    byte[] receiveBytes= new Byte[1024 * 1024 * 2];
                   
                 byte[] arr=new byte[1024*5];
               
                   arr = receiveUDPClient.Receive(ref remote);
                 
                   Buffer.BlockCopy(arr, 0, receiveBytes, 0, arr.Length);
                   int recLength = receiveBytes.Length;//debugs says recLength=1024*1024*2
                  // MessageBox.Show(recLength.ToString()+","+arr.Length);
            while (arr.ToString() != "over")
                 {
                     arr= receiveUDPClient.Receive(ref remote);
                     MessageBox.Show(arr.Length.ToString());
                     if (arr.ToString().Contains("over,"))
                     {
                         string[] splitstring = arr.ToString().Split(',');
                         recAllLength = Int32.Parse(splitstring[1]);
//I want to get the real length,but actually it is the entire length of the array, not the length of valid data                      
                            break;
                     }
                     MessageBox.Show(recLength.ToString() + "," + arr.Length.ToString());
                     //debugs Exception,Array bounds because reclength=1024*1024*2;
                 Buffer.BlockCopy(arr, 0, receiveBytes,recLength,arr.Length );                
                   // arr = null;
                     recLength += arr.Length;
                }
                    MessageBox.Show(recLength.ToString());



Now receiveBytes inside length of the value of 5 * 1024, but in fact may be less than the length of 5 * 1024, if the loop receiver, will not get the true value, and how do I get the true length of the receiver after the arr?
debug says recLength=1024*2*1024,but in fact it can be less than 5*1024,how can I get the real reclength?
Posted
Updated 28-Nov-11 8:13am
v5

1 solution

Instead of initializing arr to a certain size, let the Receive method return a Byte array, and at that point, arr.Length should have the size of the buffer being copied.
 
Share this answer
 
Comments
LanFanNinja 28-Nov-11 13:54pm    
+5 Yes that sound right to me.
PEIYANGXINQU 28-Nov-11 14:11pm    
I have show my code,but still have exception.
//debugs Exception,Array bounds because reclength=1024*1024*2;
Buffer.BlockCopy(arr, 0, receiveBytes,recLength,arr.Length );
LanFanNinja 28-Nov-11 14:21pm    
Check out this link maybe it will help you.
http://msdn.microsoft.com/en-us/library/system.net.sockets.udpclient.aspx

Look at the example at the bottom.
PEIYANGXINQU 28-Nov-11 13:59pm    
1.if arr is initializied,it must have a certain size?(You say instead of it?)
2. arr.Length should have the size of the buffer being copied.
Is there a conflict between 1 and 2?
PEIYANGXINQU 28-Nov-11 14:02pm    
Dear John Simmons,Is your code like this:
byte[] receiveBytes= new Byte[1024 * 1024 * 2]; byte[] arr=new byte[1024*5]; Buffer.BlockCopy(receiveUDPClient.Receive(ref remote), 0, receiveBytes, 0, receiveUDPClient.Receive(ref remote).Length); int recLength = receiveBytes.Length;//debug says recLength=1024*2*1024

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900