DASocket.cpp
上传用户:asikq0571
上传日期:2014-07-12
资源大小:528k
文件大小:13k
源码类别:

Internet/IE编程

开发平台:

Visual C++

  1. // DASocket.cpp: implementation of the CDASrvCnSocket class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "DASocket.h"
  6. #ifdef _DEBUG
  7. #undef THIS_FILE
  8. static char THIS_FILE[]=__FILE__;
  9. #define new DEBUG_NEW
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. //网络常用操作函数
  13. int GetLocalHostName(CString &sHostName)
  14. {
  15. char szHostName[256];
  16. int nRetCode;
  17. nRetCode=gethostname(szHostName,sizeof(szHostName));
  18. if(nRetCode!=0)
  19. {
  20. //产生错误
  21. sHostName=_T("没有取得本地用户名");
  22. return GetLastError();
  23. }
  24. sHostName=szHostName;
  25. return 0;
  26. }
  27. int GetIpAddress(const CString &sHostName, CString &sIpAddress)//获得本地IP
  28. {
  29. struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
  30. if(lpHostEnt==NULL)
  31. {
  32. //产生错误
  33. sIpAddress=_T("");
  34. return GetLastError();
  35. }
  36. //获取IP
  37. LPSTR lpAddr=lpHostEnt->h_addr_list[0];
  38. if(lpAddr)
  39. {
  40. struct in_addr inAddr;
  41. memmove(&inAddr,lpAddr,4);
  42. //转换为标准格式
  43. sIpAddress=inet_ntoa(inAddr);
  44. if(sIpAddress.IsEmpty())
  45. sIpAddress=_T("没有取得本地IP地址");
  46. }
  47. return 0;
  48. }
  49. int GetNamebyAddress(const CString &IpAddress,CString &sYouName)//获得对方计算机名称
  50. {
  51. unsigned long addr;
  52. addr=inet_addr(IpAddress);
  53. struct hostent FAR * lpHostEnt=gethostbyaddr((char *)&addr,4,AF_INET);
  54. if(lpHostEnt==NULL)
  55. {
  56. //产生错误
  57. sYouName=_T("");
  58. AfxMessageBox("连接不上");//应该取得其错误
  59. return -1;
  60. }
  61. CString name=lpHostEnt->h_name;
  62. sYouName=name;
  63. return 0;
  64. }
  65. CString GetNetError(DWORD error) //返回错误信息
  66. {
  67. CString strError;
  68. switch(error)
  69. {
  70. case WSANOTINITIALISED:
  71. strError="初始化错误";
  72. break;
  73. case WSAENOTCONN:
  74. strError="对方没有启动";
  75. break;
  76. case WSAEWOULDBLOCK :
  77. strError="对方已经关闭";
  78. break;
  79. case WSAECONNREFUSED:
  80. strError="连接的尝试被拒绝";
  81. break;
  82. case WSAENOTSOCK:
  83. strError="在一个非套接字上尝试了一个操作";
  84. break;
  85. case WSAEADDRINUSE:
  86. strError="特定的地址已在使用中";
  87. break;
  88. case WSAECONNRESET:
  89. strError="与主机的连接被关闭";
  90. break;
  91. default:
  92. strError="一般错误";
  93. }
  94. return strError;
  95. }
  96. /////////////////////////////////////////////////////////////////////////////
  97. /////////////////////////////////////////////////////////////////////////////
  98. // CDASrvCnSocket
  99. CDASrvCnSocket::CDASrvCnSocket(CDAServerSocket* pOwner)
  100. {
  101. m_pOwner = pOwner;
  102. m_pEvtReceive = NULL;      
  103. m_pEvtSend = NULL;         
  104. m_pEvtOutOfBandData = NULL;
  105. m_pEvtAccept = NULL;       
  106. m_pEvtConnect = NULL;      
  107. m_pEvtClose = NULL;        
  108. }
  109. CDASrvCnSocket::~CDASrvCnSocket()
  110. {
  111. }
  112. // Do not edit the following lines, which are needed by ClassWizard.
  113. #if 0
  114. BEGIN_MESSAGE_MAP(CDASrvCnSocket, CAsyncSocket)
  115. //{{AFX_MSG_MAP(CDASrvCnSocket)
  116. //}}AFX_MSG_MAP
  117. END_MESSAGE_MAP()
  118. #endif // 0
  119. /////////////////////////////////////////////////////////////////////////////
  120. // CDASrvCnSocket member functions
  121. void CDASrvCnSocket::OnReceive(int nErrorCode) 
  122. {
  123. // TODO: Add your specialized code here and/or call the base class
  124. if(m_pEvtReceive)
  125. {
  126. (m_pOwner->*m_pEvtReceive)(this, nErrorCode);
  127. }
  128. AsyncSelect(FD_READ | FD_CLOSE);
  129. CAsyncSocket::OnReceive(nErrorCode);
  130. }
  131. void CDASrvCnSocket::OnClose(int nErrorCode) 
  132. {
  133. // TODO: Add your specialized code here and/or call the base class
  134. if(m_pEvtClose)
  135. (m_pOwner->*m_pEvtClose)(this, nErrorCode);
  136. CAsyncSocket::OnClose(nErrorCode);
  137. }
  138. void CDASrvCnSocket::OnConnect(int nErrorCode) 
  139. {
  140. // TODO: Add your specialized code here and/or call the base class
  141. if(m_pEvtConnect)
  142. {
  143. (m_pOwner->*m_pEvtConnect)(this, nErrorCode);
  144. }
  145. AsyncSelect(FD_READ | FD_CLOSE);
  146. CAsyncSocket::OnConnect(nErrorCode);
  147. }
  148. void CDASrvCnSocket::OnOutOfBandData(int nErrorCode) 
  149. {
  150. // TODO: Add your specialized code here and/or call the base class
  151. if(m_pEvtOutOfBandData)
  152. (m_pOwner->*m_pEvtOutOfBandData)(this, nErrorCode);
  153. CAsyncSocket::OnOutOfBandData(nErrorCode);
  154. }
  155. void CDASrvCnSocket::OnSend(int nErrorCode) 
  156. {
  157. // TODO: Add your specialized code here and/or call the base class
  158. if(m_pEvtSend)
  159. {
  160. (m_pOwner->*m_pEvtSend)(this, nErrorCode);
  161. }
  162. AsyncSelect(FD_READ | FD_CLOSE);
  163. CAsyncSocket::OnSend(nErrorCode);
  164. }
  165. BOOL CDASrvCnSocket::SendData(char *pSendBuffer, int nBufLen, int& dwBytesSended)
  166. {
  167. if(nBufLen < 0) return FALSE;
  168. int iRemainBytes = nBufLen;
  169. int iTotalSentBytes = 0;
  170. int iCurSent = 0;
  171. while(iRemainBytes)
  172. {
  173. iCurSent = Send(pSendBuffer + iTotalSentBytes,iRemainBytes);
  174. if(iCurSent < 0)
  175. {
  176. TRACE("Send Error!n");
  177. return FALSE;
  178. }
  179. iTotalSentBytes += iCurSent;
  180. iRemainBytes -= iCurSent; 
  181. }
  182. dwBytesSended=iTotalSentBytes;
  183. return TRUE;
  184. }
  185. BOOL CDASrvCnSocket::RecvData(char *pRecvBuffer, int nBufLen, int& dwBytesReaded)
  186. {
  187. if(nBufLen < 0) return FALSE;
  188. int iRemainBytes = nBufLen;
  189. int iTotalRecvBytes = 0;
  190. int iCurRecv = 0;
  191. while(iRemainBytes)
  192. {
  193. iCurRecv = Receive(pRecvBuffer + iTotalRecvBytes,iRemainBytes);
  194. if(iCurRecv < 0)
  195. {
  196. TRACE("Receive Error!n");
  197. return FALSE;
  198. }
  199. iTotalRecvBytes += iCurRecv;
  200. iRemainBytes -= iCurRecv; 
  201. }
  202. dwBytesReaded=(DWORD)iTotalRecvBytes;
  203. return TRUE;
  204. }
  205. /////////////////////////////////////////////////////////////////////////////
  206. // CDAServerSocket
  207. CDAServerSocket::CDAServerSocket(HWND pOwner)
  208. {
  209. m_hOwner = pOwner;
  210. m_bHasOpened = FALSE;
  211. m_bConnected = FALSE;
  212. }
  213. CDAServerSocket::~CDAServerSocket()
  214. {
  215. for(int i = 0; i < m_arConnections.GetSize(); i++) //释放所有连接
  216. {
  217. delete m_arConnections[i];
  218. }
  219. }
  220. // Do not edit the following lines, which are needed by ClassWizard.
  221. #if 0
  222. BEGIN_MESSAGE_MAP(CDAServerSocket, CAsyncSocket)
  223. //{{AFX_MSG_MAP(CDAServerSocket)
  224. //}}AFX_MSG_MAP
  225. END_MESSAGE_MAP()
  226. #endif // 0
  227. BOOL CDAServerSocket::OpenPort(UINT nSocketPort, const CString szAddr)
  228. {
  229. m_nSocketPort = nSocketPort;
  230. m_szAddr = szAddr;
  231. AfxSocketInit();
  232. if(!Create(nSocketPort))//, SOCK_STREAM, FD_ACCEPT))//, szAddr))
  233. {
  234. return FALSE;
  235. }
  236. if(!Listen()) 
  237. {
  238. Close();
  239. return FALSE;
  240. }
  241. m_bHasOpened = TRUE;
  242. return TRUE;
  243. }
  244. void CDAServerSocket::ClosePort()
  245. {
  246. CDASrvCnSocket* pSrvCnSocket = NULL;
  247. Close();
  248. m_bConnected = FALSE;
  249. for(int i = 0; i < m_arConnections.GetSize(); i++)
  250. {
  251. pSrvCnSocket = m_arConnections.ElementAt(i);
  252. delete pSrvCnSocket;
  253. pSrvCnSocket = NULL;
  254. }
  255. m_arConnections.RemoveAll();
  256. }
  257. /////////////////////////////////////////////////////////////////////////////
  258. // CDAServerSocket member functions
  259. void CDAServerSocket::OnAccept(int nErrorCode) 
  260. {
  261. // TODO: Add your specialized code here and/or call the base class
  262. if(!nErrorCode) 
  263. {
  264. CDASrvCnSocket* pcnSocket;
  265. SOCKADDR_IN client;
  266. int iAddrSize=sizeof(client);
  267. pcnSocket = new CDASrvCnSocket(this);
  268. if(Accept(*pcnSocket,(SOCKADDR *)&client,&iAddrSize))
  269. {
  270. pcnSocket->AsyncSelect(FD_READ|FD_WRITE|FD_CLOSE);
  271. pcnSocket->m_pEvtReceive = ReceiveEvt;
  272. pcnSocket->m_pEvtSend = SendEvt;
  273. pcnSocket->m_pEvtClose = CloseEvt;
  274. pcnSocket->m_strRemoIP = inet_ntoa(client.sin_addr);
  275. pcnSocket->m_nRemoPort = client.sin_port;
  276. m_arConnections.Add(pcnSocket);
  277. m_bConnected = TRUE;
  278. SendMessage(m_hOwner, WM_TCP_SERVER_CONNECTED, (WPARAM)nErrorCode, (LPARAM)pcnSocket);
  279. else 
  280. {
  281. delete pcnSocket;
  282. pcnSocket = NULL;
  283. }
  284. }
  285. AsyncSelect(FD_ACCEPT|FD_CLOSE);
  286. CAsyncSocket::OnAccept(nErrorCode);
  287. }
  288. void CDAServerSocket::ReceiveEvt(CAsyncSocket* pSrvCnSocket, int nErrorCode)
  289. {
  290. SendMessage(m_hOwner, WM_TCP_SERVER_RECEIVE, (WPARAM)nErrorCode, (LPARAM)pSrvCnSocket);
  291. }
  292. void CDAServerSocket::SendEvt(CAsyncSocket* pSrvCnSocket, int nErrorCode)
  293. {
  294. SendMessage(m_hOwner, WM_TCP_SERVER_SEND, (WPARAM)nErrorCode, (LPARAM)pSrvCnSocket);
  295. }
  296. void CDAServerSocket::CloseEvt(CAsyncSocket* pSrvCnSocket, int nErrorCode)
  297. {
  298. SendMessage(m_hOwner, WM_TCP_SERVER_CLOSED, (WPARAM)nErrorCode, (LPARAM)pSrvCnSocket);
  299. for(int i = 0; i < m_arConnections.GetSize(); i++)
  300. {
  301. if(m_arConnections[i]->m_hSocket == pSrvCnSocket->m_hSocket)
  302. {
  303. delete pSrvCnSocket;
  304. pSrvCnSocket = NULL;
  305. m_arConnections.RemoveAt(i);
  306. if(m_arConnections.GetSize() == 0)
  307. {
  308. m_bConnected = FALSE;
  309. }
  310. return;
  311. }
  312. }
  313. }
  314. /////////////////////////////////////////////////////////////////////////////
  315. // CDAClientSocket
  316. CDAClientSocket::CDAClientSocket(HWND pOwner)
  317. {
  318. m_pOwner = pOwner;
  319. m_bHasOpened = FALSE;
  320. m_bConnected = FALSE;
  321. }
  322. CDAClientSocket::~CDAClientSocket()
  323. {
  324. if(m_hSocket != INVALID_SOCKET) Close();
  325. }
  326. BOOL CDAClientSocket::OpenPort(UINT nHostPort, const CString szHostAddr)
  327. {
  328. m_nHostPort = nHostPort;
  329. m_szHostAddr = szHostAddr;
  330. if(m_hSocket != INVALID_SOCKET)
  331. {
  332. Close();
  333. }
  334. if(!Create(0,SOCK_STREAM,FD_READ |FD_CONNECT | FD_CLOSE))
  335. {
  336. m_bHasOpened = FALSE;
  337. m_bConnected = FALSE;
  338. return FALSE;
  339. }
  340.    Connect(szHostAddr, nHostPort);
  341. return TRUE;
  342. }
  343. void CDAClientSocket::ClosePort()
  344. {
  345. Close();
  346. }
  347. // Do not edit the following lines, which are needed by ClassWizard.
  348. #if 0
  349. BEGIN_MESSAGE_MAP(CDAClientSocket, CAsyncSocket)
  350. //{{AFX_MSG_MAP(CDAClientSocket)
  351. //}}AFX_MSG_MAP
  352. END_MESSAGE_MAP()
  353. #endif // 0
  354. /////////////////////////////////////////////////////////////////////////////
  355. // CDAClientSocket member functions
  356. void CDAClientSocket::OnConnect(int nErrorCode) 
  357. {
  358. // TODO: Add your specialized code here and/or call the base class
  359. if(!nErrorCode) 
  360. {
  361. m_bHasOpened = TRUE;
  362. m_bConnected = TRUE;
  363.        SendMessage(m_pOwner, WM_TCP_CLIENT_CONNECTED, (WPARAM)nErrorCode, (LPARAM)this);
  364. }
  365. else
  366.         AfxMessageBox("无法连接指定的服务器网络端口,请重试!");
  367. AsyncSelect(FD_READ | FD_CLOSE);
  368. CAsyncSocket::OnConnect(nErrorCode);
  369. }
  370. void CDAClientSocket::OnReceive(int nErrorCode) 
  371. {
  372. SendMessage(m_pOwner, WM_TCP_CLIENT_RECEIVE, (WPARAM)nErrorCode, (LPARAM)this);
  373. AsyncSelect(FD_READ | FD_CLOSE);
  374. CAsyncSocket::OnReceive(nErrorCode);
  375. }
  376. void CDAClientSocket::OnSend(int nErrorCode) 
  377. {
  378. SendMessage(m_pOwner, WM_TCP_CLIENT_SEND, (WPARAM)nErrorCode, (LPARAM)this);
  379. AsyncSelect(FD_READ | FD_CLOSE);
  380. CAsyncSocket::OnSend(nErrorCode);
  381. }
  382. void CDAClientSocket::OnClose(int nErrorCode) 
  383. {
  384.     // TODO: Add your specialized code here and/or call the base class
  385. m_bHasOpened = FALSE;
  386. m_bConnected = FALSE;
  387. SendMessage(m_pOwner, WM_TCP_CLIENT_CLOSED, (WPARAM)nErrorCode, (LPARAM)this);
  388. CAsyncSocket::OnClose(nErrorCode);
  389. }
  390. BOOL CDAClientSocket::SendData(char *pSendBuffer, int nBufLen)
  391. {
  392. if(nBufLen < 0) return FALSE;
  393. int iRemainBytes = nBufLen;
  394. int iTotalSentBytes = 0;
  395. int iCurSent = 0;
  396. while(iRemainBytes)
  397. {
  398. iCurSent = Send(pSendBuffer + iTotalSentBytes,iRemainBytes);
  399. if(iCurSent < 0)
  400. {
  401. TRACE("Send Error!n");
  402. return FALSE;
  403. }
  404. iTotalSentBytes += iCurSent;
  405. iRemainBytes -= iCurSent; 
  406. }
  407. return TRUE;
  408. }
  409. BOOL CDAClientSocket::RecvData(char *pRecvBuffer, int nBufLen)
  410. {
  411. if(nBufLen < 0) return FALSE;
  412. int iRemainBytes = nBufLen;
  413. int iTotalRecvBytes = 0;
  414. int iCurRecv = 0;
  415. while(iRemainBytes)
  416. {
  417. iCurRecv = Receive(pRecvBuffer + iTotalRecvBytes,iRemainBytes);
  418. if(iCurRecv < 0)
  419. {
  420. TRACE("Receive Error!n");
  421. return FALSE;
  422. }
  423. iTotalRecvBytes += iCurRecv;
  424. iRemainBytes -= iCurRecv; 
  425. }
  426. return TRUE;
  427. }
  428. ///////////////////////////////////////
  429. ///////////////////////////////////////
  430. CDAUDPSocket::CDAUDPSocket(HWND pOwner)
  431. {
  432. m_pOwner = pOwner;
  433. m_bHasOpened = FALSE;
  434. }
  435. CDAUDPSocket::~CDAUDPSocket()
  436. {
  437. if(m_hSocket != INVALID_SOCKET) Close();
  438. }
  439. BOOL CDAUDPSocket::OpenPort(UINT nLocalPort)
  440. {
  441. m_nLocalPort = nLocalPort;
  442. if(m_hSocket != INVALID_SOCKET)
  443. {
  444. Close();
  445. }
  446. if(!Create(nLocalPort,SOCK_DGRAM,FD_READ |FD_CONNECT | FD_CLOSE))
  447. {
  448. m_bHasOpened = FALSE;
  449. return FALSE;
  450. }
  451. m_bHasOpened = TRUE;
  452. return TRUE;
  453. }
  454. void CDAUDPSocket::ClosePort()
  455. {
  456. Close();
  457. }
  458. // Do not edit the following lines, which are needed by ClassWizard.
  459. #if 0
  460. BEGIN_MESSAGE_MAP(CDAUDPSocket, CAsyncSocket)
  461. //{{AFX_MSG_MAP(CDAUDPSocket)
  462. //}}AFX_MSG_MAP
  463. END_MESSAGE_MAP()
  464. #endif // 0
  465. void CDAUDPSocket::OnReceive(int nErrorCode) 
  466. {
  467. SendMessage(m_pOwner, WM_UDP_RECEIVE, (WPARAM)nErrorCode, (LPARAM)this);
  468. AsyncSelect(FD_READ | FD_CLOSE);
  469. CAsyncSocket::OnReceive(nErrorCode);
  470. }
  471. void CDAUDPSocket::OnSend(int nErrorCode) 
  472. {
  473. SendMessage(m_pOwner, WM_UDP_SEND, (WPARAM)nErrorCode, (LPARAM)this);
  474. AsyncSelect(FD_READ | FD_CLOSE);
  475. CAsyncSocket::OnSend(nErrorCode);
  476. }
  477. void CDAUDPSocket::OnClose(int nErrorCode) 
  478. {
  479. // TODO: Add your specialized code here and/or call the base class
  480. m_bHasOpened = FALSE;
  481. SendMessage(m_pOwner, WM_UDP_CLOSED, (WPARAM)nErrorCode, (LPARAM)this);
  482. CAsyncSocket::OnClose(nErrorCode);
  483. }