HttpListener
I'm currently writing an extreme lightweight HTTP server in PowerShell script(which very obviously utilizes the .NET framework). I want to figure out how to check if a connection is still alive before dispatching the response with HttpListenerResponse.Close(), because if i try to write to a dead connection it obviously throws errors, and i'd like to handle this gracefully before the catch block needs to trigger
4 Replies
the documentation wasnt very helpful at first glance, probably partially because the HTTP protocoll is asynchronous in nature
this is not that simple; there are the Connected property and Poll method, but socket could have Linger option, eventually there is Keep-Alive header, and in the end simply the client could not be responding so you have to have a timeout; probably there are other conditions too
hm so better just catch it in the try catch block and handle it individually?
after all the server does not prematurely terminate the connection and if the clients no longer interested the request should be discarded anyways right?
as for tcp, closing the connection means sending a specific packet
so technically you could still send all of your data (if small) and send close
i would say just do the things you need to do, set a timeout in the socket, and that's it
i don't know if it makes sense thinking at this level if you just want to make a barebone http server
still, if you need some performance then you could need to implement some stuff
or maybe it's only for xhr requests, i don't know