在服务器上执行命令,然后将结果发送到Windows中的客户端
Execute a command on the server and send the result to the client in Windows
我正在编写一个简单的服务器程序,该程序执行命令并将结果发送给客户端。我阅读了无数涉及使用popen()
,pipe()
,dup2()
,fork()
等的示例,但是它们都不对我有用,它们并没有很好地解释代码。我也试图自己做,但没有成功。您能为我提供一个有据可查的例子吗?
这是从客户端接收命令/消息的代码:
void server_receive() {
struct sockaddr_in from;
int from_len, recv_len;
char buf[BUFLEN], path[256]; // BUFLEN = 1024
// Getting the path for the command to execute
strcpy(path, getenv("SYSTEMDRIVE"));
strcat(path, "\WINDOWS\System32\tasklist.exe");
from_len = sizeof(from);
memset(buf, ' ', BUFLEN);
// Receiving the command
// I'll add some if-else statements to handle various commands, but for
// now I just need to see if I can even get one to work.
if((recv_len = recvfrom(sockt, buf, BUFLEN, 0, (struct sockaddr*) &from, &from_len)) == SOCKET_ERROR) {
printf("[ERROR] recvfrom() failed: %d.nn", WSAGetLastError());
} else {
printf("Packet received from %s:%dn", inet_ntoa(from.sin_addr), ntohs(from.sin_port));
printf("Data: %snn", buf);
// Code to execute tasklist (I used _popen())
// and send everything back to the client (I used TransmitFile())
}
}
这是将命令/消息发送到服务器的代码:
void client_send(char server[], unsigned short port) {
struct sockaddr_in to;
int s, to_len = sizeof(to);
char buf[BUFLEN]; // BUFLEN = 1024
char message[BUFLEN];
memset((char*) &to, 0, sizeof(to));
to.sin_family = AF_INET;
to.sin_port = htons(port);
to.sin_addr.S_un.S_addr = inet_addr(server);
while(true) {
printf("Enter message: ");
gets(message);
if (sendto(sockt, message, strlen(message), 0, (struct sockaddr*) &to, to_len) == SOCKET_ERROR) {
printf("[ERROR] sendto() failed: %d.nn" , WSAGetLastError());
}
memset(buf, ' ', BUFLEN);
if (recvfrom(sockt, buf, BUFLEN, 0, (struct sockaddr*) &to, &to_len) == SOCKET_ERROR) {
printf("[ERROR] recvfrom() failed: %d.nn", WSAGetLastError());
} else {
printf("Server's response: %snn", buf); /* The result of tasklist
should be outputted by this line of code, however I'm concerned about the
relatively small receive length (BUFLEN = 1024).*/
}
}
}
不必说这两个功能只是我代码的一部分。
您提到的是_popen
(带有领先的下划线)和TransmitFile
表示您在Windows上,该Windows没有fork
或pipe
或相关功能。
在Windows中执行命令有许多替代方案。正如您已经提到的那样,它是通过_popen
(但是您没有说该方法出了什么问题)。其他包括"经典" system
CRT功能。当然,Windows本机CreateProcess
功能。如果要打开"文档",则有ShellExecute
函数。
知道可用的功能将帮助您搜索示例。将术语windows
添加到您的搜索中将有助于找到特定于Windows的示例和教程。并添加术语msdn
将有助于在Microsoft开发人员网络上找到主题。
相关文章:
- 在C 中将多个客户端连接到Windows中的单个服务器中的多线程
- Windows 服务中的多线程命名管道会锁定客户端
- 在服务器上执行命令,然后将结果发送到Windows中的客户端
- 从 SharePoint 2010 服务器下载,在 Windows 客户端上的本地应用程序中打开
- 在cygwin(windows)和gcc/g++mac中的udp客户端服务器
- Linux服务器和Windows客户端上的C 源代码
- 在Windows上编译Mongo DB客户端示例时,链接器错误
- 在C++for Windows 7中查找poco ssl websocket客户端示例
- C++websockets服务器/客户端跨平台linux/windows
- 在Windows下处理多个客户端连接的最佳方法(不使用线程)
- 加速和 Windows 套接字 - 正确处理 TCP 客户端断开连接方案
- 为 Windows 8.1 安装 MQTT 客户端
- Unix 服务器 - Windows 客户端,连接失败
- 将TCP客户端(在Linux / C ++上)连接到TCP服务器(在Windows C#上)
- 在Windows平台中,像xp,win7,并且有一个有用的redis C++客户端
- 使用 Google API 客户端库在 Windows 上C++
- 是否可以为同时使用Windows和Linux的客户端使用相同的DLL
- Linux服务器,发送文件到windows客户端(Socket)
- 在windows客户端服务器应用程序中创建CreateProcess()
- rpc server c++ Linux服务器和Windows客户端