I am doing the best research to write "correct" network code in C #
I have seen many examples using the "experiment" statement of C #, and I think that this is a good way, although I have seen it incompatible use with different expressions.
For example, suppose I have some such code:
TCP client TCP client = new TCP client ("url.com", 80); NetworkStream TCP Stream = TCP Client Gatestream (); Streamerdider TCPRDER = new streamrider (TCPSTream); StreamWriter tcpWriter = new streamer (TCPstream);
Obviously, this code is going to be very fickle. So, I've seen some code that TCP uses on the client, which sounds good though, does the networkstream not have any resources that need cleaning? What about the streamrider / writer?
Do I need to wrap all the 4 statements in nested statements?
And if so, what happens when time comes to take out? Do not stop StreamWriter stream and the resulting socket? Then what happens when the streamminder, then the networkstream, then the TCP clients go through each of their settlements?
Which one brings another question, with both StreamReader and StreamWriter, made of the same stream, who owns it? Do they both think that they do not do it on their own, and thus both will try to destroy it? Or does the structure know that the stream has already been destroyed and can quietly ignore it?
It seems that the statement in the series is only necessary for the last statement in the series, but what happens if an exception is thrown into GetStream ()? I do not think this socket will be cleaned properly, so it seems that it is not necessary to use unnecessary use.
Does anyone know about network programming with any good, recent books? Pure, and Preferentically C # include that chapter on exception handling and resource management? Or maybe a good article online? All the books I can find NET 1.1 is from the era (Microsoft .Net Framework, Network Programming in Net for Network Programming, etc.), so it seems like a subject that needs some good resources.
EDIT:
Please, do not leave a very good comment of the mark off to anyone else by commenting on this :)
I read elses to anyone Want to hear recommendations or opinions on resource management, especially in relation to asynchronous use?
After
Generally, objects should handle internally many settlement ()
Calls, and do main codes only once; Therefore there is a stream going on Dispos ()
d Sometimes there is not a problem personally, I will use many using
; Note that although you do not need to indent the nest (unless different levels are different life-time), using /:
(TcpClient tcpClient = new TcpClient ("url. Com ", 80)) using NetworkStream (tcpStream = tcpClient.GetStream ()) (StreamReader tcpReader = New StreamReader (tcpStream)) (StreamWriter using tcpWriter = New StreamWriter (tcpStream)) {...}
As you say, it ensures that if any error occurs during the beginning, then everything is still cleaned properly. Also ensures that every level gets the opportunity to get it done correctly (in the right order) with any buffer data, etc.
Re-ownership; The networkstream
is actually the inequality in the first place ... Most streams have either input xor output network stream
something Bends the rules and sends two directions in an API; So this is an exception ... In general, ownership will become clear; Additionally, many wrappers have a flag to determine if they should close the wrapped stream. does not do StreamReader
, but some do (like GZipStream
, which has a left
ctor option) . You do not want to own proprietary flow, this is an option - or use a non-closing stream moderator - one is ( NonClosingStream
or similar)
Books again; I raised a copy of "TCP / IP Sockets C #: Practical Guide to Programmers" (Enough), but not great.
Comments
Post a Comment