In C#, in order to use the UdpClient.Send() method I must provide as one of the parameters the number of bytes I am sending.
How do I calculate the number of bytes in a datagram before sending it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You pass UdpClient.Send() an array of bytes (Byte[]), an integer size, and an IPEndPoint. If you are sending the entire byte array, nothing more and nothing less, as your datagram’s payload, you can just use the Length property of arrays as follows:
Perhaps the confusion here is that you think you have to count the number of bits that will be sent out over the wire? What is actually required is just the size of the payload (the part of the provided byte array you actually want to send in this datagram). The library will do the rest.
Examples and info here.