通过端口监听和接收消息

发布于 2024-07-15 07:07:20 字数 126 浏览 2 评论 0原文

我正在用 C# 创建一个 lanmessenger。 我有通过端口接收和发送消息的代码,但我对如何使用它们有一个“轻微”的困惑。 我是否应该不断调用侦听每个事件中接收传入消息的端口的函数?如果是这样,那么我如何调用应发送消息的函数?

Am creating a lanmessenger in c#. And i have the code to receive and send messages through a port, but I am having a 'minor' confusion regarding how to use them.
Should I constantly call the function that listens to the port from which incoming messages are to be received in each event?.. If so, then how do i call the function which shall send messages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韶华倾负 2024-07-22 07:07:20

使用异步方法(例如BeginReceive),或查看 线程...

如果您使用异步方法,您将不会因轮询而陷入困境,因为您将收到一个 收到数据时的回调

如果您使用线程,则可以轮询(例如,通过检查 Available 循环中 TcpClient 的属性),并且它不会阻止其余代码的执行,因为轮询将在它自己的线程中。

Either use the asynchronous methods (e.g. BeginReceive), or look at threading...

If you use the asynchronous methods, you won't get bogged down by polling, because you will receive a callback when data is received.

If you use threading, you can poll (for example by checking the Available property of a TcpClient in a loop), and it won't prevent the rest of your code from executing, because the polling will be in its own thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文