webserver - C# Mini Http Server, Knowing when to close a connection -


i've been coding own http web server in c# using tcplistener class. before mentions this, know of httplistener, after using previously, i.ve had issues due firewall exceptions , needing have admin account etc etc. application, easier make simple, built web server. i've been using python application connect c# webserver, , send simple request, , receive in return simple response.

my question this.. server supposed close connection, or client? ask because if close connection in server after sending response, python app doesn't read of response. instead, socket error thrown "error 10054, 'connection reset peer'". but, if force python app close connection, i'm not sure how detect on c# server, since c# tcpclient not contain disconnect event. do? how know when connected client has received full response can close connection?

currently, works (with thread sleep)

// write headers , body socket         networkstream stream = client.getstream();          // write headers         byte[] buffer = encoding.utf8.getbytes(headers);         stream.write(buffer, 0, buffer.length);          // write response data if request method not head         if (request.requestmethod != httprequestmethod.head)             stream.write(bodybytearr, 0, bodybytearr.length);          stream.flush();          system.threading.thread.sleep(100);         stream.close();         client.close(); 

i think need better alternative thread.sleep(), doesn't work either if client takes more sleep time receive response (slow connection)

headers sent http server:

get /test http/1.1 host: 127.0.0.1 connection: close 

headers sent client:

http/1.1 200 ok date: {now} server: minihttp-aspserver content-type: text/plain; charset=utf-8 content-length: {length} connection: close  {contents} 

have taken @ synchronous , asynchronous socket samples @ h[ttp://msdn.microsoft.com/en-us/library/w89fhyex.aspx][1]

[1]: http://msdn.microsoft.com/en-us/library/w89fhyex.aspx ?

i think can work of logic solution. in synchronous server example (snippet):

       while (true) {             console.writeline("waiting connection...");             // program suspended while waiting incoming                      connection.             socket handler = listener.accept();             data = null;              // incoming connection needs processed.             while (true) {                 bytes = new byte[1024];                 int bytesrec = handler.receive(bytes);                 data += encoding.ascii.getstring(bytes,0,bytesrec);                 if (data.indexof("<eof>") > -1) {                     break;                 }             }              // show data on console.             console.writeline( "text received : {0}", data);              // echo data client.             byte[] msg = encoding.ascii.getbytes(data);              handler.send(msg);             handler.shutdown(socketshutdown.both);             handler.close();         } 

on client side:

         sender.connect(remoteep);              console.writeline("socket connected {0}",                               sender.remoteendpoint.tostring());              // encode data string byte array.             byte[] msg = encoding.ascii.getbytes("this test<eof>");              // send data through socket.             int bytessent = sender.send(msg);              // receive response remote device.             int bytesrec = sender.receive(bytes);             console.writeline("echoed test = {0}",                                 encoding.ascii.getstring(bytes,0,bytesrec));              // release socket.             sender.shutdown(socketshutdown.both);             sender.close(); 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -