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

游戏

开发平台:

Visual C++

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