I want to use this property but in MSDN it says "This property is not currently supported and always throws a NotSupportedException.". When will it be supported Can you suggest any other method or property
private void Receive(object sender, DoWorkEventArgs e) { while (true) { dataTransfer.Reset(); clientStream.BeginRead(ReceiveBuffer, 0, (int)clientStream.Length, new AsyncCallback(AsyncReceiveDataCallback), clientStream); dataTransfer.WaitOne(); } }
I
want to read all bytes that have came on the first time, the incoming
mesagge length is not fixed but it has a maximum value such as 1024
byte. I want to take all the message when it comes, otherwise I have to
find message header and then find the length, but if I know the amount
of data in the stream buffer, I can assume that it is a message.
A Better way to know whats coming and how much Read to make sure message is completely received and do not receive contents of next message, you should send length of each message preceeding each message here is send example:
string message = "this is a message";
byte[] bytes = Encoding.Ascii.GetBytes(message);
byte[] length = BitConvertor.GetBytes(bytes.Length); // Convert message bytes' length to bytes (Will always return 4 bytes "Size of int")
Its means that Property exists but there is not any implementation in it and when you call it it raises NotSuported exception because its somehting like:
public long Length
{
get {
throw new NotSupportedException();
}
}
Tell me what your scenerio and why do you need to get length of the stream, there may be many alternates to get your work done without this property.
Your suggestion is the procedure that I want to implement but we use DNP protocol and sending the length byte is uncompatible with DNP. I guess, I have to decompose DNP Application header then find the length of the message. This is the most reliable way. Thank you for your suggestions Rizwan...
NetworkStream.Length Property
Kamii47
Thank your for link, it gave me some idea...
Recep TARAKÇIOĞLU
Try out this article:
http://www.yoda.arachsys.com/csharp/readbinary.html
mmmmBeeeer
private void Receive(object sender, DoWorkEventArgs e)
{
while (true)
{
dataTransfer.Reset();
clientStream.BeginRead(ReceiveBuffer, 0, (int)clientStream.Length,
new AsyncCallback(AsyncReceiveDataCallback), clientStream);
dataTransfer.WaitOne();
}
}
I want to read all bytes that have came on the first time, the incoming mesagge length is not fixed but it has a maximum value such as 1024 byte. I want to take all the message when it comes, otherwise I have to find message header and then find the length, but if I know the amount of data in the stream buffer, I can assume that it is a message.
technoTABLET
A Better way to know whats coming and how much Read to make sure message is completely received and do not receive contents of next message, you should send length of each message preceeding each message here is send example:
string message = "this is a message";
byte[] bytes = Encoding.Ascii.GetBytes(message);
byte[] length = BitConvertor.GetBytes(bytes.Length); // Convert message bytes' length to bytes (Will always return 4 bytes "Size of int")
clientStream.Write(length, 0, length.Length); // Send Length
clientStream.Write(bytes , 0, bytes.Length); // Send the actual message
On Receiving side you can use mixture of Asyncrhonous and Synchnronous Read methods to get work done efficiently without blocking.
I writie the steps here, if you face any problem you can contact again.
1) Read only 4 bytes asynchronously
2) convert those 4 bytes in to int using BitConvert.ToInt32();
3) Create a Memory stream to read all the bytes fron NetworkStream into NetworkStream here is snippet
int messageSize = BitConverter.ToInt32(messageDataLengthBuffer, 0); // it was read from Asynch Read ano now we are converting it back to int
MemoryStream memstrm = new MemoryStream();
readByteCount = 0;
while (messageSize > 0)
{
if (messageSize > messageDataBuffer.Length)
readByteCount = dataStream.Read(messageDataBuffer, 0, messageDataBuffer.Length);
else
readByteCount = dataStream.Read(messageDataBuffer, 0, messageSize);
memstrm.Write(this.messageDataBuffer, 0, readByteCount);
messageSize -= readByteCount;
}
It'll exactly read the length of the message (Complete) not 1 bytes less or more.
I hope this will help.
Best Regards,
Rizwan
lou_1
Its means that Property exists but there is not any implementation in it and when you call it it raises NotSuported exception because its somehting like:
public long Length
{
get {
throw new NotSupportedException();
}
}
Tell me what your scenerio and why do you need to get length of the stream, there may be many alternates to get your work done without this property.
Best Regards,
Rizwan
Prashweenet
Your suggestion is the procedure that I want to implement but we use DNP protocol and sending the length byte is uncompatible with DNP. I guess, I have to decompose DNP Application header then find the length of the message. This is the most reliable way. Thank you for your suggestions Rizwan...