想写一个用socket套接字传递指令,实现服务器让客户端关机的功能。但是出现了问题:服务器端和客户端能建立连接,但传递不了数据。
防火墙是关闭的,求大家帮忙看看代码哪里有问题,感谢。
服务器端代码:
/*
服务器
*/
#include<iostream>
#include<string>
#include<WinSock2.h> //网络套接字的处理头文件
#pragma comment(lib,"WS2_32.lib")
using namespace std;
//函数声明
void InitSocket();
SOCKET Listen(int);
void ShutdownClient();
int main()
{
InitSocket();
ShutdownClient();
return 0;
}
void InitSocket() //初始化网络套接字资源
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData))
{
cout << "初始化套接字失败" << endl;
exit(0);
}
cout << "初始化套接字成功" << endl;
}
SOCKET Listen(int port = 31211) //监听客户端发来的请求
{
//创建套接字
SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == server)//判断是否为无效套接字
{
cout << "创建套接字失败" << endl;
exit(0);
}
cout << "创建套接字成功" << endl;
SOCKADDR_IN addr; //创建ip地址
memset(&addr, 0, sizeof(addr)); //初始化
addr.sin_family = AF_INET;//设置ip地址的类型
addr.sin_port = htons(port);
addr.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //允许任何ip连接
if (bind(server, (SOCKADDR*)&addr, sizeof(addr)) != 0)
{
cout << "绑定失败" << endl;
exit(0);
}
listen(server, 5);
return server;
}
void ShutdownClient() //关闭客户端
{
SOCKET server = Listen();
SOCKET client;
SOCKADDR_IN addr;
int addrSize = sizeof(addr);
string shutdown_command = "shutdown -h now"; // 关机指令
int command_size;
cout << "正在接收客户端连接..." << endl;
if (client = accept(server, (SOCKADDR*)&addr, &addrSize) < 0)
{
cout << "接收客户端连接失败" << endl;
exit(0);
}
cout << "接收客户端连接成功!" << endl;
command_size = shutdown_command.size();
//发送数据大小
if (send(client, (char*)&command_size, sizeof(command_size), 0) == -1)
{
cout << "大小发送失败" << endl;
exit(0);
}
cout << "大小发送成功" << endl;
//发送指令
if (send(client, shutdown_command.c_str(), command_size, 0) == -1)
{
cout << "发送失败" << endl;
exit(0);
}
cout << "指令已发送给客户端!" << endl;
closesocket(client);
}
客户端代码:
/*
客户端
*/
#include<iostream>
#include<string>
#include<WinSock2.h> //网络套接字的处理头文件
#pragma comment(lib,"WS2_32.lib")
using namespace std;
//声明函数
void InitSocket();
SOCKET ConnectServer(const char*, int);
void RCVCommand();
int main()
{
InitSocket();
RCVCommand();
}
void InitSocket() //初始化网络套接字资源
{
WSAData wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData))
{
cout << "初始化套接字失败" << endl;
exit(0);
}
cout << "初始化套接字成功" << endl;
}
SOCKET ConnectServer(const char* ip, int port = 31211) //请求连接服务器,返回值为套接字,端口号自定义
{
//创建套接字
SOCKET server = socket(AF_INET, SOCK_STREAM, 0); //(类型,协议,保留参数)
if (INVALID_SOCKET == server) //判断是否为无效套接字
{
cout << "创建套接字失败" << endl;
exit(0);
}
cout << "创建套接字成功,正在连接服务器..." << endl;
//ip地址处理
SOCKADDR_IN addr; //创建ip地址
memset(&addr, 0, sizeof(addr)); //初始化
addr.sin_family = AF_INET; //设置ip地址的类型
addr.sin_port = htons(port); //设置端口,hotons()设置为网络字节顺序
addr.sin_addr.S_un.S_addr = inet_addr(ip);//连接服务器的ip地址
//发出请求
if (connect(server, (SOCKADDR*)&addr, sizeof(addr)) != 0)
{
cout << "与服务器连接失败" << WSAGetLastError() << endl;
return INVALID_SOCKET;
}
cout << "连接服务器成功!" << endl;
return server;
}
void RCVCommand()
{
int command_size;
SOCKET server = ConnectServer("127.0.0.1");
//接收数据大小
if(recv(server,(char*) & command_size, sizeof(command_size), 0) == -1)
{
cout << "接收大小失败" << endl;
exit(0);
}
cout << "接收大小成功" << endl;
char* buffer = new char[command_size+1];
buffer[command_size] = '\0';
//接收指令
if (recv(server, buffer, command_size, 0) == -1)
{
cout << "指令接收失败" << endl;
exit(0);
}
cout << buffer << endl;
//system(buffer);
//cout << "正在关机" << endl;
closesocket(server);
}
运行结果: