Thread.cpp
上传用户:zhenhuadz
上传日期:2007-01-07
资源大小:1511k
文件大小:8k
源码类别:

远程控制编程

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "Blocksock.h"
  3. #include "AnyServer.h"
  4. #include "AnyServerView.h"
  5. #include "ChatLsnSock.h"
  6. extern CBlockingSocket g_sListen;
  7. #define WAITTIME 10
  8. CCriticalSection CriticalSection;
  9. CCriticalSection CriticalData;
  10. BOOL beAssumed=FALSE;
  11. char *data;
  12. UINT Server(LPVOID lPvoid)
  13. {
  14. CAnyServerView* pViewWnd=(CAnyServerView*)lPvoid;
  15. if (g_sListen.m_hSocket!=NULL)
  16. {
  17. HWND hwnd=pViewWnd->GetSafeHwnd();
  18. CSockAddr saClient;
  19. CBlockingSocket sConnect;
  20. try
  21. {
  22. if (!g_sListen.Accept(sConnect,saClient))
  23. {
  24. throw new CBlockingSocketException("Accept Failed!");
  25. return 0;
  26. }
  27. CSockAddr sa;
  28. sConnect.GetPeerAddr(LPSOCKADDR(sa));
  29. ::SendMessage(hwnd,WM_IPADDR,(WPARAM)TRUE,sa.IPAddr());
  30. AfxBeginThread(Server,lPvoid);
  31. char identification[17];
  32. identification[16]=NULL;
  33. memset(identification,0,16);
  34. char* username=new char[9];
  35. memset(username,0,9);
  36. char* password=new char[9];
  37. memset(password,0,9);
  38. if (!beAssumed)
  39. {
  40. if (sConnect.Receive(identification,16,WAITTIME)==16)
  41. {
  42. strncpy(username,identification,8);
  43. strncpy(password,identification+8,8);
  44. CString id=AfxGetApp()->GetProfileString("AnyServer",username);
  45. pViewWnd->GetDlgItem(IDC_RESPONSE)->SetWindowText("客户端已经连接!");
  46. if (id==password) 
  47. {
  48. beAssumed=TRUE;
  49. ::SendMessage(hwnd,WM_USERNAME,(WPARAM)beAssumed,(LPARAM)username);
  50. }
  51. else 
  52. {
  53. sConnect.Close();
  54. ::SendMessage(hwnd,WM_COMMAND,MAKEWPARAM(ID_STOPSERVER,0),0);
  55. }
  56. }
  57. else
  58. throw new CBlockingSocketException("Identification Failed!");
  59. if (sConnect.Write(identification,16,WAITTIME)!=16)
  60. throw new CBlockingSocketException("Send back Identification Failed!");
  61. }
  62. char commandstr[9];
  63. memset(commandstr,0,9);
  64. int nLength=0;
  65. if (sConnect.Receive((char*)&nLength,4,WAITTIME)!=4)
  66. throw new CBlockingSocketException("Receive Command Length wrong!");
  67. if (sConnect.Receive(commandstr,8,WAITTIME)==8)
  68. {
  69. CriticalData.Lock();
  70. data=new char[nLength-7];
  71. memset(data,0,nLength-7);
  72. if (nLength-8!=0)
  73. if (sConnect.Receive(data,nLength-8,WAITTIME)!=nLength-8)
  74. throw new CBlockingSocketException("Receive Command parameter wrong!");
  75. CriticalData.Unlock();
  76. SCommand command=NOOP;
  77. if (!strcmp(commandstr,"GETINFO*"))
  78. command=GETINFO;
  79. else if (!strcmp(commandstr,"VIEWSCR*"))
  80. command=VIEWSCR;
  81. else if (!strcmp(commandstr,"LISTFILE"))
  82. command=LISTFILE;
  83. else if (!strcmp(commandstr,"SHUTDOWN"))
  84. command=SHUTDOWN;
  85. else if (!strcmp(commandstr,"RESTART*"))
  86. command=RESTART;
  87. else if (!strcmp(commandstr,"FINDFILE"))
  88. command=FINDFILE;
  89. else if (!strcmp(commandstr,"GETFILE*"))
  90. command=GETFILE;
  91. else if (!strcmp(commandstr,"LOGOFF**"))
  92. command=LOGOFF;
  93. else if (!strcmp(commandstr,"LISTPROC"))
  94. command=LISTPROC;
  95. else if (!strcmp(commandstr,"KILLPROC"))
  96. command=KILLPROC;
  97. else if (!strcmp(commandstr,"ENDSERVE"))
  98. command=ENDSERVE;
  99. else if (!strcmp(commandstr,"PUTFILE*"))
  100. command=PUTFILE;
  101. else if (!strcmp(commandstr,"RUNFILE*"))
  102. command=RUNFILE;
  103. else if (!strcmp(commandstr,"DELFILE*"))
  104. command=DELFILE;
  105. else if (!strcmp(commandstr,"CONFIG**"))
  106. command=CONFIG;
  107. else if (!strcmp(commandstr,"LOCKKEY*"))
  108. command=LOCKKEY;
  109. else if (!strcmp(commandstr,"UNLOCK**"))
  110. command=UNLOCK;
  111. if (command!=NOOP)
  112. {
  113. ::SendMessage(hwnd,WM_SOCKCOMMAND,WPARAM(command),(LPARAM)nLength);
  114. CriticalSection.Lock();
  115. char *SendBuf=new char[pViewWnd->DataBuf.GetLength()+1];
  116. memset(SendBuf,0,pViewWnd->DataBuf.GetLength()+1);
  117. strcpy(SendBuf,(LPCTSTR)(pViewWnd->DataBuf));
  118. SendBuf[pViewWnd->DataBuf.GetLength()]=NULL;
  119. CriticalSection.Unlock();
  120. int length=strlen(SendBuf);
  121. if ((UINT)sConnect.Write((char*)&length,4,WAITTIME)!=4)
  122. throw new CBlockingSocketException("Haven't sent all the data!");
  123. if ((UINT)sConnect.Write(SendBuf,strlen(SendBuf),WAITTIME)!=strlen(SendBuf))
  124. throw new CBlockingSocketException("Haven't sent all the data!");
  125. if (SendBuf!=NULL) delete SendBuf;
  126. CriticalSection.Lock();
  127. pViewWnd->DataBuf.Empty();
  128. CriticalSection.Unlock();
  129. }
  130. else throw new CBlockingSocketException("Invalidate Command!");
  131. }
  132. else throw new CBlockingSocketException("Command Recevie Error!");
  133. sConnect.Close();
  134. }
  135. catch(CBlockingSocketException*e)
  136. {
  137. if (sConnect.m_hSocket!=INVALID_SOCKET)sConnect.Cleanup();
  138. char error[1024];
  139. e->GetErrorMessage(error,1024);
  140. strcat(error,"  或者你中止了服务!");
  141. pViewWnd->GetDlgItem(IDC_RESPONSE)->SetWindowText(error);
  142. e->Delete();
  143. beAssumed=FALSE;
  144. }
  145. catch(...)
  146. {
  147. if (sConnect.m_hSocket!=INVALID_SOCKET)sConnect.Cleanup();
  148. pViewWnd->GetDlgItem(IDC_RESPONSE)->SetWindowText("未知错误!");
  149. beAssumed=FALSE;
  150. }
  151. }
  152. return 0;
  153. }
  154. extern HANDLE hPutFile;
  155. extern HANDLE hEvent;
  156. UINT SendFile(LPVOID pParam)
  157. {
  158. char *filename;
  159. filename=(char *)pParam;
  160. filename[strlen(filename)-5]=NULL;
  161. int PORT=atoi(filename+strlen(filename)+1);
  162. CFile myFile;
  163. myFile.Open(filename, CFile::modeRead | CFile::typeBinary);
  164. int myFileLength = myFile.GetLength();
  165. CBlockingSocket sockSrvr,sockRecv;
  166. CSockAddr saClient;
  167. try
  168. {
  169. CSockAddr saServer(INADDR_ANY,PORT);
  170. sockSrvr.Create();
  171. sockSrvr.Bind(saServer);
  172. sockSrvr.Listen();
  173. ::SetEvent(hEvent);
  174. if(!sockSrvr.Accept(sockRecv,saClient))
  175. {
  176. throw new CBlockingSocketException("Accept Failed!");
  177. return 0;
  178. }
  179. sockRecv.Write((char*)&myFileLength,4,WAITTIME);
  180. byte *data=new byte[1024];
  181. int loopnum=myFileLength/1024;
  182. int rest=myFileLength-loopnum*1024;
  183. for (int i=0;i<loopnum;i++)
  184. {
  185.           myFile.Read(data,1024);
  186. sockRecv.Write((const char *)data,1024,WAITTIME);
  187. }
  188. myFile.Read(data,rest);
  189. sockRecv.Write((const char *)data,rest,WAITTIME);
  190. myFile.Close();
  191. delete data;
  192. sockRecv.Close();
  193. sockSrvr.Close();
  194. }
  195. catch(CBlockingSocketException* e)
  196. {
  197. sockSrvr.Cleanup();
  198. sockRecv.Cleanup();
  199. char error[1024];
  200. e->GetErrorMessage(error,1024);
  201. AfxMessageBox(error,MB_ICONEXCLAMATION|MB_OK);
  202. e->Delete();
  203. }
  204. delete filename;
  205. ::CloseHandle(hEvent);
  206. return 0;
  207. }
  208. UINT PutFile(LPVOID pParam)
  209. {
  210. char *filename;
  211. filename=(char *)pParam;
  212. filename[strlen(filename)-5]=NULL;
  213. int PORT=atoi(filename+strlen(filename)+1);
  214. CFile myFile;
  215. myFile.Open(filename, CFile::modeWrite | CFile::typeBinary|CFile::modeCreate);
  216. int myFileLength = 0;
  217. CBlockingSocket sockSrvr,sockRecv;
  218. CSockAddr saClient;
  219. try
  220. {
  221. CSockAddr saServer(INADDR_ANY,PORT);
  222. sockSrvr.Create();
  223. sockSrvr.Bind(saServer);
  224. sockSrvr.Listen();
  225. ::SetEvent(hPutFile);
  226. if(!sockSrvr.Accept(sockRecv,saClient))
  227. {
  228. throw new CBlockingSocketException("Accept Failed!");
  229. return 0;
  230. }
  231. sockRecv.Receive((char*)&myFileLength,4,WAITTIME);
  232. byte *data=new byte[1024];
  233. byte *temp=data;
  234. int loopnum=myFileLength/1024;
  235. int rest=myFileLength-loopnum*1024;
  236. for (int i=0;i<loopnum;i++)
  237. {
  238.           int nBytesSent = 0;
  239. int nBytesThisTime;
  240. data = temp;
  241. do 
  242. {
  243. nBytesThisTime = sockRecv.Receive((char*)data, 1024 - nBytesSent, WAITTIME);
  244. nBytesSent += nBytesThisTime;
  245. data+= nBytesThisTime;
  246. } while(nBytesSent < 1024);
  247. myFile.Write(temp,1024);
  248. }
  249. sockRecv.Receive((char *)temp,rest,WAITTIME);
  250. myFile.Write(temp,rest);
  251. myFile.Close();
  252. delete temp;
  253. sockRecv.Close();
  254. sockSrvr.Close();
  255. }
  256. catch(CBlockingSocketException* e)
  257. {
  258. sockSrvr.Cleanup();
  259. sockRecv.Cleanup();
  260. char error[1024];
  261. e->GetErrorMessage(error,1024);
  262. AfxMessageBox(error,MB_ICONEXCLAMATION|MB_OK);
  263. e->Delete();
  264. }
  265. delete filename;
  266. ::CloseHandle(hPutFile);
  267. return 0;
  268. }