将指令发送给一个线程正在等待对TCP? [英] Sending instructions to a thread which is waiting for TCP?

查看:138
本文介绍了将指令发送给一个线程正在等待对TCP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解的多线程TCP所以我编码一个基本的Telnet文本路由器。

I'm trying to understand multi-threading in TCP so I'm coding a basic telnet text "router".

使用的ReadLine()使用一个TCP监听器将等待来自Telnet客户端输入,然后每个线程响应此基础上发送的文本。我有这个正与多线程和多telnet客户端。

using ReadLine() each thread using a TCP listener will wait for input from the telnet client and then respond based on the text which is sent. I have this working with multiple threads and multiple telnet clients.

我要有条件地将消息发送到所有线程。 例如,如果从任何一个线程发出的文本是警报!然后,我想每一个线程连接的客户端执行的WriteLine(警报!)

I want to conditionally send messages to all threads. For example, if the text sent from any one thread is "Alert!" then I want every thread for connected clients to execute WriteLine("Alert!")

这是否有道理? 我的问题是,我不知道如何让一个线程引发一个事件在另一个线程。

Does this make sense? My problem is that I don't know how to make one thread raise an event in another thread.

推荐答案

您需要看一个事件代理模式。基本上你将有你的所有线程订阅事件的一个对象。它也将有一个可以被称为将调用该事件的方法。这也许听起来很复杂,但它相当简单。

You need to look at a Event Broker pattern. Basically you would have one object with an event that all your threads subscribe to. It will also have a method that can be called which will invoke the event. It maybe sounds complicated, but its fairly simple.

例如code是这里<一个href="http://msforge.net/blogs/paki/archive/2007/11/20/EventBroker-implementation-in-C_2300_-full-source-$c$c.aspx" rel="nofollow">http://msforge.net/blogs/paki/archive/2007/11/20/EventBroker-implementation-in-C_2300_-full-source-$c$c.aspx.

这篇关于将指令发送给一个线程正在等待对TCP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆