通过端口监听和接收消息
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用异步方法(例如
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 aTcpClient
in a loop), and it won't prevent the rest of your code from executing, because the polling will be in its own thread.