本文介绍了如何将对象发送回所有客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我到目前为止的代码。它正在工作,可以接收对象。但是我想将我返回的对象发送给所有连接的客户端。我猜我需要某种类型的arraylist来存储连接的客户端。我该怎么做?
我目前的代码:
class TcpServer
{
private TcpListener _server;
private Boolean _isRunning;
public TcpServer( int port)
{
_server = new TcpListener(IPAddress.Any,port);
_server.Start();
_isRunning = true ;
LoopClients();
}
public void LoopClients()
{
while (_ isRunning)
{
// 等待客户端连接
TcpClient newClient = _server.AcceptTcpClient();
// 找到客户。
// 创建一个处理通信的线程
线程t = 新线程( new ParameterizedThreadStart(HandleClient));
t.Start(newClient);
}
}
public void HandleClient( object obj)
{
// 从传递给线程的参数中检索客户端
TcpClient client =(TcpClient)obj;
// 设置两个流
StreamWriter sWriter = new StreamWriter(client.GetStream(),Encoding.ASCII);
StreamReader sReader = new StreamReader(client.GetStream(),Encoding.ASCII);
// 您可以使用NetworkStream进行读写,
// 但即使请求也没有强制刷新
布尔 bClientConnected = true ;
Person p = null ;
while (bClientConnected)
{
try
{
// 从流中读取
// 如果我想要的话,我可以让对象回来
// 但我想将此对象发回给所有客户
// NetworkStream strm = client.GetStream();
// IFormatter formatter = new BinaryFormatter();
// p =(Person)formatter。反序列化(STRM); //你必须转换反序列化的对象
// strm.Close();
// 在控制台上显示内容。
// Console.WriteLine(新人物对象:名字>+ p.FirstName);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
解决方案
Here's the code I've got so far. It is working and can receive objects. However I want to send the object I get back out to all connected clients. I'm guessing I would need an arraylist of some sort to store the connected clients. How do I do this?
My current code:
class TcpServer { private TcpListener _server; private Boolean _isRunning; public TcpServer(int port) { _server = new TcpListener(IPAddress.Any, port); _server.Start(); _isRunning = true; LoopClients(); } public void LoopClients() { while (_isRunning) { // wait for client connection TcpClient newClient = _server.AcceptTcpClient(); // client found. // create a thread to handle communication Thread t = new Thread(new ParameterizedThreadStart(HandleClient)); t.Start(newClient); } } public void HandleClient(object obj) { // retrieve client from parameter passed to thread TcpClient client = (TcpClient)obj; // sets two streams StreamWriter sWriter = new StreamWriter(client.GetStream(), Encoding.ASCII); StreamReader sReader = new StreamReader(client.GetStream(), Encoding.ASCII); // you could use the NetworkStream to read and write, // but there is no forcing flush, even when requested Boolean bClientConnected = true; Person p = null; while (bClientConnected) { try { // reads from stream //Here if I wanted I could get the object back //But I want to send this object back out to all clients //NetworkStream strm = client.GetStream(); //IFormatter formatter = new BinaryFormatter(); //p = (Person)formatter.Deserialize(strm); // you have to cast the deserialized object //strm.Close(); // shows content on the console. //Console.WriteLine("New person object: First name > " + p.FirstName); } catch (Exception ex) { Console.WriteLine(ex.Message); } } } }
解决方案
这篇关于如何将对象发送回所有客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!