stmsocket.cpp
上传用户:dzyhzl
上传日期:2019-04-29
资源大小:56270k
文件大小:4k
源码类别:

模拟服务器

开发平台:

C/C++

  1. ////////////////////////////////////////////////////////////////////////////////
  2. //  
  3. //  FileName    :   StmSocket.cpp
  4. //  Version     :   1.0
  5. //  Creater     :   Linsuyi
  6. //  Date        :   2002-02-21  10:03:16
  7. //  Comment     :   Tcp/ip streamgram base socket source file
  8. //  
  9. ////////////////////////////////////////////////////////////////////////////////
  10. #include "Stdafx.h"
  11. #include "StmSocket.h"
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CStmSocket::CStmSocket()
  16. {
  17.     m_nSckType = SOCK_STREAM;
  18.     
  19.     ZeroMemory(&m_saRemoteAddr, sizeof(m_saRemoteAddr));
  20. }
  21. CStmSocket::~CStmSocket()
  22. {
  23. }
  24. int CStmSocket::Attach(HSOCKET hInetSocket)
  25. {
  26.     int nResult = false;
  27.     int nRetCode = false;
  28.     
  29.     nRetCode = CIPSocket::Attach(hInetSocket);
  30.     if (!nRetCode)
  31.         goto Exit0;
  32.     
  33.     ZeroMemory(&m_saRemoteAddr, sizeof(m_saRemoteAddr));
  34.     nRetCode = GetRemoteAddress(&m_saRemoteAddr);
  35.     ASSERT(0 == nRetCode);
  36.     
  37.     nResult = true;
  38.     
  39. Exit0:
  40.     return nResult;
  41. }
  42. int CStmSocket::Connect(const char *pcszRemoteAddr, int nPort /*=0*/)
  43. {
  44.     int nResult = false;
  45.     int nRetCode = 0;
  46.     INET_ADDRESS saRemoteAddr = { 0 };
  47.     
  48.     ASSERT(nPort != 0);
  49.     ASSERT(pcszRemoteAddr != NULL);
  50.     if (
  51.         (0 == nPort) || 
  52.         (NULL == pcszRemoteAddr)
  53.     )
  54.     {
  55.         goto Exit0;
  56.     }
  57.     
  58.     nRetCode = GetSocketAddress(pcszRemoteAddr, &saRemoteAddr);
  59.     ASSERT(nRetCode);
  60.     
  61.     if (nRetCode)
  62.     {
  63.         if (nPort != 0)
  64.             saRemoteAddr.sin_port = ::htons(nPort);
  65.         
  66.         nResult = Connect(&saRemoteAddr);
  67.     }
  68.     
  69. Exit0:
  70.     return nResult;
  71. }
  72. int CStmSocket::Connect(const INET_ADDRESS *psaRemoteAddr)
  73. {
  74.     int nResult = false;
  75.     int nRetCode = 0;
  76.     
  77.     ASSERT(psaRemoteAddr != NULL);
  78.     if (NULL == psaRemoteAddr)
  79.         goto Exit0;
  80.     
  81.     ASSERT(IsSocketOK());
  82.     
  83.     if (
  84.         (m_saRemoteAddr.sin_family == psaRemoteAddr->sin_family) && 
  85.         (m_saRemoteAddr.sin_port   == psaRemoteAddr->sin_port) && 
  86.         (m_saRemoteAddr.sin_addr.s_addr == psaRemoteAddr->sin_addr.s_addr)
  87.     )
  88.         nResult = true;
  89.     else
  90.     {
  91.         nRetCode = ::connect(
  92.             m_hSckHandle,
  93.             (sockaddr *)psaRemoteAddr,
  94.             sizeof(INET_ADDRESS)
  95.         );
  96.         nRetCode = CheckError(nRetCode);
  97.         if (0 == nRetCode)
  98.         {
  99.             m_saRemoteAddr = *psaRemoteAddr;
  100.             
  101.             nResult = true;
  102.         }
  103.     }
  104.     
  105. Exit0:
  106.     return nResult;
  107. }
  108. int CStmSocket::Listen(int nBackLog /*=BACK_LOG_DEFAULT*/)
  109. {
  110.     int nResult = false;
  111.     int nRetCode = 0;
  112.     
  113.     ASSERT(IsSocketOK());
  114.     
  115.     nRetCode = ::listen(m_hSckHandle, nBackLog);
  116.     nRetCode = CheckError(nRetCode);
  117.     if (0 == nRetCode)
  118.     {
  119.         nResult = true;
  120.     }
  121.     
  122.     return nResult;
  123. }
  124. int CStmSocket::Accept(HSOCKET *phAcceptSocket)
  125. {
  126.     int nResult = false;
  127.     
  128.     INET_ADDRESS saRemoteAddr = { 0 };
  129.     int nAddrLength = sizeof(saRemoteAddr);
  130.     
  131.     ASSERT(phAcceptSocket != NULL);
  132.     if (NULL == phAcceptSocket)
  133.         goto Exit0;
  134.     
  135.     ASSERT(IsSocketOK());
  136.     
  137.     *phAcceptSocket = ::accept(
  138.         m_hSckHandle,
  139.         (sockaddr *)&saRemoteAddr,
  140.         &nAddrLength
  141.     );
  142.     if (INVALID_SOCKET == *phAcceptSocket)
  143.     {
  144.         CheckError(SOCKET_ERROR);
  145.         goto Exit0;
  146.     }
  147.     
  148.     nResult = true;
  149.     
  150. Exit0:
  151.     return nResult;
  152. }
  153. int CStmSocket::Accept(CStmSocket *pAcceptSocket)
  154. {
  155.     int nResult = false;
  156.     int nRetCode = false;
  157.     
  158.     HSOCKET hAcceptSocket = NULL;
  159.     
  160.     ASSERT(pAcceptSocket != NULL);
  161.     if (NULL == pAcceptSocket)
  162.         goto Exit0;
  163.     
  164.     nRetCode = Accept(&hAcceptSocket);
  165.     if (!nRetCode)
  166.         goto Exit0;
  167.     
  168.     nResult = pAcceptSocket->Attach(hAcceptSocket);
  169.     
  170. Exit0:
  171.     return nResult;
  172. }