我不得不编辑我的答案,因为愚蠢地我没有完整地阅读你的问题。
我建议创建一个应用程序,它位于两台计算机上,通过一个简单的 TCP 服务器/客户端套接字相互发送请求。
例如,这将允许您按下 PC 1 上的按钮,使其进入睡眠状态并对监视器执行相同操作,并向 PC 2 发送消息以唤醒并窃取监视器输入。就速度而言,我想这取决于几个变量,但这可以在以后进行。
TCP 客户端/服务器:
using System;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO;
namespace ClientSocket_System
{
class tcpClientSocket
{
#region Global Variables
TcpClient tcpService;
IPEndPoint serverEndPoint;
NetworkStream netStream;
#endregion
public void commToServer()
{
tcpService = new TcpClient();
serverEndPoint = new IPEndPoint(IPAddress.Parse("xxx.xxx.xxx.xx"), xxxx); //Enter IP address of computer here along with a port number if needed
try
{
tcpService.Connect(serverEndPoint);
netStream = tcpService.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes("SwitchComputers");
netStream.Write(buffer, 0, buffer.Length);
netStream.Flush();
tcpService.Close();
}
catch(Exception ex)
{
}
}
}
}
和服务器:
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.IO;
namespace ClientSocket_System
{
class tcpServerTerminal
{
private TcpListener tcpListener;
private Thread listenThread;
private TcpClient tcpService;
string msgFromClient;
public void ServerStart()
{
tcpListener = new TcpListener(IPAddress.Any, 5565);
listenThread = new Thread(new ThreadStart(ListenForClients));
listenThread.Start();
}
public void ListenForClients()
{
tcpListener.Start();
while (true)
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
//create a thread to handle communication
//with connected client
Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
clientThread.Start(client);
}
}
public void HandleClientComm(object client)
{
tcpService = (TcpClient)client;
NetworkStream netStream = tcpService.GetStream();
byte[] message = new byte[4096];
int bytesRead;
while (true)
{
bytesRead = 0;
try
{
//blocks until a client sends a message
bytesRead = netStream.Read(message, 0, 4096);
}
catch
{
//a socket error has occured
break;
}
if (bytesRead == 0)
{
//the client has disconnected from the server
break;
}
//message has successfully been received
ASCIIEncoding encoder = new ASCIIEncoding();
msgFromClient = encoder.GetString(message, 0, bytesRead);
if (msgFromClient == "SwitchComputers")
{
//RUN CODE HERE TO ACTION PC SLEEP AND MONITOR SLEEP
msgFromClient = null;
}
}
}
public void SocketSend()
{
NetworkStream streamToClient = tcpService.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes("SwitchComputers");
streamToClient.Write(buffer, 0, buffer.Length);
streamToClient.Flush();
}
}
}
类似的东西,至少值得研究一下,上面的代码并没有完全完善,但它可以让您通过家庭网络控制两台计算机的操作,从而允许同时执行特定命令:睡眠/唤醒等.
希望这能给你一个新的调查方向。
另外,我认为将阻止输入的代码格式化为最佳做法:
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", CharSet=CharSet.Auto, ExactSpelling=true)]
public static extern bool BlockInput([In, MarshalAs(UnmanagedType.Bool)] bool fBlockIt);