ClientSocket.cpp
上传用户:sycq158
上传日期:2008-10-22
资源大小:15361k
文件大小:6k
源码类别:

游戏

开发平台:

Visual C++

  1. // ClientSocket.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Example2_ChatRoom.h"
  5. #include "ClientSocket.h"
  6. #include "Example2_ChatRoomDlg.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CClientSocket
  14. CClientSocket::CClientSocket()
  15. {
  16. m_aSessionIn=NULL;
  17. m_aSessionOut=NULL;
  18. m_sfSocketFile=NULL;
  19. m_bInit=false;
  20. m_bClose=false;
  21. }
  22. CClientSocket::~CClientSocket()
  23. {
  24. if(m_aSessionIn)
  25. delete m_aSessionIn;
  26. if(m_aSessionOut)
  27. delete m_aSessionOut;
  28. if(m_sfSocketFile)
  29. delete m_sfSocketFile;
  30. }
  31. // Do not edit the following lines, which are needed by ClassWizard.
  32. #if 0
  33. BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
  34. //{{AFX_MSG_MAP(CClientSocket)
  35. //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37. #endif // 0
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CClientSocket member functions
  40. void CClientSocket::OnReceive(int nErrorCode) 
  41. {
  42. // TODO: Add your specialized code here and/or call the base class
  43. CSocket::OnReceive(nErrorCode);
  44. do
  45. {
  46. CMessg temp;
  47. temp.Serialize(*m_aSessionIn);
  48. m_dlg->m_sMsgList+=temp.m_strText;
  49. m_dlg->SetDlgItemText(IDC_SHOWTEXT,m_dlg->m_sMsgList);
  50. int linenum=((CEdit *)(m_dlg->GetDlgItem(IDC_SHOWTEXT)))->GetLineCount();
  51. ((CEdit *)(m_dlg->GetDlgItem(IDC_SHOWTEXT)))->LineScroll(linenum);
  52. if(!m_dlg->m_bClient)
  53. {
  54. for(POSITION pos=m_dlg->m_connectionList.GetHeadPosition();pos!=NULL;)
  55. {
  56. CClientSocket * t = (CClientSocket*)m_dlg->m_connectionList.GetNext(pos);
  57. if(t->m_hSocket!=this->m_hSocket)
  58. {
  59. t->SendMessage(&temp);
  60. }
  61. }
  62. }
  63. }
  64. while (!m_aSessionIn->IsBufferEmpty());
  65. }
  66. void CClientSocket::Init(CExample2_ChatRoomDlg * dlg)
  67. {
  68. m_sfSocketFile= new CSocketFile(this);
  69. m_aSessionIn=new CArchive(m_sfSocketFile,CArchive::load);
  70. m_aSessionOut=new CArchive(m_sfSocketFile,CArchive::store);
  71. m_bClose=false;
  72. this->m_dlg=dlg;
  73. }
  74. BOOL CClientSocket::SendMessage(CMessg * msg)
  75. {
  76. if (m_aSessionOut != NULL)
  77. {
  78. msg->Serialize(*m_aSessionOut);
  79. m_aSessionOut->Flush();
  80. return TRUE;
  81. }
  82. else
  83. {
  84. //对方关闭了连接
  85. m_bClose=true;
  86. CloseSocket();
  87. m_dlg->CloseSessionSocket();
  88. return FALSE;
  89. }
  90. }
  91. void CClientSocket::CloseSocket()
  92. {
  93. if(m_aSessionIn)
  94. {
  95. delete m_aSessionIn;
  96. m_aSessionIn=NULL;
  97. }
  98. if(m_aSessionOut)
  99. {
  100. delete m_aSessionOut;
  101. m_aSessionOut=NULL;
  102. }
  103. if(m_sfSocketFile)
  104. {
  105. delete m_aSessionOut;
  106. m_sfSocketFile=NULL;
  107. }
  108. Close();
  109. m_bInit=false;
  110. m_bClose=true;
  111. }
  112. void CClientSocket::OnClose(int nErrorCode) 
  113. {
  114. // TODO: Add your specialized code here and/or call the base class
  115. m_bClose=true;
  116. CloseSocket();
  117. m_dlg->CloseSessionSocket();
  118. CSocket::OnClose(nErrorCode);
  119. }
  120. int CClientSocket::GetLocalHostName(CString &sHostName) //获得本地计算机名称
  121. {
  122. char szHostName[256];
  123. int nRetCode;
  124. nRetCode=gethostname(szHostName,sizeof(szHostName));
  125. if(nRetCode!=0)
  126. {
  127. //产生错误
  128. sHostName=_T("没有取得");
  129. return GetLastError();
  130. }
  131. sHostName=szHostName;
  132. return 0;
  133. }
  134. int CClientSocket::GetIpAddress(const CString &sHostName, CString &sIpAddress)//获得本地IP
  135. {
  136. struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
  137. if(lpHostEnt==NULL)
  138. {
  139. //产生错误
  140. sIpAddress=_T("");
  141. return GetLastError();
  142. }
  143. //获取IP
  144. LPSTR lpAddr=lpHostEnt->h_addr_list[0];
  145. if(lpAddr)
  146. {
  147. struct in_addr inAddr;
  148. memmove(&inAddr,lpAddr,4);
  149. //转换为标准格式
  150. sIpAddress=inet_ntoa(inAddr);
  151. if(sIpAddress.IsEmpty())
  152. sIpAddress=_T("没有取得");
  153. }
  154. return 0;
  155. }
  156. int CClientSocket::GetIpAddress(const CString &sHostName, BYTE &f0,BYTE &f1,BYTE &f2,BYTE &f3)//获得本地IP
  157. {
  158. struct hostent FAR * lpHostEnt=gethostbyname(sHostName);
  159. if(lpHostEnt==NULL)
  160. {
  161. //产生错误
  162. f0=f1=f2=f3=0;
  163. return GetLastError();
  164. }
  165. //获取IP
  166. LPSTR lpAddr=lpHostEnt->h_addr_list[0];
  167. if(lpAddr)
  168. {
  169. struct in_addr inAddr;
  170. memmove(&inAddr,lpAddr,4);
  171. f0=inAddr.S_un.S_un_b.s_b1;
  172. f1=inAddr.S_un.S_un_b.s_b2;
  173. f2=inAddr.S_un.S_un_b.s_b3;
  174. f3=inAddr.S_un.S_un_b.s_b4;
  175. }
  176. return 0;
  177. }
  178. CString CClientSocket::ErrorReason(int tag)
  179. {
  180. CString result;
  181. switch(tag)
  182. {
  183. case WSANOTINITIALISED:
  184. result="A successful AfxSocketInit must occur before using this API.";
  185. break;
  186. case WSAENETDOWN:
  187. result="the network subsystem failed";
  188. break;
  189. case WSAEADDRINUSE:
  190. result="The specified address is already in use";
  191. break;
  192. case WSAEINPROGRESS:
  193. result="A blocking Windows Sockets call is in progress";
  194. break;
  195. case WSAEADDRNOTAVAIL:
  196. result="The specified address is not available from the local machine";
  197. break;
  198. case WSAEAFNOSUPPORT:
  199. result="Addresses in the specified family cannot be used with this socket";
  200. break;
  201. case WSAECONNREFUSED:
  202. result="The attempt to connect was rejected";
  203. break;
  204. case WSAEDESTADDRREQ:
  205. result="A destination address is required";
  206. break;
  207. case WSAEFAULT:
  208. result="The nSockAddrLen argument is incorrect";
  209. break;
  210. case WSAEINVAL:
  211. result="Invalid host address";
  212. break;
  213. case WSAEISCONN:
  214. result="The socket is already connected.";
  215. break;
  216. case WSAEMFILE:
  217. result="No more file descriptors are available";
  218. break;
  219. case WSAENETUNREACH:
  220. result="The network cannot be reached from this host at this time";
  221. break;
  222. case WSAENOBUFS:
  223. result="No buffer space is available. The socket cannot be connected";
  224. break;
  225. case WSAENOTSOCK:
  226. result="The descriptor is not a socket";
  227. break;
  228. case WSAETIMEDOUT:
  229. result="Attempt to connect timed out without establishing a connection";
  230. break;
  231. case WSAEWOULDBLOCK:
  232. result="The socket is marked as nonblocking and the connection cannot be completed immediately.";
  233. break;
  234. default:
  235. result="unknown error";
  236. }
  237. return result;
  238. }