最近在学习远程控制的写法,于是按照网络的资料写了这个
客户端:
[C++] 纯文本查看 复制代码 #define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
enum { SHUTDOWN = 1, REBOOT };
//创建客户端套接字
void CreatClientSocket()
{
}
/* 客户端
1. 初始化 Winsock。
2. 创建套接字。
3. 连接到该服务器。
4. 发送和接收数据。
5. 断开连接。
*/
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
//1. 初始化 Winsock。
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
printf("err - could not find a usable");
return 0;
}
//判断版本
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("wVersion - could not find a usable");
WSACleanup();
return 0;
}
//2. 创建套接字。
SOCKET ClientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (ClientSocket == INVALID_SOCKET)
{
printf("ListenSocket -socket error");
return 0;
}
//要分配给绑定套接字的本地地址的 sockaddr 结构
struct sockaddr_in saServer;
//IP地址
saServer.sin_addr.S_un.S_addr = inet_addr("192.168.0.105");
//地址族
saServer.sin_family = AF_INET;
//端口号 h:host to:to n:net网络 s:short l:long
saServer.sin_port = htons(10086);
//3. 连接到该服务器。
int iResult = connect(ClientSocket, (sockaddr*)&saServer, sizeof(saServer));
if (iResult == SOCKET_ERROR) {
printf("connect function failed with error: %ld\n", WSAGetLastError());
iResult = closesocket(ClientSocket);
if (iResult == SOCKET_ERROR)
printf("closesocket function failed with error: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}
printf("成功连接到服务器\n");
//4.接收数据
while (true)
{
int cmd = 0;
//接收数据
recv(ClientSocket, (char*)&cmd, sizeof(cmd), 0);
//匹配数据
switch (cmd)
{
case SHUTDOWN://关机
//system("shutdown -s -t 2");
case REBOOT://重启
//system("shutdown -r -t 2");
break;
default:
//printf("您的输入有误.\n");
break;
}
}
return 0;
}
服务端:
[C++] 纯文本查看 复制代码 #define WIN32_LEAN_AND_MEAN
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
enum { SHUTDOWN = 1, REBOOT };
/* 服务器
1. 初始化 Winsock。
2. 创建套接字。
3. 绑定套接字。
4. 侦听客户端的套接字。
5. 接受来自客户端的连接。
6. 接收和发送数据。
7. 断开连接。
*/
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD(2, 2);
//1. 初始化 Winsock。
err = WSAStartup(wVersionRequested, &wsaData);
if (err != 0) {
printf("err - could not find a usable");
return 0;
}
//判断版本
if (LOBYTE(wsaData.wVersion) != 2 ||
HIBYTE(wsaData.wVersion) != 2) {
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
printf("wVersion - could not find a usable");
WSACleanup();
return 0;
}
//2. 创建套接字。
SOCKET ListenSocket = socket(AF_INET, SOCK_STREAM, 0);
if (ListenSocket == INVALID_SOCKET)
{
printf("ListenSocket -socket error");
return 0;
}
//要分配给绑定套接字的本地地址的 sockaddr 结构
struct sockaddr_in saServer;
//IP地址
saServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
//地址族
saServer.sin_family = AF_INET;
//端口号 h:host to:to n:net网络 s:short l:long
saServer.sin_port = htons(10086);
//3. 绑定套接字。
int iResult = bind(ListenSocket, (sockaddr*)&saServer, sizeof(saServer));
if (iResult == SOCKET_ERROR) {
printf("bind failed with error %u\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
//4. 侦听客户端的套接字。
if (listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
printf("listen function failed with error: %d\n", WSAGetLastError());
printf("正在监听客户端socket...\n");
//5. 接受来自客户端的连接。
while (true)
{
struct sockaddr_in ClientAddr;
int nLength = sizeof(ClientAddr);
//返回的新的套接字 - 建立连接
SOCKET ClientSocket = accept(ListenSocket, (sockaddr*)&ClientAddr, &nLength);
if (ClientSocket == INVALID_SOCKET) {
printf("accept failed: %d\n", WSAGetLastError());
closesocket(ListenSocket);
WSACleanup();
return 1;
}
//建立连接成功 - 主机上线提示
printf("隔壁%s:%d已上线\n", inet_ntoa(ClientAddr.sin_addr), ClientAddr.sin_port);
//提示
printf("请控制主机:\n");
printf("\t1.关机:\n");
printf("\t2.重启:\n");
int cmd;
scanf_s("%d", &cmd);
switch (cmd)
{
case SHUTDOWN://关机
case REBOOT://重启
send(ClientSocket, (char*)&cmd, sizeof(cmd),0);
break;
default:
printf("您的输入有误.\n");
break;
}
//6.关闭套接字
closesocket(ClientSocket);
}
return 0;
}
|