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

模拟服务器

开发平台:

C/C++

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