#pragma once #pragma comment(lib,"Ws2_32.lib") #include <winsock2.h> #define BUFFER_SIZE 1024 #define IP_ADDRESS_BUFFER_LEN 15 class CSeverMessage { public: CSeverMessage(){}; public: virtual void OnError(char* pBuffer)=0; virtual void OnRecv(char* pBuffer,int iLen)=0; virtual void OnConnect(char* ichAddress,int iPort)=0; }; class CServer { public: CServer(int iPort,CSeverMessage *pMessagePor); ~CServer(void); int SendData(char* ch,int len); bool IsConnect(){return m_bIsConnect;} char* GetClientAddress(){return m_chClientIPAddress;} int GetPort(){return m_iPort;} private: void PreServer(); void CreateSever(); void SetErrorMessage(char *ch); static DWORD WINAPI LissionProc(LPVOID lpParameter); DWORD OnLission(); static DWORD WINAPI RecvProc(LPVOID lpParameter); DWORD OnRecv(); private: int m_iPort; char m_chClientIPAddress[IP_ADDRESS_BUFFER_LEN]; SOCKET m_socket; SOCKET m_clientSocket; char m_chRecvBuffer[BUFFER_SIZE]; CSeverMessage *MessagePor; bool m_bIsAccept; bool m_bIsConnect; bool m_bExit; };
#include "StdAfx.h" #include "Server.h" #include <time.h> CServer::CServer(int iPort,CSeverMessage* pMessagePor): MessagePor(pMessagePor), m_bIsAccept(false), m_bIsConnect(false), m_bExit(false) { m_iPort=iPort; CreateSever(); } CServer::~CServer(void) { m_bIsConnect=false; m_bExit=true; Sleep(100); } void CServer::PreServer() { memset(m_chRecvBuffer,0,BUFFER_SIZE); memset(m_chClientIPAddress,0,IP_ADDRESS_BUFFER_LEN); WSACleanup(); } void CServer::CreateSever() { PreServer(); SetErrorMessage("开始连接...\n"); WORD wVersionRequested; WSADATA wsaData ; int retVal; wVersionRequested = MAKEWORD(1,1); retVal = WSAStartup(wVersionRequested,&wsaData); if (0!=retVal) { SetErrorMessage("连接失败!位置:WSAStartup(),未找到socket dll!\n"); return; } if (LOBYTE(wsaData.wVersion)!=1 || HIBYTE(wsaData.wVersion)!=1) { SetErrorMessage("连接失败!位置:WSAStartup(),不支持这个版本的socket dll!\n"); goto Loop; } m_socket=socket(AF_INET,SOCK_STREAM,0); if (INVALID_SOCKET==m_socket) { SetErrorMessage("连接失败!位置:socket()!\n"); goto Loop; } SOCKADDR_IN addrServ; addrServ.sin_family = AF_INET; addrServ.sin_port = htons(m_iPort); addrServ.sin_addr.s_addr =INADDR_ANY; retVal = bind(m_socket,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN)); if (SOCKET_ERROR == retVal) { SetErrorMessage("连接失败!位置:bind()!\n"); closesocket(m_socket); goto Loop; } listen(m_socket,1); if(::CreateThread(NULL,0,LissionProc,this,0,NULL)==0) { SetErrorMessage("建立连接线程失败!\n"); goto Loop; } return; Loop: WSACleanup(); } void CServer::SetErrorMessage(char *ch) { if(MessagePor!=NULL) { MessagePor->OnError(ch); } } DWORD CServer::LissionProc(LPVOID lpParameter) { return ((CServer*)lpParameter)->OnLission(); } DWORD CServer::OnLission() { int times=0; time_t tStart=time(NULL); time_t tEnd; SetErrorMessage("等待连接...\n"); sockaddr_in addrClient; int addrClientlen = sizeof(addrClient); m_bIsAccept=true; while (1) { if(m_bExit) { break; } tEnd=time(NULL); m_clientSocket = accept(m_socket,(sockaddr FAR*)&addrClient,&addrClientlen); if (INVALID_SOCKET != m_clientSocket) { char *pClientIP = inet_ntoa(addrClient.sin_addr); memcpy(m_chClientIPAddress,pClientIP,strlen(pClientIP)); char ch[100]={0}; if(MessagePor!=NULL) { MessagePor->OnConnect(pClientIP,m_iPort); } m_bIsConnect=true; CreateThread(NULL,0,RecvProc,this,0,NULL); break; } Sleep(10); if(tEnd-tStart>600) { SetErrorMessage("未监听到客户端!\n"); break; } } m_bIsAccept=false; return 0; } DWORD CServer::RecvProc(LPVOID lpParameter) { return ((CServer*)lpParameter)->OnRecv(); } DWORD CServer::OnRecv() { int len; WSAEVENT hEvent = WSACreateEvent(); if( WSAEventSelect( m_clientSocket , hEvent , FD_READ) != 0 ) { SetErrorMessage("连接失败!位置:WSAEventSelect()!\n"); return 0; } WSANETWORKEVENTS networkEvents; while(1) { if(m_bExit) { break; } if(!m_bIsConnect) { break; } DWORD index = WSAWaitForMultipleEvents(5 , &hEvent , FALSE , WSA_INFINITE , FALSE); int EventType = WSAEnumNetworkEvents(m_clientSocket , hEvent , &networkEvents ); if(networkEvents.lNetworkEvents & FD_READ ) { len=recv(m_clientSocket, m_chRecvBuffer,BUFFER_SIZE , 0 )>0; if(len>0) { if(MessagePor!=NULL) { MessagePor->OnRecv(m_chRecvBuffer,len); } } } Sleep(1); } return 0; } int CServer::SendData(char* ch,int len) { if(!m_bIsConnect) { return -1; } int sendlen=send(m_clientSocket,ch,len,0); printf("%d\n",sendlen); if(sendlen==-1) { m_bIsConnect=false; SetErrorMessage("发送失败!\n"); if(!m_bIsAccept) { if(::CreateThread(NULL,0,LissionProc,this,0,NULL)==0) { SetErrorMessage("建立连接线程失败!\n"); } } } return sendlen; }
#pragma once #include "Server.h" class CContrilComputer:public CSeverMessage { public: CContrilComputer(void); ~CContrilComputer(void); void OnError(char* pBuffer); void OnRecv(char* pBuffer,int iLen); void OnConnect(char* ichAddress,int iPort); private: void doMission(int type,int wParam,int lParam); private: CServer *m_pServer; };
#include "StdAfx.h" #include "ContrilComputer.h" #include <Windows.h> CContrilComputer::CContrilComputer(void) { m_pServer=new CServer(10001,this); } CContrilComputer::~CContrilComputer(void) { delete m_pServer; } void CContrilComputer::OnError(char* pBuffer) { } void CContrilComputer::OnRecv(char* pBuffer,int iLen) { int wParam,lParam; int type; type=int(*pBuffer); memcpy(&wParam,pBuffer+1,4); memcpy(&lParam,pBuffer+5,4); doMission(type,wParam,lParam); } void CContrilComputer::OnConnect(char* ichAddress,int iPort) { } void CContrilComputer::doMission(int type,int wParam,int lParam) { switch(type) { case 1: ::SetCursorPos(wParam,lParam); break; case 2: ::SetCursorPos(wParam,lParam); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); break; case 3: ::SetCursorPos(wParam,lParam); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); break; case 4: ::SetCursorPos(wParam,lParam); mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0); break; case 5: ::SetCursorPos(wParam,lParam); mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0); break; case 6: keybd_event(wParam,0,0,0 keybd_event(wParam,0,KEYEVENTF_KEYUP,0); break; } }