// ClientInit.cpp: implementation of the CClientInit class.
#include "stdafx.h"
#include "lanpcwatch.h"
#include "ClientInit.h"
#include "Lanpcwatchdlg.h"
#include "Prowarning.h"
#include <winsock.h>#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endifextern CLanpcwatchApp theApp;
CRemoteScreen src;
CClientInit::CClientInit(){m_nCommPort=6578; //初始化命令等待端口
m_nVideoPort=6583; //初始化视频端口
m_bConnect=FALSE; //初始化连接标志
Title="局域网远程监控系统";
RemotePcOntimeShutdown="FALSE";
RemoteCtrlCheckBox="000";
srvwatch=FALSE;
AutoKillProcess=FALSE;
BalloonTips=FALSE;
m_bEndThreadVideo=FALSE;
}CClientInit::~CClientInit()
{
if(m_socketConnectForComm!=NULL)
closesocket(m_socketConnectForComm);
}BOOL CClientInit::InitSocket(SOCKET &socketForConnect, int nPort)
{ WORD wVersionRequested = MAKEWORD(2,0);
WSADATA wsaData;
int nErr;
//调用WSAStartup()
nErr = WSAStartup(wVersionRequested, &wsaData);
if(nErr != 0)
{
AfxMessageBox("调用WSAStarup()出错");
return false;
}
//调用socket()
socketForConnect = socket(AF_INET, SOCK_STREAM, 0);
if(socketForConnect == INVALID_SOCKET)
{
AfxMessageBox("调用socket()出错");
return false;
}
//调用connect()
//初始化服务器地址
m_AddrServer.sin_family = AF_INET;
m_AddrServer.sin_addr.s_addr = inet_addr(m_strServerIpAddr);
m_AddrServer.sin_port = htons(nPort);
//连接服务器
nErr = connect(socketForConnect, (sockaddr *)&m_AddrServer, sizeof(m_AddrServer));
if(nErr != 0)
{
AfxMessageBox("调用connect()出错,服务端没有响应");
return false;
} return true;}void CClientInit::RecvMsg()
{//接收服务端返回的信息
char temp[10000];
int nRecv=recv(m_socketConnectForComm,temp,10000,0);
if (nRecv==0||nRecv==SOCKET_ERROR)
{
AfxMessageBox("接收远程信息失败,与远程计算机的连接可能已断开!");
m_bConnect=FALSE;
return ;
}
char* pMsg=new char[nRecv];
strncpy(pMsg,temp,nRecv);
strMsg=(CString)pMsg;
int nPos=strMsg.Find('|');
int nPos2=strMsg.Find('&');
strMsg=strMsg.Left(nPos);
//命令辨别 if (strMsg=="CONNECT_OK")
{

m_bConnect=TRUE;
CString strTemp=pMsg;
CString temp;
temp=strTemp.Right(strTemp.GetLength()-nPos-1);
RemotePcOntimeShutdown=temp.Left(5);
temp=strTemp.Right(strTemp.GetLength()-nPos2-1);
RemoteCtrlCheckBox=temp.Left(3);
HWND   m_hWnd=::FindWindow(NULL,Title);
SetTimer(m_hWnd,1,500,NULL);
::SendMessage(m_hWnd,WM_EnableBtn,NULL,NULL);
AfxMessageBox("连接成功!",MB_ICONASTERISK);
Title.Format("局域网远程监控系统-%s已连接",m_strServerIpAddr);
SetWindowText(m_hWnd,Title);
}
if (strMsg=="SERVER_EXIT")
{
m_bConnect=FALSE;
} if (strMsg=="SECCESSKILL")
{
AfxMessageBox("成功结束进程!",MB_ICONASTERISK);
HWND   m_hWnd=::FindWindow(NULL,Title);
::SendMessage(m_hWnd,WM_RENEWprocess,NULL,NULL); }
if (strMsg=="UNKILL")
{
AfxMessageBox("无法结束进程,可能为系统关键进程!"); } if (strMsg=="OP_SECCESS") //关闭服务器计算机
{
m_bConnect=FALSE;
AfxMessageBox("操作已成功!",MB_ICONASTERISK);
}
if (strMsg=="ONTIME_SECCESS")
{
AfxMessageBox("成功设置远程定时关机!",MB_ICONASTERISK);
RemotePcOntimeShutdown="TRUEE";
}
if (strMsg=="CANCEL_ONTIMEDOWN_SECCESS")
{
AfxMessageBox("成功取消远程定时关机!",MB_ICONASTERISK);
RemotePcOntimeShutdown="FALSE";
}
if (strMsg=="TELNET_RETURN")
{
CString strTemp=pMsg;
CString temp;
temp=strTemp.Right(strTemp.GetLength()-nPos-1);
HWND   m_hWnd=::FindWindow(NULL,"远程命令行");
SetDlgItemText(m_hWnd,IDC_RETURN,temp);
}
delete []pMsg;
pMsg=NULL;
}
UINT ThreadFunVideo(LPVOID pParam)
{
CClientInit* pClient=(CClientInit*)pParam;
while (1)
{
if(pClient->m_bEndThreadVideo) //结束传输
AfxEndThread(2);
//先接收位图结构
char bmpBuffer[24]; //位图结构占24个字节
int nRecv=recv(pClient->m_socketConnectForVideo,bmpBuffer,sizeof(BITMAP),0);
BITMAP* pBitmap=(BITMAP*)bmpBuffer; //如果接收有误,跳过错误继续接收
if(pBitmap==NULL)
continue;
//填充位图结构信息
pClient->m_bmpBit.bmBits = pBitmap->bmBits;
pClient->m_bmpBit.bmBitsPixel = pBitmap->bmBitsPixel;
pClient->m_bmpBit.bmHeight = pBitmap->bmHeight;
pClient->m_bmpBit.bmPlanes = pBitmap->bmPlanes;
pClient->m_bmpBit.bmType = pBitmap->bmType;
pClient->m_bmpBit.bmWidth = pBitmap->bmWidth;
pClient->m_bmpBit.bmWidthBytes = pBitmap->bmWidthBytes; //获得位图数据
int size=(pClient->m_bmpBit.bmWidthBytes) * (pClient->m_bmpBit.bmHeight);
pClient->m_pBmpData=new char[size];
if (pClient->m_pBmpData==NULL)
{//分配内存失败
AfxMessageBox("分配图像数据内存失败!");
return 0;
}
char* pch=pClient->m_pBmpData;
int nByteRecv=0;
int nByteThisTime=0;
do //循环接收图像数据 
{
if(pClient->m_bEndThreadVideo)
break;
nByteThisTime=recv(pClient->m_socketConnectForVideo,pch,size-nByteRecv,0);
nByteRecv+=nByteThisTime;
pch+=nByteThisTime; } while(nByteRecv<size);
HWND   m_hWnd=::FindWindow(NULL,"远程屏幕");
::SendMessage(m_hWnd,WM_ShowBmp,NULL,NULL);
}
return 1;
}BOOL CClientInit::ConnectServer(CString strIP)
{
//准备连接服务器
m_strServerIpAddr=strIP;
if (!InitSocket(m_socketConnectForComm,m_nCommPort)) 
{
AfxMessageBox("初始化Socket失败");
return FALSE;
}
RecvMsg();
//初始化主界面信息 SendMsg("GETPROCESSINFO|");
ProcessInfoInit();
SendMsg("GETPCINFO|");
PCInfoInit();
SendMsg("GETDISKINFO|");
DiskInfoInit();
SendMsg("GETSOFTINFO|");
SoftInfoInit();
return TRUE;
}void CClientInit::SendMsg(CString temp)
{//向服务器发送指令
m_strClientMsg=temp;
send(m_socketConnectForComm,m_strClientMsg,m_strClientMsg.GetLength(),0);
}BOOL CClientInit::SoftInfoInit()
{   
HWND   m_hWnd=::FindWindow(NULL,Title); //获取主窗口句柄  
SOFTINFO temp;
int nRecv;
//循环接收安装软件信息
do
{
nRecv=recv(m_socketConnectForComm,(char*)&temp,sizeof(temp),0);
if (nRecv==0||nRecv==SOCKET_ERROR)
{
AfxMessageBox("接收信息失败!");
m_bConnect=FALSE;
return FALSE;
}
::SendMessage(m_hWnd,WM_RECVsoft,(WPARAM)temp.softname,(LPARAM)temp.soft);//向主窗口软件列表框发送更新消息
}while(strcmp(temp.softname,""));

return TRUE;
}

解决方案 »

  1.   

    void CClientInit::SendMsg(CString temp)
    {//向服务器发送指令
    m_strClientMsg=temp;
    send(m_socketConnectForComm,m_strClientMsg,m_strClientMsg.GetLength(),0);
    }BOOL CClientInit::SoftInfoInit()
    {   
    HWND   m_hWnd=::FindWindow(NULL,Title); //获取主窗口句柄  
    SOFTINFO temp;
    int nRecv;
    //循环接收安装软件信息
    do
    {
    nRecv=recv(m_socketConnectForComm,(char*)&temp,sizeof(temp),0);
    if (nRecv==0||nRecv==SOCKET_ERROR)
    {
    AfxMessageBox("接收信息失败!");
    m_bConnect=FALSE;
    return FALSE;
    }
    ::SendMessage(m_hWnd,WM_RECVsoft,(WPARAM)temp.softname,(LPARAM)temp.soft);//向主窗口软件列表框发送更新消息
    }while(strcmp(temp.softname,""));

    return TRUE;
    }BOOL CClientInit::ProcessInfoInit()
    {   
    HWND   m_hWnd=::FindWindow(NULL,Title); //获取主窗口句柄  
    PROCESSINFO temp;
    CString str; int nRecv;
    //循环接收进程信息
    do
    {
    nRecv=recv(m_socketConnectForComm,(char*)&temp,sizeof(temp),0);
    if (nRecv==0||nRecv==SOCKET_ERROR)
    {
    AfxMessageBox("接收信息失败!");
    m_bConnect=FALSE;
    return FALSE;
    }
    if(srvwatch==TRUE)
    {
    str.Format("%s",temp.process);
    for(int i=0;i<=m_prolistcount-1;i++)
    if(str.Find(m_strprowatch[i])!=-1)
    {
    caption.Format("警告:包含关键字'%s'进程被发现运行在服务端!",m_strprowatch[i]);
    if(BalloonTips==FALSE)
    {
    CProwarning *ProwarnDlg=new CProwarning();
    ProwarnDlg->Create(IDD_PROWARNING);
    ProwarnDlg->ShowWindow(SW_SHOW);
    ProwarnDlg->SetForegroundWindow( );
    }
    else
    ::SendMessage(m_hWnd,WM_BALLOONTIPS,(WPARAM)&caption,NULL);
    m_strprowatch[i]=m_strprowatch[i]+"(已经提示)";
    if(AutoKillProcess==TRUE)
    {
    SendMsg("KILLPROCESS|"+(CString)temp.processid);
    m_strprowatch[i]=m_strprowatch[i]+"(已成功结束)";
    }
    }
    }
    ::SendMessage(m_hWnd,WM_RECVprocess,(WPARAM)temp.processid,(LPARAM)temp.process);//向主窗口进程列表框发送更新消息
    }while(strcmp(temp.processid,""));return TRUE;
    }BOOL CClientInit::PCInfoInit()
    {
    HWND   m_hWnd=::FindWindow(NULL,Title); //获取主窗口句柄  
    PCINFO temp;
    int nRecv;
    nRecv=recv(m_socketConnectForComm,(char*)&temp,sizeof(temp),0);
    if (nRecv==0||nRecv==SOCKET_ERROR)
    {
    AfxMessageBox("接收信息失败!");
    m_bConnect=FALSE;
    return FALSE;
    }
    ::SendMessage(m_hWnd,WM_RECVpcinfo,(WPARAM)&temp,0);//向主窗口发送列表框更新消息return TRUE;
    }BOOL CClientInit::DiskInfoInit()
    { HWND   m_hWnd=::FindWindow(NULL,Title); //获取主窗口句柄  
    DISKINFO temp;
    int nRecv,count=0;
    do
    {
    nRecv=recv(m_socketConnectForComm,(char*)&temp,sizeof(temp),0);
    if (nRecv==0||nRecv==SOCKET_ERROR)
    {
    AfxMessageBox("接收信息失败!");
    m_bConnect=FALSE;
    return FALSE;
    }
    if(!strcmp(temp.diskid,"end"))break;
    ::SendMessage(m_hWnd,WM_RECVdiskinfo,(WPARAM)&temp,(LPARAM)count);//向主窗口磁盘信息列表框发送更新消息
    count++;
    }while(1);

    return TRUE;
    }
    void CClientInit::StartRecvVideo()
    {

    InitSocket(m_socketConnectForVideo,m_nVideoPort);
    CWinThread* pThread;
    pThread=AfxBeginThread(ThreadFunVideo,this);
    }UINT ThreadScan(LPVOID pParam){
    CClientInit* pServer=(CClientInit*)pParam;
    int i=0;
    CString tempip;
    WORD wVersionRequested = MAKEWORD(2,0);
    WSADATA wsaData;
    int nErr;
    //调用WSAStartup()
    nErr = WSAStartup(wVersionRequested, &wsaData);
    if(nErr != 0)
    {
    AfxMessageBox("调用WSAStarup()出错");
    return 0;
    }
    for(pServer->currentip=1;pServer->currentip<255;pServer->currentip++)
    {
    HWND   m_hWnd=::FindWindow(NULL,"服务端扫描....");
    SendMessage(m_hWnd,WM_UPDATEPROGRESS, (WPARAM)pServer->currentip,NULL);
    tempip.Format("%s.%d",pServer->locaIPSection,pServer->currentip);
    pServer->m_socketConnectForComm = socket(AF_INET, SOCK_STREAM, 0);
    if(pServer->m_socketConnectForComm == INVALID_SOCKET)
    {
    AfxMessageBox("调用socket()出错");
    return 0;
    }
    unsigned   long ul=1;   
        int ret=ioctlsocket(pServer->m_socketConnectForComm,   FIONBIO,   (unsigned   long*)&ul);  
        if(ret==SOCKET_ERROR)
    {
    AfxMessageBox("设置非阻塞模式失败");
        return 0;
    }
    pServer->m_AddrServer.sin_family = AF_INET;
    pServer->m_AddrServer.sin_addr.s_addr = inet_addr(tempip);
    pServer->m_AddrServer.sin_port = htons(pServer->m_nCommPort); nErr = connect(pServer->m_socketConnectForComm, (sockaddr *)&pServer->m_AddrServer, sizeof(pServer->m_AddrServer));
    Sleep(15);
    struct   timeval   timeout   ;   
    fd_set   r;   
    FD_ZERO(&r);   
        FD_SET(pServer->m_socketConnectForComm, &r);   
        timeout.tv_sec  = 0;      
        timeout.tv_usec =10000; //非阻塞模式超时时间500微秒

    ret=select(0,0,&r,0,&timeout);   
        if (ret<=0)   
    {
    closesocket(pServer->m_socketConnectForComm);
    continue;//连接超时,选择下一个IP连接
    }
    else
    {//连接成功,将IP添加到列表,同时接收主机名
    char temp[10000];
    int nRecv=recv(pServer->m_socketConnectForComm,temp,10000,0);
    if (nRecv==0||nRecv==SOCKET_ERROR)
    {
    AfxMessageBox("接收远程信息失败!");
    return 0;
    }
    char* pMsg=new char[nRecv];
    strncpy(pMsg,temp,nRecv);
    pServer->strMsg=(CString)pMsg;
    int nPos=pServer->strMsg.Find('|');
    int nPos2=pServer->strMsg.Find('*');

    pServer->strMsg=pServer->strMsg.Left(nPos);
    //命令辨别
    if (pServer->strMsg=="CONNECT_OK")
    {
    CString strTemp=pMsg;
    CString tempname;
    tempname=strTemp.Right(strTemp.GetLength()-nPos2-1);
    int nPos3=tempname.Find('@');
    pServer->HostInfo.hostname=tempname.Left(nPos3);
    pServer->HostInfo.ipaddress=tempip;
    }
    HWND   m_hWnd=::FindWindow(NULL,"连接...."); //获取主窗口句柄  
    ::SendMessage(m_hWnd,WM_IPLISTADDITEM,(WPARAM)&pServer->HostInfo,NULL);
    pServer->HostInfos[i].hostname=pServer->HostInfo.hostname;
    pServer->HostInfos[i].ipaddress=pServer->HostInfo.ipaddress;
    i++;
    pServer->SendMsg("EXIT|");
    }
     closesocket(pServer->m_socketConnectForComm);
      }   
    AfxEndThread(2);
    return 0;}void CClientInit::ScanForSrv()
    {//启动扫描局域网线程
    CWinThread* pThread;
    pThread=AfxBeginThread(ThreadScan,this);}
      

  2.   

    运行不了你跟踪一下,在App类的InitInstance中放个断点,看是不是异常退出了。
      

  3.   

    CXX0017: CXX0014: Error: missing operand
    temp.softname CXX0017: Error: symbol "temp" not found
    调试时出现这两句,高手能解释下嘛?
      

  4.   

    char temp[10000];CString temp;你定义了两个变量都叫temp?????
      

  5.   

    的确这个函数中 void CClientInit::RecvMsg()
    temp 重复定义了