C
C#8mo ago
mellowzippy

✅ Mysterious byte problem

https://github.com/MellowZippy1/Networking I am working on a school project, I need to make a UDP-based FTP server, it should send a file using a slow-start approach, this has not been implemented yet. I do have a quite unique problem which is that even though I should be sending the same amount of bytes to the client (1024 bytes plus a JSON overhead) it keeps sending different amounts of overhead as shown in the picture below. It is sending all the data but I am wondering why the amount of bytes keeps changing? Is this an issue or just something that I should deal with?
GitHub
GitHub - MellowZippy1/Networking: Networking assignments met Naud
Networking assignments met Naud . Contribute to MellowZippy1/Networking development by creating an account on GitHub.
No description
28 Replies
mellowzippy
mellowzippyOP8mo ago
Unsure what difficulty to tag this problem at. I just pushed the new version, if there's any bugs please attempt to clone again
Angius
Angius8mo ago
Did you try debugging to see what gets sent?
mellowzippy
mellowzippyOP8mo ago
Yeah It sends an array of 1024 bytes, a MessageType enum which is Data and then some json overhead. I am unsure why this would keep changing? Its 1024 bytes, the same MessageType and it should be the same json overhead, right?
Angius
Angius8mo ago
Is the content of the JSON the same each time?
mellowzippy
mellowzippyOP8mo ago
No, it would be different parts of the hamlet. But it would still be 1024 bytes Same amount of bytes, different bytes.
Angius
Angius8mo ago
Then the length will differ as well
mellowzippy
mellowzippyOP8mo ago
How so?
Angius
Angius8mo ago
{"name":"bob"} is shorter than {"name":"phranephrates"}
mellowzippy
mellowzippyOP8mo ago
no think of it like this:
{"content": "abc"}
{"content": "cba"}
{"content": "abc"}
{"content": "cba"}
or
{"content": "abc"}
{"content": "def"}
{"content": "abc"}
{"content": "def"}
It would be the same amount of bytes, correct?
Angius
Angius8mo ago
Yeah
mellowzippy
mellowzippyOP8mo ago
So why does it send a different amount of bytes
Angius
Angius8mo ago
Place a breakpoint at where the data gets serialized, and double-check that it actually always gets serialized to a string of the same length
mellowzippy
mellowzippyOP8mo ago
A string gets put into a byte[1024]
Angius
Angius8mo ago
a string gets put into a byte[] Of whatever is the length required
mellowzippy
mellowzippyOP8mo ago
no the length is always 1024
Angius
Angius8mo ago
string serializedMessage = JsonSerializer.Serialize(message);
byte[] data = Encoding.ASCII.GetBytes(serializedMessage);
string serializedMessage = JsonSerializer.Serialize(message);
byte[] data = Encoding.ASCII.GetBytes(serializedMessage);
yes
mellowzippy
mellowzippyOP8mo ago
so hamlet (a big file) gets split into multiple packets of 1024 byes.
Angius
Angius8mo ago
I don't see you setting 1024 anywhere here
mellowzippy
mellowzippyOP8mo ago
private Dictionary<int, (bool, byte[])>? InitializePackets()
{
Dictionary<int, (bool, byte[])>? packets = new();
try
{
string text = File.ReadAllText("hamlet.txt");
byte[] tempbuffer = Encoding.ASCII.GetBytes(text);
int amountOfPackets = (int)Math.Ceiling((double)tempbuffer.Length / (double)ChunkSize);

for (int i = 0; i < amountOfPackets; i++)
{
int startIndex = i * ChunkSize;
int length = Math.Min(ChunkSize, tempbuffer.Length - startIndex);
byte[] chunk = new byte[length];
Buffer.BlockCopy(tempbuffer, startIndex, chunk, 0, length);
packets[i] = (i == amountOfPackets - 1, chunk); // Mark last packet
}

return packets;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
HandleErrors(e);
}

return null!;
}
private Dictionary<int, (bool, byte[])>? InitializePackets()
{
Dictionary<int, (bool, byte[])>? packets = new();
try
{
string text = File.ReadAllText("hamlet.txt");
byte[] tempbuffer = Encoding.ASCII.GetBytes(text);
int amountOfPackets = (int)Math.Ceiling((double)tempbuffer.Length / (double)ChunkSize);

for (int i = 0; i < amountOfPackets; i++)
{
int startIndex = i * ChunkSize;
int length = Math.Min(ChunkSize, tempbuffer.Length - startIndex);
byte[] chunk = new byte[length];
Buffer.BlockCopy(tempbuffer, startIndex, chunk, 0, length);
packets[i] = (i == amountOfPackets - 1, chunk); // Mark last packet
}

return packets;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
HandleErrors(e);
}

return null!;
}
Angius
Angius8mo ago
Ah, I was looking at the client
mellowzippy
mellowzippyOP8mo ago
Is okay
Angius
Angius8mo ago
Huh At which point does the size become not-1024?
! ! Paradise
! ! Paradise8mo ago
wow cool
mellowzippy
mellowzippyOP8mo ago
In theory, the content of each packet should be 1024 bytes (plus overhead, which should be the same). In practice, the total amount of bytes keeps changing everytime it gets sent.
Angius
Angius8mo ago
Yeah, but where? How deep did you try placing the breakpoints? I'd step through the code from the moment you split the file text, and see where does the length change
mellowzippy
mellowzippyOP8mo ago
We figured it out, its because we encoded the chunk of hamlet text twice. We encode it, then convert it to json, encode it again and send it How do I close a ticket?
Angius
Angius8mo ago
$close
MODiX
MODiX8mo ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server