I need to communicate with a piece of hardware that only accepts 1 TCP connection. But I cannot find any good example where the same NetworkStream is used for reading and writing simoultanously.
The MSDN documentation says the following:
Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. As long as there is one unique thread for the write operations and one unique thread for the read operations, there will be no cross-interference between read and write threads and no synchronization is required.
However, I am unable to find an example that shows how to use the same instance of a NetworkStream in different threads to read and write.
I am unsure whether I should:
- simply use the
NetworkStreamdirectly for reading withmyNetworkStream.ReadAsync(), or use aStreamReader, - simply use the
NetworkStreamdirectly for writing withmyNetworkStream.WriteAsync(), or use aStreamWriter, - directly start a new thread with a
while(true)for reading upon instantiation of theTcpClient, - keep a global reference to my
NetworkStreaminstance so that I can write whenever...
I'm far from being an expert when it comes to TCP connections/network communication, so there are definitely things I don't fully understand...
Thanks in advance :)