System.AggregateException in TCP requests

I'm trying to make my own version of HTTP, however I get the following exception whenever I try to communicate between my client and server: System.AggregateException: One or more errors occurred. (Unable to read data from the transport connection: An established connection was aborted by the software in your host machine..)
41 Replies
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
I've attached my library, the error is on line 58 of WtpClient.cs
jcotton42
jcotton42โ€ข2mo ago
please $paste the relevant code @LocalDutchBoi โœ๐Ÿณ๐ŸŒˆ, people are unlikely to download zips
MODiX
MODiXโ€ข2mo ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
Never used the bot, give me a sec $paste
MODiX
MODiXโ€ข2mo ago
If your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
It uses a self-made testing lib, so it likely won't run on your own machine, the testing lib is in the zip
leowest
leowestโ€ข2mo ago
for one this is wrong
int bytesRead;
while ((bytesRead = await _stream.ReadAsync(buffer)) > 0)
{

}
int bytesRead;
while ((bytesRead = await _stream.ReadAsync(buffer)) > 0)
{

}
every time the loop hits ReadAsync it may return a different number so u need to sum it for the total bytes u've read
jcotton42
jcotton42โ€ข2mo ago
small nit, you don't need GC.SuppressFinalize also, I see await using var stream = client.GetStream(); in WtpServer.HandleClientAsync, which I think might be closing the connection after the initial message handle Is that what you want?
leowest
leowestโ€ข2mo ago
also you're encoding with u8 but reading ASCII on line 38 on the first tab of the paste.mod link above althou it might work for some strings not good to mix encoding like that
cap5lut
cap5lutโ€ข2mo ago
yes disposing a stream would close the connection. the whole reading back of the response in the client is wrong. as mentioned, you are not keeping track of how many bytes u have read in total, and u are also overwriting the previously received chunk. basically if u get a 16384 bytes request, in best case u get the last 8192 bytes, in worst case u get the last 1 byte. HTTP messages (both requests and responses) can often be really long so u dont want to read them into memory all at once. you would streamingly read the next bytes to build some kind of object that contains the headers and then some stream object that consists of the body by parsing the incoming data. the buffering of data is really annoying here because HTTP isnt a length+value protocol, so u dont really know when u can stop reading the headers and maybe even have read part of the message body. for that data buffering at least there is System.IO.Pipelines (i would recommend reading the article as a whole, it has through out some important pieces of information)
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
In this case it returns the same byte[] using only disposes it after it exits the scope, no? I'll try doing it without the await using part yeah ik i try to keep it consistent but my ide whines i'll fix the memory reading part, it was just a quick thing to test
leowest
leowestโ€ข2mo ago
the thing is, it goes out of scope as soon as it finishes the first message because it does not have a loop to receive more messages
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
yeah because then it instantly returns
cap5lut
cap5lutโ€ข2mo ago
the server side part i couldnt check, i dont download arbitrary zip files
leowest
leowestโ€ข2mo ago
thus closing the connection
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
removed await using, now it doesnt error anymore but it hangs probably an issue with my testing framework theres a server file in the paste i sent
leowest
leowestโ€ข2mo ago
either way you have no logic in your server that will care for more than the first message it gets
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
i started it yesterday night i do
while (true)
{
_ = HandleClientAsync(await _tcpListener.AcceptTcpClientAsync());
}
while (true)
{
_ = HandleClientAsync(await _tcpListener.AcceptTcpClientAsync());
}
leowest
leowestโ€ข2mo ago
no that only cares for the connection
cap5lut
cap5lutโ€ข2mo ago
i think im blind โคด๏ธ is the only source i see
leowest
leowestโ€ข2mo ago
it takes the accepted client and calls that method
leowest
leowestโ€ข2mo ago
that method does not do anything else besides running once
cap5lut
cap5lutโ€ข2mo ago
ooh
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
that's a design thing
leowest
leowestโ€ข2mo ago
sure
cap5lut
cap5lutโ€ข2mo ago
didnt know that has tabs nowadays ๐Ÿ˜‚ im sorry
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
it's a like http not sockets
leowest
leowestโ€ข2mo ago
always had im sure it is that is why cotton asked u how you wanted it work if u wanted to get only 1 request at a time of ir u wanted it to return multiple
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
i just want 1 request, 1 response
leowest
leowestโ€ข2mo ago
anyway, what u want to do is you want to signal you're closing the socket after u send your response
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
might update WtpClient so it can reconnect to different servers
leowest
leowestโ€ข2mo ago
wait for the other part to reply and then close
cap5lut
cap5lutโ€ข2mo ago
that HandleClientAsync closes the connection after handling it, is okay and expected. its just that it should handle the whole thing, not just one message (eg, if u want to implement keep-alive, it would start to handle multiple requests, timeouts, etc)
Wqffles โœ๐Ÿณ๐ŸŒˆ๐Ÿ‡ฎ๐Ÿ‡ฑ
i just want 1 message i'll implement things like "received" and stuff this fixed the error btw thanks leowest, leopard and cap5lut
cap5lut
cap5lutโ€ข2mo ago
even if its just accet-receive-response-close, u would sill do all of that in that method ;p
leowest
leowestโ€ข2mo ago
leopard actually pointed that out not me ๐Ÿ˜‰ but you're welcome
cap5lut
cap5lutโ€ข2mo ago
that method is more or less ur main method for each connection
Want results from more Discord servers?
Add your server