I have a situation where I need to send and receive information parallelly.
My protocol can define a read port and a write port.
I currently have the following code:
public void Listen()
{
ThreadPool.SetMinThreads(50, 50);
listener.Start();
while (true)
{
var context = new ListeningContext(listener.AcceptTcpClient(), WritePort);
}
}
How can I create another listener from the TcpClient I am passing?
A TcpClient
object wraps a NetworkStream
object. You use the GetStream()
method of TcpClient
to access the NetworkStream
object, which is then used to read data from and write data to the network. The MSDN article for NetworkStream
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.
Use the TcpListener
object to listen for incoming connections. Use the TcpClient
object returned from the call to AcceptTcpClient()
to communicate (read and write) with the remote endpoint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With