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

📄 sendmsg.cpp

📁 简单的远程控制工具,分为服务器与客户斋,让你了解socket编程的知识.
💻 CPP
字号:
// msg.cpp : implementation of the CMsg class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1998 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.

#include "stdafx.h"
#include "Resource.h"
#include "Sendmsg.h"
#include "Ctrl_Clnt9Doc.H"
#include "..\CtrlSrvr2\SENDKEY.H"
#include "cmd_file.H"
extern CCtrl_Clnt9Doc g_pDoc;


 int ReadSafe(CWSocket *lpWSK,  char * lpData, int nSize)
{
/*	int nLen, nRet;

    nLen=nSize;
    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);
}

bool RetrviceMsg(UINT wCmd, CCtrl_Clnt9Doc *pDoc)
{
	SENDMSG SendMsg;
    char *lpText;
	CString strtmp;

Loop01:
	//等待返回
	memset(&SendMsg, 0, sizeof(SENDMSG));
	if (ReadSafe(&pDoc->m_wskClient, (char *)&SendMsg, sizeof(SENDMSG)) < 0)
        goto Err01;
	switch(SendMsg.wCmd)
	{
/*	case CMD_FILE_RECEIVE:
		  dwFileSize = SendMsg.dwBmpSize;
		  lpFileData = GlobalAlloc (GMEM_FIXED,dwFileSize);
		  if (lpFileData == NULL)
			{SendMsg.iErrorCode = -12;
		     strcpy(SendMsg.szText,"MSG: File Send Failed");
		     break;
		  }
		  else
			SendMsg.iErrorCode = 0;
	      nRet = m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60);		
          if (nRet <= 0 ){ GlobalFree(lpFileData); goto ErrorSend001;}
	      if (ReadSafe(&m_wskClient, (char *)lpFileData, dwFileSize) < 0)
		  {GlobalFree(lpFileData); goto ErrorSend001; }
		  lpParam = m_strSaveFile;
		  CmdProc_RecFile((char *)lpParam, lpFileData);
		  DisplayMsg (SendMsg.szText);
		break;
*/
	case CMD_GET_SCREEN_INFO://被控端信息
			pDoc->m_nBits = SendMsg.nBits;      
			pDoc->m_nSrvrBits = SendMsg.nBits;
			pDoc->m_nVersion = SendMsg.nVersion;
	        pDoc->m_SrvrArea = CRect (0, 0, SendMsg.rcArea.right, SendMsg.rcArea.bottom);
			break;
	 case CMD_STATE_INFO:
     case CMD_SEND_END:
		    if (SendMsg.dwFileSize == 0) break;
		    lpText = new char[SendMsg.dwFileSize+1];
			memset(lpText, 0, SendMsg.dwFileSize+1);
		    if (lpText == NULL) 
				goto Err01;
	        if (ReadSafe(&pDoc->m_wskClient, (char *)lpText,SendMsg.dwFileSize) < 0)
                goto Err01;
		    pDoc->DisplayMsg (lpText);
			delete lpText;
			if (SendMsg.wCmd == CMD_SEND_END) break;
			goto Loop01;
/*		     while(SendMsg.wCmd==CMD_STATE_INFO)
			 {
//Step 1: get struct
		        nSize = (int)SendMsg.dwFileSize;
			    lpText = (char *)calloc(nSize+1, sizeof(char));
			    if (lpText == NULL)
				{
				  SendMsg.iErrorCode = -1;
                  if (m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
	              goto ErrorSend001;
				}
//Step 2: confirm
			     SendMsg.iErrorCode = 0;
                 if (m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
	             goto ErrorSend001;
//Step 3: Get Msg
	             if (ReadSafe(&m_wskClient, (char *)lpText, nSize) < 0)
                 goto ErrorSend001;
		         DisplayMsg (lpText);
			     free(lpText);
			     lpText = NULL;
//Step 4: confirm
                 if (m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
                 goto ErrorSend001;
//Step 5: wait next
	             if (ReadSafe(&m_wskClient, (char *)&SendMsg,sizeof(SENDMSG)) < 0)
                 goto ErrorSend001;
			 }//end while
		     DisplayMsg (SendMsg.szText);
			 break;

       case CMD_SOCKET_CLOSE:
		     pDoc->DisplayMsg (SendMsg.szText);
			 break;
*/
	}//end switch
	pDoc->m_bUsing = false;
    return true;		
Err01:		
	pDoc->DisconnectSocket();
    pDoc->m_bUsing = false;
	strtmp.Format("MSG: s%X error for socket  %d", pDoc->m_wskClient, pDoc->m_wskClient.GetError());
    pDoc->DisplayMsg(strtmp);
    if (!pDoc->m_bConnectAuto) return false;
	while(AfxMessageBox("注意: 你确定要重新连接吗?", MB_YESNO) == IDYES)
		{
          if (pDoc->ConnectSocket())  return false;
		}
    return false;
}



bool SendMsg(UINT wCmd)
{
    bool b_return = true;
	CCtrl_Clnt9Doc *pDoc;
//	pDoc = (CCtrl_Clnt9Doc *)GetActiveDoument();
//取当前Doument
	CDocTemplate*	pDocTp;
	POSITION pos = AfxGetApp()->GetFirstDocTemplatePosition ();
	pDocTp = AfxGetApp()->GetNextDocTemplate (pos);
	POSITION posDoc = ((CMultiDocTemplate*)pDocTp)->GetFirstDocPosition ();
	pDoc = (CCtrl_Clnt9Doc *)pDocTp->GetNextDoc (posDoc);
	if (pDoc==NULL) return false;

//	LPVOID  lpFileData;
//	DWORD   dwFileSize;
	CFile	fp;
//	int		nRet;
	CString strtmp;
    SENDMSG SendMsg;
    LPCTSTR   lpParam;
//    LPSTR   lpText;
//	int     nSize;

    if (!pDoc->m_bConnecting) return false;
//    if (pDoc->m_bUsing) return true;
	pDoc->m_bUsing = true;
    memset(&SendMsg, 0, sizeof(SENDMSG));
	memcpy(SendMsg.szPassword, pDoc->m_strPassword, 
		   pDoc->m_strPassword.GetLength());

/*	if (m_strParam.GetLength() > 50)
	{
	  SendMsg.wCmd = CMD_STATE_INFO;
	  SendMsg.dwFileSize = m_strParam.GetLength();
      if (m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
	    goto ErrorSend001;
	  if (ReadSafe(&m_wskClient, (char *)&SendMsg, sizeof(SENDMSG)) < 0)
        goto ErrorSend001;
	  if (SendMsg.iErrorCode < 0) 
	  return ;
	  lpParam = m_strParam;
      if (m_wskClient.SendData((char *)lpParam,m_strParam.GetLength() , 60) < 0 )
	    goto ErrorSend001;
	  SendMsg.dwFileSize = 0;
	  if (ReadSafe(&m_wskClient, (char *)&SendMsg, sizeof(SENDMSG)) < 0)
        goto ErrorSend001;
	  if (SendMsg.iErrorCode < 0) 
	  return ;
	}
	else
	{
	  memcpy(SendMsg.szText, m_strParam, m_strParam.GetLength());
	  SendMsg.dwFileSize = m_strParam.GetLength();
	}
*/
    pDoc->m_wskClient.SetTimeout(60);
	SendMsg.wCmd = wCmd;
	switch (wCmd)
		{

/*		case CMD_FILE_SEND:
//Step 1: Get File
			nRet = fp.Open (m_strSaveFile,CFile::modeRead);
			if (nRet == 0) 	return;
			dwFileSize = fp.GetLength();
			lpFileData = GlobalAlloc (GMEM_FIXED,dwFileSize);
			if (lpFileData == NULL)
			{GlobalFree(lpFileData);fp.Close(); return;}
			fp.ReadHuge (lpFileData,dwFileSize);
			fp.Close ();
//Step 2: Send struct
			SendMsg.dwBmpSize = m_strParam.GetLength();
            if (m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
	        goto ErrorSend001;
//Step 3: Confirm
	        if (ReadSafe(&m_wskClient, (char *)&SendMsg, sizeof(SENDMSG)) < 0)
			{GlobalFree(lpFileData);goto ErrorSend001;}
			if (SendMsg.iErrorCode < 0) 
			{GlobalFree(lpFileData);return ;}
//Step 4: Send file
            if (m_wskClient.SendData((char *)lpFileData,dwFileSize, 60) < 0 )
			{GlobalFree(lpFileData);goto ErrorSend001;}
	        GlobalFree(lpFileData);
            goto SendWait001;
//DEL		case CMD_KEY_HOOK:
*/
//		case CMD_NULL:
//		case CMD_LOCK_SCREEN:
//		case CMD_KEY_CAD:
		case CMD_KEY_HOOK:
			SendMsg.dwHookFlags = pDoc->m_dwHookFlags;
			SendMsg.dwHookParam1 =pDoc->m_dwHookParam1;
			SendMsg.dwHookParam2 =pDoc->m_dwHookParam2;
            //if (pDoc->m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
            //   goto Err01;
			//pDoc->m_bUsing = false;
			//return true;
			break;
		case CMD_MOUSE_DBHOOK:
			SendMsg.wCmd = CMD_MOUSE_DBHOOK;
			SendMsg.dwHookFlags = pDoc->m_dwHookFlags;
			SendMsg.dwHookParam1 = pDoc->m_dwHookParam1 * 65535 / pDoc->m_SrvrArea.Width();
			SendMsg.dwHookParam2 = pDoc->m_dwHookParam2 * 65535 / pDoc->m_SrvrArea.Height();
            //if (pDoc->m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
            //   goto Err01;
			//pDoc->m_bUsing = false;
			//return true;
			break;
		case CMD_MOUSE_HOOK:
			SendMsg.dwHookFlags = pDoc->m_dwHookFlags;
			SendMsg.dwHookParam1 = pDoc->m_dwHookParam1 * 65535 / pDoc->m_SrvrArea.Width();
			SendMsg.dwHookParam2 = pDoc->m_dwHookParam2 * 65535 / pDoc->m_SrvrArea.Height();
            //if (pDoc->m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
            //   goto Err01;
			//pDoc->m_bUsing = false;
			//return true;
			break;
		case CMD_GET_SCREEN_INFO:
		case CMD_NULL:
		case CMD_LOCK_SCREEN:
		case CMD_KEY_CAD:
			 break;
		default:
	        SendMsg.dwFileSize = pDoc->m_strParam.GetLength();
			break;
		}//end switch
//发送命令
   if (pDoc->m_wskClient.SendData((char *)&SendMsg, sizeof(SENDMSG), 60) < 0 )
     goto Err01;
   if(SendMsg.dwFileSize > 0) 
   {
	 lpParam = pDoc->m_strParam;
     if (pDoc->m_wskClient.SendData(lpParam, SendMsg.dwFileSize, 60) < 0 )
     goto Err01;
   }
   switch (wCmd)
   {
		case CMD_FILE_SEND:  //发送文件
		if (CmdProc_ReceiveFile ((LPSTR)(LPCTSTR)pDoc->m_strSaveFile, &pDoc->m_wskClient) < 0) goto Err01;
		strtmp = "MSG: File Send Successfully!";
		break;
		case CMD_FILE_RECEIVE:
		if (CmdProc_SendFile ((LPSTR)(LPCTSTR)pDoc->m_strSaveFile, &pDoc->m_wskClient) < 0) goto Err01;
//		strtmp = "MSG: File Send Successfully!";
		break;
		case CMD_NULL:
		case CMD_LOCK_SCREEN:
		case CMD_KEY_CAD:
		case CMD_KEY_HOOK:
		case CMD_MOUSE_DBHOOK:
		case CMD_MOUSE_HOOK:
            pDoc->m_bUsing = false;
			return true;
			
   }
   b_return = RetrviceMsg(wCmd, pDoc);
   pDoc->m_bUsing = false;
   return b_return;		


Err01:		
    pDoc->m_bUsing = false;
	pDoc->DisconnectSocket();
	strtmp.Format("MSG: s%X error for socket  %d", pDoc->m_wskClient, pDoc->m_wskClient.GetError());
    pDoc->DisplayMsg(strtmp);
    if (!pDoc->m_bConnectAuto) return false; 
//自动重连
	while(AfxMessageBox("注意: 你确定要重新连接吗?", MB_YESNO) == IDYES)
	{        Sleep(200);
             if (pDoc->ConnectSocket()) return false;
		}
    return false;
}


⌨️ 快捷键说明

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