gpt4 book ai didi

c# - 从 C# Windows 窗体向我的域中的客户端发送消息框

转载 作者:太空宇宙 更新时间:2023-11-03 20:46:39 26 4
gpt4 key购买 nike

我有一个 Windows 窗体项目,而不是将文件和文件夹复制到我域中的客户端。将文件或目录进程复制到我域中的 [x] 个客户端时。我想向它发送一个消息框,上面写着“您的 [xdirectory] ​​中有一个新文件夹或文件”。

我无法通过 Messenger 服务完成此操作,因为 Messenger 服务在 XP Sp2 中不起作用,因此我需要另一种方法。也许可以制作客户端/服务器端应用程序。客户端中会有一个监听器应用程序,然后我会向它发送一个 messagebox.show 代码,然后它会向我们显示一个消息框。等

最佳答案

我以前做过类似的事情。我从其他地方获得了大部分代码,但我不记得从哪里来了。

首先是服务器代码:

public class HSTcpServer
{
private TcpListener m_listener;
private IPAddress m_address = IPAddress.Any;
private int m_port;
private bool m_listening;
private object m_syncRoot = new object();
public event EventHandler<TcpMessageReceivedEventArgs> MessageReceived;

public HSTcpServer(int port)
{
m_port = port;
}

public IPAddress Address
{
get { return m_address; }
}

public int Port
{
get { return m_port; }
}

public bool Listening
{
get { return m_listening; }
}

public void Listen()
{
try
{
lock (m_syncRoot)
{
m_listener = new TcpListener(m_address, m_port);

// fire up the server
m_listener.Start();

// set listening bit
m_listening = true;
}

// Enter the listening loop.
do
{
Trace.Write("Looking for someone to talk to... ");

// Wait for connection
TcpClient newClient = m_listener.AcceptTcpClient();
//Trace.WriteLine("Connected to new client");

// queue a request to take care of the client
ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessClient), newClient);
}
while (m_listening);
}
catch (SocketException se)
{
Trace.WriteLine("SocketException: " + se.ToString());
}
finally
{
// shut it down
StopListening();
}
}

public void StopListening()
{
if (m_listening)
{
lock (m_syncRoot)
{
// set listening bit
m_listening = false;
// shut it down
m_listener.Stop();
}
}
}

private void sendMessage(string message)
{
// Copy to a temporary variable to be thread-safe.
EventHandler<TcpMessageReceivedEventArgs> messageReceived = MessageReceived;
if (messageReceived != null)
messageReceived(this, new TcpMessageReceivedEventArgs(message));
}


private void ProcessClient(object client)
{
TcpClient newClient = (TcpClient)client;
try
{
// Buffer for reading data
byte[] bytes = new byte[1024];
StringBuilder clientData = new StringBuilder();

// get the stream to talk to the client over
using (NetworkStream ns = newClient.GetStream())
{
// set initial read timeout to 1 minute to allow for connection
ns.ReadTimeout = 60000;
// Loop to receive all the data sent by the client.
int bytesRead = 0;
do
{
// read the data
try
{
bytesRead = ns.Read(bytes, 0, bytes.Length);
if (bytesRead > 0)
{
// Translate data bytes to an ASCII string and append
clientData.Append(Encoding.ASCII.GetString(bytes, 0, bytesRead));
// decrease read timeout to 1 second now that data is
// coming in
ns.ReadTimeout = 1000;
}
}
catch (IOException ioe)
{
// read timed out, all data has been retrieved
Trace.WriteLine("Read timed out: {0}", ioe.ToString());
bytesRead = 0;
}
}
while (bytesRead > 0);

bytes = Encoding.ASCII.GetBytes("clowns");

// Send back a response.
ns.Write(bytes, 0, bytes.Length);
sendMessage(clientData.ToString());
}
}
finally
{

// stop talking to client
if (newClient != null)
newClient.Close();
}
}
}

public class TcpMessageReceivedEventArgs : EventArgs
{
private string m_message;

public TcpMessageReceivedEventArgs(string message)
{
m_message = message;
}

public string Message
{
get
{
return m_message;
}
}
}

客户端代码:

 class HSTcpClient
{
private TcpClient _client;
private IPAddress _address;
private int _port;
private IPEndPoint _endPoint;
private bool _disposed;

public HSTcpClient(IPAddress address, int port)
{
_address = address;
_port = port;
_endPoint = new IPEndPoint(_address, _port);
}

public void SendForwardedClientMessage(int senderId, int receiverId, int hsId)
{
SendMessage(senderId.ToString() + ":" + receiverId.ToString() + ":" + hsId.ToString());
}

public void SendUpdatedCGBMessage()
{
SendMessage("Update your CGB you clowns");
}

public void SendMessage(string msg)
{
try
{
_client = new TcpClient();
_client.Connect(_endPoint);

// Get the bytes to send for the message
byte[] bytes = Encoding.ASCII.GetBytes(msg);
// Get the stream to talk to the server on
using (NetworkStream ns = _client.GetStream())
{
// Send message
Trace.WriteLine("Sending message to server: " + msg);
ns.Write(bytes, 0, bytes.Length);
// Get the response
// Buffer to store the response bytes
bytes = new byte[1024];

// Display the response
int bytesRead = ns.Read(bytes, 0, bytes.Length);
string serverResponse = Encoding.ASCII.GetString(bytes, 0, bytesRead);
Trace.WriteLine("Server said: " + serverResponse);
}
}
catch (SocketException se)
{
Trace.WriteLine("There was an error talking to the server: " +
se.ToString());
}
finally
{
Dispose();
}
}

#region IDisposable Members

public void Dispose()
{
Dispose(true);
System.GC.SuppressFinalize(this);
}

private void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
if (_client != null)
_client.Close();
}
_disposed = true;
}
}

#endregion

}

然后使用,在表单代码中创建一个服务器变量:

private HSTcpServer m_server;

然后我创建了一个后台工作线程:

private System.ComponentModel.BackgroundWorker hsTcpServerThread;

处理 DoWork 事件:

private void hsTcpServerThread_DoWork(object sender, DoWorkEventArgs e)
{
m_server = new HSTcpServer(<pick your port>);
m_server.MessageReceived += new EventHandler<TcpMessageReceivedEventArgs>(m_server_MessageReceived);
m_server.Listen();
}

然后处理消息接收事件:

void m_server_MessageReceived(object sender, TcpMessageReceivedEventArgs e)
{
//<your code here> - e contains message details
}

然后向服务器发送消息:

HSTcpClient client = new HSTcpClient(<ip address>, <port you picked>);
client.SendForwardedClientMessage(<message details>);

希望我能把所有东西都放进去。

关于c# - 从 C# Windows 窗体向我的域中的客户端发送消息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/615086/

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