1

I have a function that takes an array as a parameter. The function then fills the array up with information of unknown length. So how would I create an array to store a message of unknown length? Because I can't specify the size of the array since I don't know the size of the message it is going to store.

Would this be valid?

byte [] array;
function (array);

And then the size of the array will be determined by the size of the message it is filled in? If this isn't possible how would I do this?

I need the array to be the exact size of the message it is filled up with, so I can't just specify the array to be some random size big enough to fit the message.


Additional, from a comment:

public int ReceiveFrom( byte[] buffer, int offset, int size, 
       SocketFlags socketFlags, ref EndPoint remoteEP ) 
7
  • 1
    You can't. And that's why such interfaces don't exist in the library. Commented Dec 30, 2011 at 23:45
  • 1
    @DarrenYoung: one valid reason for downvoting is that the relevant info (again) has to be extracted form comments. The misunderstanding that caused the question is excusable, the (far) too short question itself is not. Also look at Fahrad's previosu question. Commented Dec 31, 2011 at 0:12
  • @HenkHolterman I specifically didn't specify the function prototype for a reason: The function that I mentioned is not the only function which is giving me a problem. In addition I have several other functions which is giving me the same problem. I cannot and will not waste time posting a variety of prototypes when the question is so generalized. Commented Dec 31, 2011 at 0:17
  • 1
    @Fahrad - one example would have cleared up a lot. As it finally did. Commented Dec 31, 2011 at 0:18
  • @HenkHolterman Your right, that was a fault on my part. I will be more specific next time. Commented Dec 31, 2011 at 0:27

5 Answers 5

3

You can use a List and then once your message is filled call ToArray

Edit

Example:

List<byte> message = new List<byte>();
message.Add(/*your byte*/);
//More Adds
function(message.ToArray());
Sign up to request clarification or add additional context in comments.

2 Comments

Not going to help. OP has no control over the method implementation or its signature, so passing anything other than an array is not possible.
I see that now. That wasn't obvious before he commented on your answer.
2

About using

public int ReceiveFrom( byte[] buffer, int offset, int size, 
   SocketFlags socketFlags, ref EndPoint remoteEP ) 

You are supposed to use this in a loop. You are receiving parts of the total message (stream) at a time. Up to you to decide which part you need. Optionally shift the rest down and specify an offset in the next call.

byte[] buffer = new byte[1024];
ínt n;
do 
{
    n = ReceiveFrom(buffer, 0, buffer.Lenght, ...);

    if (n > 0)
        // process n bytes in buffer

} while (n > 0);

4 Comments

Thanks. The looping would only be required for TCP sockets though right? Or would this be for UDP sockets too?
I don't know UDP sockets but I'm quite certain it works the same way.
UDP is datagram-oriented, not stream-oriented. The buffer needs to be large enough to hold the entire datagram (up to 64 KiB). Otherwise the data gets truncated and the method throws a SocketException.
Thank You- I guess I won't need to loop then.
0

You could use a List instead. Although possible, you don't need to set a predetermined length and the List will expand as more objects are added.

16 Comments

It specifically has to be an array.
Does that mean you have no control over the function? (i.e. change it?)
@Farhad - Can you post the function's signature?
@Fahrad: I thought so. You are just misunderstanding how socket I/O works.
Please see the MSDN documentation regarding use of Socket.ReceiveFrom. It describes fairly well how it is intended to be called. msdn.microsoft.com/en-us/library/kbfwcz73.aspx
|
0

Do you need to do something with the array before passing it to the function that fills it up? If you don't, why don't you just return the array from the function instead of passing it one:

byte[] retArray = function();

where function is:

byte[] function()
{
}

You can then find out the length of the array by checking:

if( retArray != null )
{
  Console.WriteLine( retArray.Count );
}

Comments

0

It's hard to tell without knowing the exact details, but you could let the function create the array after it knows the length of the information.

Additionally, with Linq (using ToArray and ToList) you can cast from and to arrays and lists, but again, it's hard to tell without knowing the intentions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.