c# - how to close network stream connection properly? -
i have application connecting network server (tcp) , connected using network stream:
if (!ssl) { _networkstream = new system.net.sockets.tcpclient(server, port).getstream(); _streamwriter = new system.io.streamwriter(_networkstream); _streamreader = new system.io.streamreader(_networkstream, encoding.utf8); } if (ssl) { system.net.sockets.tcpclient client = new system.net.sockets.tcpclient(server, port); _networkssl = new system.net.security.sslstream(client.getstream(), false, new system.net.security.remotecertificatevalidationcallback(protocol.validateservercertificate), null); _networkssl.authenticateasclient(server); _streamwriter = new system.io.streamwriter(_networkssl); _streamreader = new system.io.streamreader(_networkssl, encoding.utf8); } ///// since working both reader , writer globally in multiple threads i using reader , writer asynchronously (in 2 threads) , need have both of these streams globally available (they defined public references on class level), don't know if using statement can used here.
how should close connection? objects (networkstream, stream writer , reader) have close() method. should call of them? or 1 of them? there need release resources?
put them in using block. close , dispose of each stream appropriately after operation has been completed.
using (streamreader reader = new streamreader(_networkstream, encoding.utf8)) { }
Comments
Post a Comment