Thread.cpp
资源名称:anywhere.zip [点击查看]
上传用户:zhenhuadz
上传日期:2007-01-07
资源大小:1511k
文件大小:8k
源码类别:
远程控制编程
开发平台:
Visual C++
- #include "stdafx.h"
- #include "Blocksock.h"
- #include "AnyServer.h"
- #include "AnyServerView.h"
- #include "ChatLsnSock.h"
- extern CBlockingSocket g_sListen;
- #define WAITTIME 10
- CCriticalSection CriticalSection;
- CCriticalSection CriticalData;
- BOOL beAssumed=FALSE;
- char *data;
- UINT Server(LPVOID lPvoid)
- {
- CAnyServerView* pViewWnd=(CAnyServerView*)lPvoid;
- if (g_sListen.m_hSocket!=NULL)
- {
- HWND hwnd=pViewWnd->GetSafeHwnd();
- CSockAddr saClient;
- CBlockingSocket sConnect;
- try
- {
- if (!g_sListen.Accept(sConnect,saClient))
- {
- throw new CBlockingSocketException("Accept Failed!");
- return 0;
- }
- CSockAddr sa;
- sConnect.GetPeerAddr(LPSOCKADDR(sa));
- ::SendMessage(hwnd,WM_IPADDR,(WPARAM)TRUE,sa.IPAddr());
- AfxBeginThread(Server,lPvoid);
- char identification[17];
- identification[16]=NULL;
- memset(identification,0,16);
- char* username=new char[9];
- memset(username,0,9);
- char* password=new char[9];
- memset(password,0,9);
- if (!beAssumed)
- {
- if (sConnect.Receive(identification,16,WAITTIME)==16)
- {
- strncpy(username,identification,8);
- strncpy(password,identification+8,8);
- CString id=AfxGetApp()->GetProfileString("AnyServer",username);
- pViewWnd->GetDlgItem(IDC_RESPONSE)->SetWindowText("客户端已经连接!");
- if (id==password)
- {
- beAssumed=TRUE;
- ::SendMessage(hwnd,WM_USERNAME,(WPARAM)beAssumed,(LPARAM)username);
- }
- else
- {
- sConnect.Close();
- ::SendMessage(hwnd,WM_COMMAND,MAKEWPARAM(ID_STOPSERVER,0),0);
- }
- }
- else
- throw new CBlockingSocketException("Identification Failed!");
- if (sConnect.Write(identification,16,WAITTIME)!=16)
- throw new CBlockingSocketException("Send back Identification Failed!");
- }
- char commandstr[9];
- memset(commandstr,0,9);
- int nLength=0;
- if (sConnect.Receive((char*)&nLength,4,WAITTIME)!=4)
- throw new CBlockingSocketException("Receive Command Length wrong!");
- if (sConnect.Receive(commandstr,8,WAITTIME)==8)
- {
- CriticalData.Lock();
- data=new char[nLength-7];
- memset(data,0,nLength-7);
- if (nLength-8!=0)
- if (sConnect.Receive(data,nLength-8,WAITTIME)!=nLength-8)
- throw new CBlockingSocketException("Receive Command parameter wrong!");
- CriticalData.Unlock();
- SCommand command=NOOP;
- if (!strcmp(commandstr,"GETINFO*"))
- command=GETINFO;
- else if (!strcmp(commandstr,"VIEWSCR*"))
- command=VIEWSCR;
- else if (!strcmp(commandstr,"LISTFILE"))
- command=LISTFILE;
- else if (!strcmp(commandstr,"SHUTDOWN"))
- command=SHUTDOWN;
- else if (!strcmp(commandstr,"RESTART*"))
- command=RESTART;
- else if (!strcmp(commandstr,"FINDFILE"))
- command=FINDFILE;
- else if (!strcmp(commandstr,"GETFILE*"))
- command=GETFILE;
- else if (!strcmp(commandstr,"LOGOFF**"))
- command=LOGOFF;
- else if (!strcmp(commandstr,"LISTPROC"))
- command=LISTPROC;
- else if (!strcmp(commandstr,"KILLPROC"))
- command=KILLPROC;
- else if (!strcmp(commandstr,"ENDSERVE"))
- command=ENDSERVE;
- else if (!strcmp(commandstr,"PUTFILE*"))
- command=PUTFILE;
- else if (!strcmp(commandstr,"RUNFILE*"))
- command=RUNFILE;
- else if (!strcmp(commandstr,"DELFILE*"))
- command=DELFILE;
- else if (!strcmp(commandstr,"CONFIG**"))
- command=CONFIG;
- else if (!strcmp(commandstr,"LOCKKEY*"))
- command=LOCKKEY;
- else if (!strcmp(commandstr,"UNLOCK**"))
- command=UNLOCK;
- if (command!=NOOP)
- {
- ::SendMessage(hwnd,WM_SOCKCOMMAND,WPARAM(command),(LPARAM)nLength);
- CriticalSection.Lock();
- char *SendBuf=new char[pViewWnd->DataBuf.GetLength()+1];
- memset(SendBuf,0,pViewWnd->DataBuf.GetLength()+1);
- strcpy(SendBuf,(LPCTSTR)(pViewWnd->DataBuf));
- SendBuf[pViewWnd->DataBuf.GetLength()]=NULL;
- CriticalSection.Unlock();
- int length=strlen(SendBuf);
- if ((UINT)sConnect.Write((char*)&length,4,WAITTIME)!=4)
- throw new CBlockingSocketException("Haven't sent all the data!");
- if ((UINT)sConnect.Write(SendBuf,strlen(SendBuf),WAITTIME)!=strlen(SendBuf))
- throw new CBlockingSocketException("Haven't sent all the data!");
- if (SendBuf!=NULL) delete SendBuf;
- CriticalSection.Lock();
- pViewWnd->DataBuf.Empty();
- CriticalSection.Unlock();
- }
- else throw new CBlockingSocketException("Invalidate Command!");
- }
- else throw new CBlockingSocketException("Command Recevie Error!");
- sConnect.Close();
- }
- catch(CBlockingSocketException*e)
- {
- if (sConnect.m_hSocket!=INVALID_SOCKET)sConnect.Cleanup();
- char error[1024];
- e->GetErrorMessage(error,1024);
- strcat(error," 或者你中止了服务!");
- pViewWnd->GetDlgItem(IDC_RESPONSE)->SetWindowText(error);
- e->Delete();
- beAssumed=FALSE;
- }
- catch(...)
- {
- if (sConnect.m_hSocket!=INVALID_SOCKET)sConnect.Cleanup();
- pViewWnd->GetDlgItem(IDC_RESPONSE)->SetWindowText("未知错误!");
- beAssumed=FALSE;
- }
- }
- return 0;
- }
- extern HANDLE hPutFile;
- extern HANDLE hEvent;
- UINT SendFile(LPVOID pParam)
- {
- char *filename;
- filename=(char *)pParam;
- filename[strlen(filename)-5]=NULL;
- int PORT=atoi(filename+strlen(filename)+1);
- CFile myFile;
- myFile.Open(filename, CFile::modeRead | CFile::typeBinary);
- int myFileLength = myFile.GetLength();
- CBlockingSocket sockSrvr,sockRecv;
- CSockAddr saClient;
- try
- {
- CSockAddr saServer(INADDR_ANY,PORT);
- sockSrvr.Create();
- sockSrvr.Bind(saServer);
- sockSrvr.Listen();
- ::SetEvent(hEvent);
- if(!sockSrvr.Accept(sockRecv,saClient))
- {
- throw new CBlockingSocketException("Accept Failed!");
- return 0;
- }
- sockRecv.Write((char*)&myFileLength,4,WAITTIME);
- byte *data=new byte[1024];
- int loopnum=myFileLength/1024;
- int rest=myFileLength-loopnum*1024;
- for (int i=0;i<loopnum;i++)
- {
- myFile.Read(data,1024);
- sockRecv.Write((const char *)data,1024,WAITTIME);
- }
- myFile.Read(data,rest);
- sockRecv.Write((const char *)data,rest,WAITTIME);
- myFile.Close();
- delete data;
- sockRecv.Close();
- sockSrvr.Close();
- }
- catch(CBlockingSocketException* e)
- {
- sockSrvr.Cleanup();
- sockRecv.Cleanup();
- char error[1024];
- e->GetErrorMessage(error,1024);
- AfxMessageBox(error,MB_ICONEXCLAMATION|MB_OK);
- e->Delete();
- }
- delete filename;
- ::CloseHandle(hEvent);
- return 0;
- }
- UINT PutFile(LPVOID pParam)
- {
- char *filename;
- filename=(char *)pParam;
- filename[strlen(filename)-5]=NULL;
- int PORT=atoi(filename+strlen(filename)+1);
- CFile myFile;
- myFile.Open(filename, CFile::modeWrite | CFile::typeBinary|CFile::modeCreate);
- int myFileLength = 0;
- CBlockingSocket sockSrvr,sockRecv;
- CSockAddr saClient;
- try
- {
- CSockAddr saServer(INADDR_ANY,PORT);
- sockSrvr.Create();
- sockSrvr.Bind(saServer);
- sockSrvr.Listen();
- ::SetEvent(hPutFile);
- if(!sockSrvr.Accept(sockRecv,saClient))
- {
- throw new CBlockingSocketException("Accept Failed!");
- return 0;
- }
- sockRecv.Receive((char*)&myFileLength,4,WAITTIME);
- byte *data=new byte[1024];
- byte *temp=data;
- int loopnum=myFileLength/1024;
- int rest=myFileLength-loopnum*1024;
- for (int i=0;i<loopnum;i++)
- {
- int nBytesSent = 0;
- int nBytesThisTime;
- data = temp;
- do
- {
- nBytesThisTime = sockRecv.Receive((char*)data, 1024 - nBytesSent, WAITTIME);
- nBytesSent += nBytesThisTime;
- data+= nBytesThisTime;
- } while(nBytesSent < 1024);
- myFile.Write(temp,1024);
- }
- sockRecv.Receive((char *)temp,rest,WAITTIME);
- myFile.Write(temp,rest);
- myFile.Close();
- delete temp;
- sockRecv.Close();
- sockSrvr.Close();
- }
- catch(CBlockingSocketException* e)
- {
- sockSrvr.Cleanup();
- sockRecv.Cleanup();
- char error[1024];
- e->GetErrorMessage(error,1024);
- AfxMessageBox(error,MB_ICONEXCLAMATION|MB_OK);
- e->Delete();
- }
- delete filename;
- ::CloseHandle(hPutFile);
- return 0;
- }