cuốn sách gpt4 ai đã làm

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

In lại Tác giả: Hồ Xil 更新时间:2023-11-01 02:32:19 25 4
mua khóa gpt4 Nike

我有一种情况需要并行发送和接收信息。
我的协议(protocol)可以定义一个读端口和一个写端口。

我目前有以下代码:

public void Listen()
{
ThreadPool.SetMinThreads(50, 50);
listener.Start();

while (true)
{
var context = new ListeningContext(listener.AcceptTcpClient(), WritePort);
}
}

如何从我传递的 TcpClient 创建另一个监听器?

câu trả lời hay nhất

TcpClient 对象包装了一个NetworkStream 对象。您使用 TcpClient của GetStream() 方法访问 NetworkStream 对象,然后使用该对象从网络读取数据和向网络写入数据. NetworkStream 的 MSDN 文章说如下:

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.

sử dụngTcpListener 对象来监听传入的连接。使用调用 AcceptTcpClient() 返回的 TcpClient 对象与远程端点进行通信(读取写入)。

关于c# - 如何使用 TcpListener 和 TcpClient 创建双工连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4442504/

25 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress