C.S远程指令控制
一. 模型结构
客户端:
客户端首先创建一个套接字,该套接字绑定的是目标IP和端口,在调用connect函数的时候将自己的IP和端口发送给服务器。
服务端:
服务端首先创建一个套接字,该套接字和自身IP以及端口绑定,在调用accept函数的时候,从缓冲区将客户端的IP和端口提取出来,创建一个新的套接字,利用该套接字可以和特定客户端进行通信。
二. 代码
客户端:
#include<winsock2.h>
#include<winsock.h>
#include<stdio.h>
#include<iostream>
#include<chrono>
#include<thread>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"ws2_32")
using namespace std;
int portScan(char* ip, int port)
{
cout << "Scan port " << port << endl;
int the_socket = socket(AF_INET, SOCK_STREAM, 0);
if (the_socket < 0)
{
cout << "Error creating socket!" << endl;
return 1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
if (connect(the_socket, (struct sockaddr*)&server_addr, sizeof(server_addr)) < 0)
{
closesocket(the_socket);
return 1;
}
cout << "Port " << port << " is open" << endl;
closesocket(the_socket);
return 0;
}
int beat(char* ip,int port,int the_socket)
{
cout << "发送心跳报文!" << endl;
char sendbuf[] = { "I like you!" };
if (send(the_socket, sendbuf, int(strlen(sendbuf)), 0) > 0)
{
cout << "心跳发送成功!" << endl;
}
return 0;
}
void threadFunction(char* ip, int port)
{
int the_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
bind(the_socket, (SOCKADDR*)&server_addr, sizeof(server_addr));
if (connect(the_socket, (SOCKADDR*)&server_addr, sizeof(server_addr)) == 0)
{
cout << "连接成功!" << endl << endl;
}
while (true)
{
beat(ip, port,the_socket);
this_thread::sleep_for(chrono::seconds(1));
}
closesocket(the_socket);
}
int command(char* ip, int port)
{
return 0;
}
int main()
{
WSADATA wsa;
int num;
WSAStartup(MAKEWORD(2, 2), &wsa);
int port = 10001;
char ip[20] = { "192.168.125.137" };
for (num = 0; num < 1001; num++)
{
portScan(ip, num);
}
int the_socket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
bind(the_socket, (SOCKADDR*)&server_addr, sizeof(server_addr));
if (connect(the_socket, (SOCKADDR*)&server_addr, sizeof(server_addr)) == 0)
{
cout << "Connect Success!" << endl << endl;
}
cout << "发送心跳报文!" << endl;
char sendbuf[] = { "I like you!" };
while (true)
{
if (send(the_socket, sendbuf, int(strlen(sendbuf)), 0) > 0)
{
cout << "心跳发送成功!" << endl;
}
this_thread::sleep_for(chrono::seconds(5));
}
char cmd[25] = { "ipconfig > output.txt" };
char buf[100] = {0};
int num = send(the_socket, cmd, sizeof(cmd), 0);
if (num > 0)
{
cout << "命令发送成功!" << endl;
}
int flag = 0;
do
{
flag = recv(the_socket, buf, sizeof(buf), 0);
if (flag > 0)
{
cout << "接收反馈成功!" << endl;
}
} while (flag == 0);
cout << buf;
closesocket(the_socket);
WSACleanup();
return 0;
}
服务端:
#include<winsock2.h>
#include<winsock.h>
#include<stdio.h>
#include<iostream>
#include<iomanip>
#include<ctime>
#include<chrono>
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"ws2_32")
using namespace std;
int main()
{
WSADATA wsa;
WSAStartup(MAKEWORD(2, 2), &wsa);
char Recv[100];
int the_socket = socket(AF_INET, SOCK_STREAM, 0);
char ip[] = { "192.168.226.25" };
int port = 10001;
int iResult = 0;
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr(ip);
server_addr.sin_port = htons(port);
if (!bind(the_socket, (SOCKADDR*)&server_addr, sizeof(server_addr)))
{
cout << "bind操作成功" << endl;
}
if (!listen(the_socket, SOMAXCONN))
{
cout << "listen操作成功" << endl;
}
int con_socket = accept(the_socket, NULL, NULL);
char Answer[10] = { "Success!" };
while (true)
{
iResult = recv(con_socket, Recv, sizeof(Recv), 0);
cout << Recv << endl;
int Result = system(Recv);
if (!Result)
{
cout << "命令执行成功!"<< endl;
if (send(con_socket, Answer, sizeof(Answer), 0))
{
cout << "反馈成功!" << endl;
}
break;
}
}
closesocket(the_socket);
WSACleanup();
return 0;
}
标签:控制,addr,lib,int,server,指令,include,远程,cout From: https://www.cnblogs.com/ONEZJ/p/18088667/cs-remote-instruction-control-19laai