简体   繁体   English

如何使用TcpListener和TcpClient创建双工连接?

[英]How to create a duplex connection using TcpListener and TcpClient?

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? 如何从我传递的TcpClient创建另一个侦听器?

A TcpClient object wraps a NetworkStream object. TcpClient对象包装NetworkStream对象。 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. 您可以使用TcpClientGetStream()方法访问NetworkStream对象,然后使用该对象从网络读取数据并将数据写入网络。 The MSDN article for NetworkStream says the following: NetworkStream的MSDN文章说明如下:

Read and write operations can be performed simultaneously on an instance of the NetworkStream class without the need for synchronization. 可以在NetworkStream类的实例上同时执行读写操作,而无需同步。 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. 使用TcpListener对象侦听传入连接。 Use the TcpClient object returned from the call to AcceptTcpClient() to communicate (read and write) with the remote endpoint. 使用从AcceptTcpClient()调用返回的TcpClient对象与远程端点进行通信(读取写入)。

TCP connection is a full-duplex pipe , take a look here . TCP连接是一个全双工管道 ,看看这里 You don't need a separate port or anything else. 您不需要单独的端口或其他任何东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果Tcplistener不可用,则Tcpclient连接冻结 - Tcpclient connection freeze if Tcplistener is not available 使用TcpClient和TcpListener后混乱的字节数组 - Jumbled byte array after using TcpClient and TcpListener 如何处理TcpClient与TcpListener断开连接 - How to handle TcpClient disconnect from TcpListener 如何通过TcpClient和TcpListener发送对象? - How to send objects through TcpClient and TcpListener? TCPClient & TCPListener | 对等崩溃 c# 重置连接 - TCPClient & TCPListener | Connection reset by peer crash c# 如何使用TcpClient检查您的连接是否被接受? - How to check if your connection is accepted using TcpClient? 如何在 System.Net.Sockets C# 中使用 TCPClient/TCPListener 实现“点对点”连接(NAT 遍历,无端口转发,打孔) - How to achieve a "peer-to-peer" connection with TCPClient/TCPListener in System.Net.Sockets C# (NAT Traversal, no portforward, hole punching) TcpListener TcpClient 获取 IP 地址 - TcpListener TcpClient get IPAddress Tcpclient-NetworkStream-Java中的TcpListener - Tcpclient - NetworkStream - TcpListener in Java 如何在同一网络中具有不同 IP 的设备和计算机上使用 tcplistener 和 tcpclient? - How to use tcplistener and tcpclient on device and computer with different ip in same Network?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM