thread1.cpp
资源名称:anywhere.zip [点击查看]
上传用户:zhenhuadz
上传日期:2007-01-07
资源大小:1511k
文件大小:17k
源码类别:
远程控制编程
开发平台:
Visual C++
- // thread1.cpp: implementation of the thread class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "anywhere.h"
- #include "thread1.h"
- #include "blocksock.h"
- //#include "msgman.h"
- //#include "mainfrm.h"
- //#include "thread.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- //#define timeout 15
- //#define request 400
- HWND m_hMainWnd;
- HANDLE hRecv,hSend;//接受,发送两个线程的句柄
- HANDLE recvevent,sendevent;
- CCriticalSection cs;
- Link linkrc;
- int timeout,request;
- CString pix="800*600";
- void LogBlockingSocketException(LPVOID pParam, char* pch, CBlockingSocketException* pe)
- { // pParam holds the HWND for the destination window (in another thread)
- /* CString strGmt = CTime::GetCurrentTime().FormatGmt("%m/%d/%y %H:%M:%S GMT");
- char text1[200], text2[50];
- pe->GetErrorMessage(text2, 49);
- wsprintf(text1, "WINSOCK ERROR--%s %s -- %srn", pch, text2, (const char*) strGmt);
- ::SendMessage((HWND) pParam, EM_SETSEL, (WPARAM) 65534, 65535);
- ::SendMessage((HWND) pParam, EM_REPLACESEL, (WPARAM) 0, (LPARAM) text1);
- */
- MessageBox(NULL,pch,NULL,MB_OK);
- }
- UINT Connect(LPVOID pParam)
- {
- Link *plinkrc;
- plinkrc=(Link*)pParam;
- CBlockingSocket saClient;
- char connect[17];
- char assume[17];
- cs.Lock();
- strcpy(assume,plinkrc->username);
- strcat(assume,plinkrc->password);
- //strcpy(szsBuf,plinkrc->sbuf);
- CSockAddr sa=plinkrc->sAddr;
- cs.Unlock();//将待发消息传给变量
- connect[16]=0;
- assume[16]=0;
- //CBlockingSocket sockClient;
- try {
- saClient.Create();
- saClient.Connect(sa);
- if (!plinkrc->bConnected) {
- saClient.Write(assume,strlen(assume),timeout);
- saClient.Receive(connect,16,timeout);
- if (strcmp(assume,connect)) {
- MessageBox(m_hMainWnd,"不正确的用户或密码,被服务端拒绝服务","提示:",MB_OK);
- ExitThread(0);//不等退出
- }//认证过程
- else plinkrc->bConnected=TRUE;
- }
- cs.Lock();
- plinkrc->bConnected=TRUE;
- cs.Unlock();
- saClient.Close();
- return 1;
- }
- catch (CBlockingSocketException *e) {
- MessageBox(NULL,"连接错误!","提示",MB_OK);
- e->Delete();
- saClient.Close();
- return 0;
- //cs.Unlock();
- }
- //sockClient.Close();
- }
- DWORD WINAPI SysThread(LPVOID pParam)
- {
- WaitForSingleObject(sendevent,INFINITE);//无限阻塞
- //自动重设,使信号实效
- Link *plinkrc;
- plinkrc=(Link *)pParam;
- char szsBuf[1024];//待发字串
- //char connect[17]; //认证
- //char assume[17];
- //LPTSTR szrBuf;
- //int *iLength;
- //iLength= new int;//堆,这里new的对象共计有:ilength,szrbuf
- HWND hWndThread;
- CBlockingSocket saClient;
- cs.Lock();
- //strcpy(assume,plinkrc->username);
- //strcat(assume,plinkrc->password);
- strcpy(szsBuf,plinkrc->sbuf);
- CSockAddr sa=plinkrc->sAddr;
- hWndThread=plinkrc->m_hWnd;
- cs.Unlock();//将待发消息传给变量
- //connect[16]=0;
- //assume[16]=0;
- try {
- saClient.Create();
- saClient.Connect(sa);
- int tmplength=strlen(szsBuf);
- saClient.Write((char *)&tmplength,4,timeout);
- saClient.Write(szsBuf,strlen(szsBuf),timeout);//命令
- if ((!strcmp(szsBuf,"RESTART*")) || (!strcmp(szsBuf,"SHUTDOWN")))
- linkrc.bConnected=FALSE;
- SetEvent(sendevent);//设置可接受信息长度,生效
- /*saClient.Receive((char *)iLength,4,request);
- //SetEvent(recvevent);
- WaitForSingleObject(recvevent,INFINITE);
- //失效
- szrBuf=new char[*iLength+1];//应该判断
- saClient.Receive(szrBuf,*iLength,timeout);
- szrBuf[*iLength]=0;
- CString text=szrBuf;
- // WaitForSingleObject(recvevent,INFINITE);
- ::SendMessage(hWndThread,WM_SETACTIVE,1,(DWORD)&text);
- //ResetEvent(recvevent);//重置信号//发送消息给窗口
- //会等待sendmessage返回
- //必须在适当时候删除szrbuf
- saClient.Close();//Cleanup();
- SetEvent(recvevent);//设置可接受信息
- delete iLength;
- delete szrBuf;
- //cs.Lock();
- //strcpy(plinkrc->rbuf,szrBuf);
- //cs.Unlock();
- */
- return 1;//成功
- }
- catch (CBlockingSocketException *e) {
- CString sErrmsg;
- sErrmsg.LoadString(IDS_ERROR_RECEIVE);
- MessageBox(hWndThread,sErrmsg,"提示",MB_OK);
- e->Delete();
- saClient.Close();//leanup();
- SetEvent(sendevent);
- ::SendMessage(hWndThread,WM_SETACTIVE,0,(DWORD)&sErrmsg);
- return 0;
- }
- }
- DWORD WINAPI SendThread(LPVOID pParam)
- {
- WaitForSingleObject(sendevent,INFINITE);//无限阻塞
- //自动重设,使信号实效
- Link *plinkrc;
- plinkrc=(Link *)pParam;
- char szsBuf[1024];//待发字串
- char connect[17]; //认证
- char assume[17];
- LPTSTR szrBuf=NULL;
- int *iLength=NULL;
- iLength= new int;//堆,这里new的对象共计有:ilength,szrbuf
- HWND hWndThread;
- CBlockingSocket saClient;
- cs.Lock();
- strcpy(assume,plinkrc->username);
- strcat(assume,plinkrc->password);
- strcpy(szsBuf,plinkrc->sbuf);
- CSockAddr sa=plinkrc->sAddr;
- hWndThread=plinkrc->m_hWnd;
- cs.Unlock();//将待发消息传给变量
- connect[16]=0;
- assume[16]=0;
- try {
- saClient.Create();
- saClient.Connect(sa);
- if (!plinkrc->bConnected) {
- saClient.Write(assume,16,timeout);
- saClient.Receive(connect,16,timeout);
- if (strcmp(assume,connect)) {
- CString sErrmsg,tmp2;
- sErrmsg.LoadString(IDS_ERROR_USER);
- tmp2.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,tmp2,MB_OK);
- saClient.Close();
- ExitThread(0);//不等退出
- }//认证过程
- else plinkrc->bConnected=TRUE;
- }
- int tmplength=strlen(szsBuf);
- saClient.Write((char *)&tmplength,4,timeout);
- saClient.Write(szsBuf,strlen(szsBuf),timeout);//命令
- saClient.Receive((char *)iLength,4,request);
- SetEvent(sendevent);//设置可接受信息长度,生效
- //SetEvent(recvevent);
- WaitForSingleObject(recvevent,INFINITE);
- //失效
- if ((*iLength<0) || (*iLength>10000))
- throw new CBlockingSocketException("Receive");
- szrBuf=new char[*iLength+1];//应该判断
- saClient.Receive(szrBuf,*iLength,timeout);
- szrBuf[*iLength]=0;
- CString text=szrBuf;
- // WaitForSingleObject(recvevent,INFINITE);
- ::SendMessage(hWndThread,WM_SETACTIVE,1,(DWORD)&text);
- //ResetEvent(recvevent);//重置信号//发送消息给窗口
- //会等待sendmessage返回
- //必须在适当时候删除szrbuf
- saClient.Close();//Cleanup();
- SetEvent(recvevent);//设置可接受信息
- if (iLength!=NULL) delete iLength;
- if (szrBuf!=NULL) delete szrBuf;
- return 1;//成功
- }
- catch (CBlockingSocketException *e) {
- CString sErrmsg;
- sErrmsg.LoadString(IDS_ERROR_RECEIVE);
- MessageBox(hWndThread,sErrmsg,"提示",MB_OK);
- e->Delete();
- saClient.Cleanup();
- SetEvent(sendevent);
- SetEvent(recvevent);//设置事件可用
- ::SendMessage(hWndThread,WM_SETACTIVE,0,(DWORD)&sErrmsg);
- linkrc.bConnected=FALSE;
- //失败
- if (iLength!=NULL)
- delete iLength;
- if (szrBuf!=NULL)
- delete szrBuf;
- return 0;
- }
- }
- DWORD WINAPI ViewScr(LPVOID pParam)
- {
- Link *plinkrc;
- plinkrc=(Link*)pParam;
- char szsBuf[1025];//定义发送字串
- char *data=NULL;
- char *tmp=NULL;
- data=new char[1024];
- int *iLength=NULL;
- LPTSTR szrBuf=NULL;
- iLength= new int;//堆
- HWND hWndThread;
- memset(szsBuf,0,sizeof(szsBuf));
- CBlockingSocket saClient;
- CBlockingSocket sockClient;
- cs.Lock();
- strcpy(szsBuf,plinkrc->sbuf);
- CSockAddr sa=plinkrc->sAddr;
- CSockAddr saa=CSockAddr(plinkrc->ip,plinkrc->viewport);//temp截图
- hWndThread=plinkrc->m_hWnd;
- cs.Unlock();//临界区
- try {
- saClient.Create();
- saClient.Connect(sa);
- int order_length=8;
- saClient.Write((char *)&order_length,4,timeout);
- saClient.Write(szsBuf,strlen(szsBuf),timeout);
- saClient.Receive((char *)iLength,4,request);
- szrBuf=new char[*iLength+1];
- saClient.Receive(szrBuf,*iLength,timeout);
- szrBuf[*iLength]=0;
- //是否需等待????
- if (strcmp(szsBuf,szrBuf)) {//为真则不相等
- CString sErrmsg,caption;
- sErrmsg.LoadString(IDS_SERVER_FAILED);//无法响应
- caption.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,caption,MB_OK);
- saClient.Close();
- if (szrBuf!=NULL) delete szrBuf;
- if (iLength!=NULL) delete iLength;
- ExitThread(0);//不等退出
- }
- saClient.Close();
- sockClient.Create();
- sockClient.Connect(saa);
- int *pLength;
- char cLength[4];
- sockClient.Receive(cLength, 4,timeout);
- pLength=(int *)cLength;
- int dataLength=*pLength;
- char tmppath[MAX_PATH];
- char tmpname[MAX_PATH];
- GetTempPath(MAX_PATH,tmppath);
- GetTempFileName(tmppath,"any",0,tmpname);
- //获得一个临时文件名
- CFile destFile(tmpname, CFile::modeCreate| CFile::modeWrite| CFile::typeBinary);
- int loopnum=dataLength/1024;
- int rest=dataLength-loopnum*1024;
- int nBytesRecv,nBytesThisTime;;
- for (int i=0;i<loopnum;i++)
- {
- nBytesRecv=0;
- tmp=data;
- do {
- nBytesThisTime=sockClient.Receive(data,1024-nBytesRecv,timeout);
- nBytesRecv+=nBytesThisTime;
- data+=nBytesThisTime;
- } while (nBytesRecv<1024);
- destFile.Write(tmp,1024);
- data=tmp;
- }
- sockClient.Receive(data,rest,timeout);
- destFile.Write(data,rest);
- destFile.Flush();
- destFile.Close();
- sockClient.Close();
- if (iLength!=NULL) delete iLength;
- if (szrBuf!=NULL) delete szrBuf;
- if (data!=NULL) delete data;
- CString text="VIEWSCR*";
- text+=tmpname;
- //strcpy(szrBuf,"VIEWSCR*");
- //strcat(szrBuf,tmpname);//将命令和文件名发给主窗口
- ::SendMessage(hWndThread,WM_SETACTIVE,1,(DWORD)&text);
- return 1;
- }
- catch (CBlockingSocketException *e) {
- CString sErrmsg,caption;
- sErrmsg.LoadString(IDS_ERROR_RECEIVE);
- caption.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,caption,MB_OK);
- e->Delete();
- data=tmp;//add temp
- if (data!=NULL) delete data;
- if (szrBuf!=NULL) delete szrBuf;
- if (iLength!=NULL) delete iLength;
- sockClient.Cleanup();
- return 0;
- }
- }
- DWORD WINAPI GetFileThread(LPVOID pParam)
- {
- Link *plinkrc;
- plinkrc=(Link*)pParam;
- char szsBuf[1025];//定义发送字串
- char filename[MAX_PATH];
- char *data=NULL; //用于传输文件
- data=new char[1024];
- int *iLength=NULL;
- LPTSTR szrBuf=NULL;
- char *tmp=NULL;
- iLength= new int;//堆
- HWND hWndThread;
- memset(szsBuf,0,sizeof(szsBuf));
- memset(filename,0,sizeof(filename));
- CBlockingSocket saClient;
- CBlockingSocket sockClient;
- cs.Lock();
- strcpy(szsBuf,plinkrc->sbuf);
- strcpy(filename,plinkrc->rbuf);
- CSockAddr sa=plinkrc->sAddr;
- CSockAddr saa=CSockAddr(plinkrc->ip,plinkrc->getfileport);//temp截图
- hWndThread=plinkrc->m_hWnd;
- cs.Unlock();//临界区
- try {
- saClient.Create();
- saClient.Connect(sa);
- int order_length=strlen(szsBuf);
- saClient.Write((char *)&order_length,4,timeout);
- saClient.Write(szsBuf,strlen(szsBuf),timeout);
- saClient.Receive((char *)iLength,4,request);
- szrBuf=new char[*iLength+1];
- saClient.Receive(szrBuf,*iLength,timeout);
- szrBuf[*iLength]=0;
- //是否需等待????
- if (strncmp(szsBuf,szrBuf,8)) {//为真则不相等
- CString sErrmsg,caption;
- sErrmsg.LoadString(IDS_SERVER_FAILED);//无法响应
- caption.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,caption,MB_OK);
- saClient.Close();
- if (szrBuf!=NULL) delete szrBuf;
- ExitThread(0);//不等退出
- }
- saClient.Close();
- sockClient.Create();
- sockClient.Connect(saa);
- int *pLength;
- char cLength[4];
- sockClient.Receive(cLength, 4,timeout);
- pLength=(int *)cLength;
- int dataLength=*pLength;
- CFile destFile(filename, CFile::modeCreate| CFile::modeWrite| CFile::typeBinary);
- int loopnum=dataLength/1024;
- int rest=dataLength-loopnum*1024;
- int nBytesRecv,nBytesThisTime;;
- for (int i=0;i<loopnum;i++)
- {
- nBytesRecv=0;
- tmp=data;
- do {
- nBytesThisTime=sockClient.Receive(data,1024-nBytesRecv,timeout);
- nBytesRecv+=nBytesThisTime;
- data+=nBytesThisTime;
- } while (nBytesRecv<1024);
- destFile.Write(tmp,1024);
- data=tmp;
- }
- sockClient.Receive(data,rest,timeout);
- destFile.Write(data,rest);
- destFile.Flush();
- destFile.Close();
- sockClient.Close();
- if (data!=NULL) delete data;
- if (iLength!=NULL) delete iLength;
- if (szrBuf!=NULL) delete szrBuf;
- CString text="GETFILE*";
- text+=filename;//filename为下载到那个文件
- //将命令和文件名发给主窗口
- ::SendMessage(hWndThread,WM_SETACTIVE,1,(DWORD)&text);
- return 1;
- }
- catch (CBlockingSocketException *e) {
- CString sErrmsg,caption;
- sErrmsg.LoadString(IDS_ERROR_RECEIVE);
- caption.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,caption,MB_OK);
- e->Delete();
- CString text="GETFILE*";
- text+=filename;//filename为下载到那个文件
- ::SendMessage(hWndThread,WM_SETACTIVE,0,(DWORD)&text);
- data=tmp;
- if (data!=NULL) delete data;
- if (iLength!=NULL) delete iLength;
- if (szrBuf!=NULL) delete szrBuf;
- sockClient.Cleanup();
- return 0;
- }
- catch (CFileException *fe) {
- CString sErrmsg,caption;
- sErrmsg.LoadString(IDS_FILEWRITE_FAILED);
- caption.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,caption,MB_OK);
- fe->Delete;
- CString text="GETFILE*";
- text+=filename;//filename为下载到那个文件
- ::SendMessage(hWndThread,WM_SETACTIVE,0,(DWORD)&text);
- data=tmp;
- if (data!=NULL) delete data;
- if (iLength!=NULL) delete iLength;
- if (szrBuf!=NULL) delete szrBuf;
- return 0;
- }
- }
- DWORD WINAPI PutFileThread(LPVOID pParam)
- {
- Link *plinkrc;
- plinkrc=(Link*)pParam;
- char szsBuf[1025];//定义发送字串
- char filename[MAX_PATH];
- char *data=NULL; //用于传输文件
- data=new char[1024];
- int *iLength;
- LPTSTR szrBuf=NULL;
- iLength= new int;//堆
- HWND hWndThread;
- memset(szsBuf,0,sizeof(szsBuf));
- memset(filename,0,sizeof(filename));
- CBlockingSocket saClient;
- CBlockingSocket sockClient;
- cs.Lock();
- strcpy(szsBuf,plinkrc->sbuf);//发往客户端的命令
- strcpy(filename,plinkrc->rbuf);
- CSockAddr sa=plinkrc->sAddr;
- CSockAddr saa=CSockAddr(plinkrc->ip,plinkrc->putfileport);//temp截图
- hWndThread=plinkrc->m_hWnd;
- cs.Unlock();//临界区
- try {
- saClient.Create();
- saClient.Connect(sa);
- int order_length=strlen(szsBuf);
- saClient.Write((char *)&order_length,4,timeout);
- saClient.Write(szsBuf,strlen(szsBuf),timeout);
- saClient.Receive((char *)iLength,4,request);
- szrBuf=new char[*iLength+1];
- saClient.Receive(szrBuf,*iLength,timeout);
- szrBuf[*iLength]=0;
- //是否需等待????
- if (strncmp(szsBuf,szrBuf,8)) {//为真则不相等
- CString sErrmsg,caption;
- sErrmsg.LoadString(IDS_SERVER_FAILED);//无法响应
- caption.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,caption,MB_OK);
- saClient.Close();
- if (szrBuf!=NULL) delete szrBuf;
- ExitThread(0);//不等退出
- }
- saClient.Close();
- sockClient.Create();
- sockClient.Connect(saa);
- //int Length;
- //char cLength[4];
- CFile destFile(filename, CFile::modeRead|CFile::typeBinary);
- int dataLength=destFile.GetLength();
- sockClient.Write((char *)&dataLength,4,timeout);
- //pLength=(int *)cLength;
- //int dataLength=*pLength;
- int loopnum=dataLength/1024;
- int rest=dataLength-loopnum*1024;
- //int nBytesRecv,nBytesThisTime;;
- for (int i=0;i<loopnum;i++)
- {
- /*nBytesRecv=0;
- char *tmp=data;
- do {
- nBytesThisTime=sockClient.Receive(data,1024-nBytesRecv,timeout);
- nBytesRecv+=nBytesThisTime;
- data+=nBytesThisTime;
- } while (nBytesRecv<1024);
- */
- destFile.Read(data,1024);
- sockClient.Write(data,1024,timeout);
- //data=tmp;
- }
- destFile.Read(data,rest);
- sockClient.Write(data,rest,timeout);
- //destFile.Flush();
- destFile.Close();
- sockClient.Close();
- if (data!=NULL) delete data;
- if (iLength!=NULL) delete iLength;
- if (szrBuf!=NULL) delete szrBuf;
- //delete data;
- CString text=szsBuf; //为上传到那个文件
- //将命令和文件名发给主窗口
- ::SendMessage(hWndThread,WM_SETACTIVE,1,(DWORD)&text);
- return 1;
- }
- catch (CBlockingSocketException *e) {
- CString sErrmsg,caption;
- sErrmsg.LoadString(IDS_ERROR_RECEIVE);
- caption.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,caption,MB_OK);
- e->Delete();
- if (data!=NULL) delete data;
- if (iLength!=NULL) delete iLength;
- if (szrBuf!=NULL) delete szrBuf;
- sockClient.Cleanup();
- return 0;
- }
- /* catch (CFileException *fe) {
- CString sErrmsg,caption;
- sErrmsg.LoadString(IDS_FILEWRITE_FAILED);
- caption.LoadString(IDS_CAPTION);
- MessageBox(hWndThread,sErrmsg,caption,MB_OK);
- fe->Delete;
- }
- */
- }