服务器收到客户端的消息,如何将消息转换为Active Window中的击键?
Server receives message from Client, how to convert message to keystrokes in Active Window?
我已经让 client/server 正常工作,客户端正在毫无问题地向服务器发送数据,现在我只有一个显示从客户端接收到的数据的 RichTextBox。
我想做的是让服务器显示 RichTextBox 中接收到的数据,然后将消息发送到活动 window(发生这种情况时,服务器将最小化到系统托盘).
客户端部分:
using System.Net;
using System.Net.Sockets;
private TcpClient client = new TcpClient();
private IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("MyIP"), 8888);
public Console()
{
InitializeComponent();
client.Connect(serverEndPoint);
}
private void SendMessage(string msg)
{
NetworkStream clientStream = client.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(msg);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
///...
private void button1_Click(object sender, EventArgs e)
{
SendMessage("T");
}
现在,所有这些对我来说似乎都很好,因为我的服务器应用程序正在正常接收消息。我的问题出在服务器端。我似乎无法让 SendKeys 函数正常工作,所以我在我的代码片段中对其进行了注释:
服务器部分:
using System.Net;
using System.Net.Sockets;
using System.Threading;
private TcpListener tcpListener;
private Thread listenThread;
private int connectedClients = 0;
private delegate void WriteMessageDelegate(string msg);
public Form1()
{
InitializeComponent();
Server();
}
private void Server()
{
this.tcpListener = new TcpListener(IPAddress.Any, 8888);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
TcpClient client = this.tcpListener.AcceptTcpClient();
connectedClients++;
lblNumberOfConnections.Text = connectedClients.ToString();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
break;
}
if (bytesRead == 0)
{
connectedClients--;
lblNumberOfConnections.Text = connectedClients.ToString();
break;
}
ASCIIEncoding encoder = new ASCIIEncoding();
//Write message in RichTextBox and send keystrokes to active window
string msg = encoder.GetString(message, 0, bytesRead);
WriteMessage(msg);
// SendKeys.Send(msg);
}
tcpClient.Close();
}
private void WriteMessage(string msg)
{
if (this.rtbServer.InvokeRequired)
{
WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
this.rtbServer.Invoke(d, new object[] { msg });
}
else
{
this.rtbServer.AppendText(msg + Environment.NewLine);
}
正如我所说,它在文本框中显示数据就好了,但我也想做的(也是这个应用程序的主要目的)是获取从客户端收到的消息,并添加它到 SendKeys(或类似的),并在活动 window 中模拟那些按键。不过我似乎不知道该怎么做...
此外,由于我在我的客户端应用程序中使用了 SendMessage,并且我需要发送到活动 window 的一些按键包含修饰符,我只是将它们作为文本消息发送到我的服务器(即。对于命令 CTRL-T,我确实将文本 "CTRL-T" 发送到我的服务器应用程序),我希望我可以按照以下方式做一些事情:
- 如果通过单次击键收到消息,只需使用 SendKeys(即
SendKeys.Send("T");
)
- 如果消息是作为带有修饰符(即 CTRL-T)的击键接收的,请将其更改为正确的 SendKeys 修饰符(即。
SendKeys.Send("{^T}");
)
我手动为带有修饰符的命令分配事件没有问题,因为只会收到两个使用修饰符的命令,但我也不确定如何实现该部分。
我希望我的服务器发送从我的客户端接收到的字符串(我认为是 msg
)并将其作为按键发送到活动的 window。我想在代码中添加一个 "IF" 语句,这样如果服务器收到某个消息(带有修饰符的命令;这些都是已知命令,因此我可以编写要监听的命令),它会发送一个指定按键(取决于接收到的命令)到活动 window.
更新:
我已经设法弄清楚如何使用 SendKeys
将数据从客户端发送到服务器计算机上的活动 window。需要更改的是 SendKeys
命令的原始位置。我已经更新如下:
//Write message in RichTextBox
string msg = encoder.GetString(message, 0, bytesRead);
WriteMessage(msg);
}
tcpClient.Close();
}
private void WriteMessage(string msg)
{
if (this.rtbServer.InvokeRequired)
{
WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
this.rtbServer.Invoke(d, new object[] { msg });
}
else
{
this.rtbServer.AppendText(msg + Environment.NewLine);
// SEND KEYSTROKES TO ACTIVE WINDOW
SendKeys.Send(msg);
}
现在我的问题又回到了我对 C# 缺乏经验的问题上。
我不确定实现 IF
语句的正确方法 我需要在将字符串发送到活动 window 之前捕获该字符串。我需要先使用 IF
语句来捕获字符串,因为来自服务器的某些消息未格式化为正确的击键。
基本上,我想做的是在我的 IF
语句中输入任何只是一个字符的内容,然后查找两个特定的消息。
我需要拦截的两条消息是"CTRL-T"和"CTRL-H"。
我不知道正确的格式,因为这是我的第一个 C# 应用程序,我边学边学,但我的想法是按如下方式告诉程序:
如果字符串中包含的消息是 "CTRL-T" 那么 SendKeys.Send("{^T}");
如果字符串中包含的消息是 "CTRL-H" 那么 SendKeys.Send("{^H}");
如果是其他情况,则 SendKeys.Send(msg);
这听起来像是完成我在这里解释的内容的正确方法吗?
编辑: 我试图弄清楚如何格式化我的 IF
语句,但这显然是错误的:
// SEND KEYSTROKES TO ACTIVE WINDOW
if (msg.Equals("CTRL-T"))
{
SendKeys.Send("{^T}");
}
else if (msg.Equals("CTRL-H"))
{
SendKeys.Send("{^H}");
}
else
{
SendKeys.Send(msg);
}
我通过用 "TEST"
替换 "{^T}"
测试了第一个 IF
语句,它毫无问题地将单词 TEST 发送到记事本,但是当我输入 "{^T}"
,我一按下按钮发送该命令,我的服务器就崩溃了。
尝试发送 "{^H}"
时也会发生同样的事情
有人知道我做错了什么吗?
我想我会回答我自己的问题,因为我已经有了我现在正在寻找的基本功能。
基本上,我想接受来自客户端应用程序的消息,并将其显示在我的服务器应用程序的文本框中,然后将该消息转换为活动 window 中的模拟按键。
在将按键发送到 window 之前,我必须确保只有单个数字的按键被立即发送到活动的 window,而其他两个已知命令需要修饰符附加到它们的在发送之前被捕获,并进行了修改,因此它们将充当修改后的按键,而不是作为文本块(即 "CTRL-T")而不是实际的 "CTRL" button
+[=12= 发送]
//Write message in RichTextBox
string msg = encoder.GetString(message, 0, bytesRead);
WriteMessage(msg);
}
tcpClient.Close();
}
private void WriteMessage(string msg)
{
if (this.rtbServer.InvokeRequired)
{
WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
this.rtbServer.Invoke(d, new object[] { msg });
}
else
{
this.rtbServer.AppendText(msg + Environment.NewLine);
// SEND KEYSTROKES TO ACTIVE WINDOW
if (msg.Equals("CTRL-T"))
{
SendKeys.Send("^T");
}
else if (msg.Equals("CTRL-H"))
{
SendKeys.Send("^H");
}
else
{
SendKeys.Send(msg);
}
我已经让 client/server 正常工作,客户端正在毫无问题地向服务器发送数据,现在我只有一个显示从客户端接收到的数据的 RichTextBox。
我想做的是让服务器显示 RichTextBox 中接收到的数据,然后将消息发送到活动 window(发生这种情况时,服务器将最小化到系统托盘).
客户端部分:
using System.Net;
using System.Net.Sockets;
private TcpClient client = new TcpClient();
private IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("MyIP"), 8888);
public Console()
{
InitializeComponent();
client.Connect(serverEndPoint);
}
private void SendMessage(string msg)
{
NetworkStream clientStream = client.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(msg);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
}
///...
private void button1_Click(object sender, EventArgs e)
{
SendMessage("T");
}
现在,所有这些对我来说似乎都很好,因为我的服务器应用程序正在正常接收消息。我的问题出在服务器端。我似乎无法让 SendKeys 函数正常工作,所以我在我的代码片段中对其进行了注释:
服务器部分:
using System.Net;
using System.Net.Sockets;
using System.Threading;
private TcpListener tcpListener;
private Thread listenThread;
private int connectedClients = 0;
private delegate void WriteMessageDelegate(string msg);
public Form1()
{
InitializeComponent();
Server();
}
private void Server()
{
this.tcpListener = new TcpListener(IPAddress.Any, 8888);
this.listenThread = new Thread(new ThreadStart(ListenForClients));
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while (true)
{
TcpClient client = this.tcpListener.AcceptTcpClient();
connectedClients++;
lblNumberOfConnections.Text = connectedClients.ToString();
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
private void HandleClientComm(object client)
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
bytesRead = clientStream.Read(message, 0, 4096);
}
catch
{
break;
}
if (bytesRead == 0)
{
connectedClients--;
lblNumberOfConnections.Text = connectedClients.ToString();
break;
}
ASCIIEncoding encoder = new ASCIIEncoding();
//Write message in RichTextBox and send keystrokes to active window
string msg = encoder.GetString(message, 0, bytesRead);
WriteMessage(msg);
// SendKeys.Send(msg);
}
tcpClient.Close();
}
private void WriteMessage(string msg)
{
if (this.rtbServer.InvokeRequired)
{
WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
this.rtbServer.Invoke(d, new object[] { msg });
}
else
{
this.rtbServer.AppendText(msg + Environment.NewLine);
}
正如我所说,它在文本框中显示数据就好了,但我也想做的(也是这个应用程序的主要目的)是获取从客户端收到的消息,并添加它到 SendKeys(或类似的),并在活动 window 中模拟那些按键。不过我似乎不知道该怎么做...
此外,由于我在我的客户端应用程序中使用了 SendMessage,并且我需要发送到活动 window 的一些按键包含修饰符,我只是将它们作为文本消息发送到我的服务器(即。对于命令 CTRL-T,我确实将文本 "CTRL-T" 发送到我的服务器应用程序),我希望我可以按照以下方式做一些事情:
- 如果通过单次击键收到消息,只需使用 SendKeys(即
SendKeys.Send("T");
) - 如果消息是作为带有修饰符(即 CTRL-T)的击键接收的,请将其更改为正确的 SendKeys 修饰符(即。
SendKeys.Send("{^T}");
)
我手动为带有修饰符的命令分配事件没有问题,因为只会收到两个使用修饰符的命令,但我也不确定如何实现该部分。
我希望我的服务器发送从我的客户端接收到的字符串(我认为是 msg
)并将其作为按键发送到活动的 window。我想在代码中添加一个 "IF" 语句,这样如果服务器收到某个消息(带有修饰符的命令;这些都是已知命令,因此我可以编写要监听的命令),它会发送一个指定按键(取决于接收到的命令)到活动 window.
更新:
我已经设法弄清楚如何使用 SendKeys
将数据从客户端发送到服务器计算机上的活动 window。需要更改的是 SendKeys
命令的原始位置。我已经更新如下:
//Write message in RichTextBox
string msg = encoder.GetString(message, 0, bytesRead);
WriteMessage(msg);
}
tcpClient.Close();
}
private void WriteMessage(string msg)
{
if (this.rtbServer.InvokeRequired)
{
WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
this.rtbServer.Invoke(d, new object[] { msg });
}
else
{
this.rtbServer.AppendText(msg + Environment.NewLine);
// SEND KEYSTROKES TO ACTIVE WINDOW
SendKeys.Send(msg);
}
现在我的问题又回到了我对 C# 缺乏经验的问题上。
我不确定实现 IF
语句的正确方法 我需要在将字符串发送到活动 window 之前捕获该字符串。我需要先使用 IF
语句来捕获字符串,因为来自服务器的某些消息未格式化为正确的击键。
基本上,我想做的是在我的 IF
语句中输入任何只是一个字符的内容,然后查找两个特定的消息。
我需要拦截的两条消息是"CTRL-T"和"CTRL-H"。
我不知道正确的格式,因为这是我的第一个 C# 应用程序,我边学边学,但我的想法是按如下方式告诉程序:
如果字符串中包含的消息是 "CTRL-T" 那么 SendKeys.Send("{^T}");
如果字符串中包含的消息是 "CTRL-H" 那么 SendKeys.Send("{^H}");
如果是其他情况,则 SendKeys.Send(msg);
这听起来像是完成我在这里解释的内容的正确方法吗?
编辑: 我试图弄清楚如何格式化我的 IF
语句,但这显然是错误的:
// SEND KEYSTROKES TO ACTIVE WINDOW
if (msg.Equals("CTRL-T"))
{
SendKeys.Send("{^T}");
}
else if (msg.Equals("CTRL-H"))
{
SendKeys.Send("{^H}");
}
else
{
SendKeys.Send(msg);
}
我通过用 "TEST"
替换 "{^T}"
测试了第一个 IF
语句,它毫无问题地将单词 TEST 发送到记事本,但是当我输入 "{^T}"
,我一按下按钮发送该命令,我的服务器就崩溃了。
尝试发送 "{^H}"
有人知道我做错了什么吗?
我想我会回答我自己的问题,因为我已经有了我现在正在寻找的基本功能。
基本上,我想接受来自客户端应用程序的消息,并将其显示在我的服务器应用程序的文本框中,然后将该消息转换为活动 window 中的模拟按键。
在将按键发送到 window 之前,我必须确保只有单个数字的按键被立即发送到活动的 window,而其他两个已知命令需要修饰符附加到它们的在发送之前被捕获,并进行了修改,因此它们将充当修改后的按键,而不是作为文本块(即 "CTRL-T")而不是实际的 "CTRL" button
+[=12= 发送]
//Write message in RichTextBox
string msg = encoder.GetString(message, 0, bytesRead);
WriteMessage(msg);
}
tcpClient.Close();
}
private void WriteMessage(string msg)
{
if (this.rtbServer.InvokeRequired)
{
WriteMessageDelegate d = new WriteMessageDelegate(WriteMessage);
this.rtbServer.Invoke(d, new object[] { msg });
}
else
{
this.rtbServer.AppendText(msg + Environment.NewLine);
// SEND KEYSTROKES TO ACTIVE WINDOW
if (msg.Equals("CTRL-T"))
{
SendKeys.Send("^T");
}
else if (msg.Equals("CTRL-H"))
{
SendKeys.Send("^H");
}
else
{
SendKeys.Send(msg);
}