gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:32:19 24 4
gpt4 key购买 nike

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

我目前有以下代码:

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

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

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

最佳答案

TcpClient 对象包装了一个NetworkStream 对象。您使用 TcpClientGetStream() 方法访问 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.

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com