Hi!
There is anyway to make the buffer size of a socket dynamic ?
Because i need to download some things and my buffer of 1024 bytes cant get all the information.
How could i do this ?
Thanks
Hi!
There is anyway to make the buffer size of a socket dynamic ?
Because i need to download some things and my buffer of 1024 bytes cant get all the information.
How could i do this ?
Thanks
You do not need a dynamic socket buffer. Use a fixed-size read buffer in a loop and grow only the destination (file or MemoryStream). The key is to always write exactly the number of bytes that Receive returned. Converting arbitrary file bytes to a string (as in your GetString) will corrupt data, which explains the garbled characters you saw. Also, trimming trailing zeros from the buffer is incorrect; those zeros are just unused bytes in the array, not part of the payload.
Example receiver (binary-safe, unknown total size, stop when the sender closes the connection):
using (var fs = File.Create(path))
{
var buf = new byte[32 * 1024]; // fixed chunk size
int n;
while ((n = sock.Receive(buf, 0, buf.Length, SocketFlags.None)) > 0)
{
fs.Write(buf, 0, n); // write only what was read
}
} When to stop: either
Receive returns 0), orstatic void ReadExact(Socket s, byte[] b, int len) {
int off = 0;
while (off < len) {
int n = s.Receive(b, off, len - off, SocketFlags.None);
if (n == 0) throw new IOException("Disconnected");
off += n;
}
} Avoid sentinel strings like "opasgarfinish" inside a binary stream; those byte patterns can appear in real data. And changing Socket.ReceiveBufferSize does not solve this; it controls the OS buffer, not how much you get per call. was right to steer you toward chunked reads in a loop.
Jump to Post— kvprajapati 1,826>Dynamic buffer size for socket ?
Take a look at this thread.
So you say to do a while loop, to receive all the data in more than one step ?
I have tried that, but i have two problems the one is that i don't know how to properly safe the temp buffer and then join with the other buffer that are received.
Suppouse, i send a data that is read in 5 times by mu buffer.
How could i save the 5 parts (in bytes) and join them propertly ?
And the other thing is, that i don't know when to stop the while loop.
If i send 3mb data, how do i know when all data is received and stop the loop?
Thanks
I have been trying but without success.
I have this code:
while (!String.Equals(opasgard, "opasgarfinish"))
{
coming = getSocket(numsock).receiveDownload();
opasgard = encoding.GetString(coming);
if(!String.Equals(opasgard, "opasgarfinish"))
bw.Write(coming);
}
bw.Close();
fs.Close(); And the receive function:
public byte[] receiveDownload()
{
buffer = new byte[13];
string[] words;
len = socket.Receive(buffer);
i = (buffer.Length) - 1;
while (buffer[i] == 0)
--i;
// now bar[i] is the last non-zero byte
bar = new byte[i + 1];
words = System.Text.Encoding.UTF8.GetString(bar).Split('|');
if (words.Contains("opasgarfinish"))
{
System.Text.ASCIIEncoding encoding=new System.Text.ASCIIEncoding();
Byte[] opasgard = encoding.GetBytes("opasgarfinish");
return opasgard;
}
return bar;
} And the file is made, but now with the correct chars, i get:
**You have 敲挀攀椀瘀攀搀 琀栀攀 猀oftware as p牡琀 漀昀 琀栀攀 嘀椀猀ual Studio 9〮 䈀攀琀愀 倀爀漀最爀愀m. When it must be:
**You have received the software as part of the Visual Studio 9.0 Beta Program. Any idea?
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.