what to do when message size is bigger than the byte[] size in Socket.BeginReceive?
-
lets say you set an initial byte[] size to 30 and the message that you receive has a size of 50. How would you handle that with Socket.BeginReceive With Socket.Receive this would work:
byte[] buffer = new byte[30];
int size = Socket.Receive(buffer);
String msg = Encoding.ASCII.GetString(buffer, 0, size);while(size > 0)
{
buffer = new byte[30];
size = Socket.Receive(buffer);
msg = msg + Encoding.ASCII.GetString(buffer, 0, size);
}How would you do the same thing with Socket.BeginReceive? Thank you, Prateek
-
lets say you set an initial byte[] size to 30 and the message that you receive has a size of 50. How would you handle that with Socket.BeginReceive With Socket.Receive this would work:
byte[] buffer = new byte[30];
int size = Socket.Receive(buffer);
String msg = Encoding.ASCII.GetString(buffer, 0, size);while(size > 0)
{
buffer = new byte[30];
size = Socket.Receive(buffer);
msg = msg + Encoding.ASCII.GetString(buffer, 0, size);
}How would you do the same thing with Socket.BeginReceive? Thank you, Prateek
use the overloaded receive method that takes in an offset, and a length to read. Also, don't redeclare the buffer as it is unnecessary.
int read = 0;
byte[] buffer = new byte[255];
while( ( read = socket.Receive(buffer, 0, buffer.Length)) != 0){... Encoding.ASCII.GetString(buffer, 0, read);
}
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
use the overloaded receive method that takes in an offset, and a length to read. Also, don't redeclare the buffer as it is unnecessary.
int read = 0;
byte[] buffer = new byte[255];
while( ( read = socket.Receive(buffer, 0, buffer.Length)) != 0){... Encoding.ASCII.GetString(buffer, 0, read);
}
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.Thank you for your suggestion... i know how to do it with Socket.Receive. I wanted to do this with Socket.BeginReceive. The problem is in order to get the first part of the msg, you'll have to use Socket.EndReceive and then how will you get the rest of the msg?
-
Thank you for your suggestion... i know how to do it with Socket.Receive. I wanted to do this with Socket.BeginReceive. The problem is in order to get the first part of the msg, you'll have to use Socket.EndReceive and then how will you get the rest of the msg?
Begin Receive contains a similar overload as receive. The logic is the exact same, except in the callback method you must call BeginRecieve again after reading all of the bytes.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
Begin Receive contains a similar overload as receive. The logic is the exact same, except in the callback method you must call BeginRecieve again after reading all of the bytes.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.so just for clearification the code would be something like this: int size = socket.EndReceive(iasyncresult); while(size > 0) socket.BeginReceive(...);
-
so just for clearification the code would be something like this: int size = socket.EndReceive(iasyncresult); while(size > 0) socket.BeginReceive(...);
no. read the MSDN.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
no. read the MSDN.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego.ok. Thanks for your help :)
-
lets say you set an initial byte[] size to 30 and the message that you receive has a size of 50. How would you handle that with Socket.BeginReceive With Socket.Receive this would work:
byte[] buffer = new byte[30];
int size = Socket.Receive(buffer);
String msg = Encoding.ASCII.GetString(buffer, 0, size);while(size > 0)
{
buffer = new byte[30];
size = Socket.Receive(buffer);
msg = msg + Encoding.ASCII.GetString(buffer, 0, size);
}How would you do the same thing with Socket.BeginReceive? Thank you, Prateek
If anybody is looking for the answer, do check this link [^] out. Also, be careful with the ReceiveTimeouts. ReceiveTimeout is for synchrounous receive only. So, if you don't want your loop to get stuck, make sure you include some way of identifying the end of message. Good Luck, Prateek