UDPSocket.cpp
上传用户:oldpeter23
上传日期:2013-01-09
资源大小:1111k
文件大小:6k
源码类别:

IP电话/视频会议

开发平台:

Visual C++

  1. //NetTalk
  2. /*------------------------------------------------------------------------------*
  3.  =============================
  4.    模块名称: UDPSocket.cpp
  5.  =============================
  6.  
  7.  [版权]
  8.  
  9.    2000-2002  115软件工厂  版权所有
  10.                                               
  11. *------------------------------------------------------------------------------*/
  12. #include <winsock2.h>
  13. #include "UDPSocket.h"
  14. #pragma comment(lib,"Ws2_32")
  15. /*------------------------------------------------------------------------------*/
  16. //初始化
  17. WORD InitializeSocket()
  18. {
  19. WORD wVersionRequested;
  20. WSADATA wsaData;
  21. WORD wVerGet=0;
  22. wVersionRequested=MAKEWORD(2,2);
  23. while(1)
  24. {
  25. int err;
  26. err = WSAStartup(wVersionRequested, &wsaData );
  27. if ( err != 0 ) 
  28. {
  29. BYTE* p=(BYTE*)&wVersionRequested;
  30. if(p[1]>0)
  31. p[1]--;
  32. else
  33. if(p[1]==0)
  34. {
  35. p[1]=1;
  36. if(p[0]>1)
  37. p[0]--;
  38. else
  39. if(p[0]==0)
  40. {
  41. break;
  42. }
  43. }
  44. continue;
  45. }
  46. else
  47. {
  48. wVerGet=wsaData.wVersion;
  49. break;
  50. }
  51. }
  52. return wVerGet;
  53. }
  54. /*------------------------------------------------------------------------------*/
  55. int CleanUpSocket()
  56. {
  57. return WSACleanup();
  58. }
  59. /*------------------------------------------------------------------------------*/
  60. CUDPSocket::CUDPSocket()
  61. {
  62. memset((void*)&m_LocalAddr,0,sizeof(m_LocalAddr));
  63. memset((void*)&m_RemoteAddr,0,sizeof(m_RemoteAddr));
  64. m_Socket=0;
  65. }
  66. /*------------------------------------------------------------------------------*/
  67. CUDPSocket::~CUDPSocket()
  68. {
  69. Destroy();
  70. }
  71. /*------------------------------------------------------------------------------*/
  72. BOOL CUDPSocket::Create(UINT uSocketPort,char* pszSockAddr)
  73. {
  74. BOOL bRet=FALSE;
  75. if(!m_Socket)
  76. {
  77. m_LocalAddr.sin_family=AF_INET;
  78. if(pszSockAddr)
  79. m_LocalAddr.sin_addr.s_addr=Name2Inet(pszSockAddr);
  80. else
  81. m_LocalAddr.sin_addr.s_addr=htonl(INADDR_ANY);
  82. if((m_Socket=socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP))!=INVALID_SOCKET)
  83. {  //创建无连接数据报套接口
  84. m_LocalAddr.sin_port=htons(uSocketPort);
  85. if(bind(m_Socket,(SOCKADDR*)&m_LocalAddr,sizeof(SOCKADDR_IN))==SOCKET_ERROR)
  86. {   
  87. m_Socket=0;
  88. memset((void*)&m_LocalAddr,0,sizeof(m_LocalAddr));
  89. }
  90. else
  91. {
  92. bRet=TRUE;
  93. }
  94. }
  95. }
  96. return bRet;
  97. }
  98. /*------------------------------------------------------------------------------*/
  99. int CUDPSocket::Destroy()
  100. {
  101. int iRet;
  102. shutdown(m_Socket,SD_SEND);
  103. iRet=closesocket(m_Socket);
  104. memset((void*)&m_LocalAddr,0,sizeof(m_LocalAddr));
  105. memset((void*)&m_RemoteAddr,0,sizeof(m_RemoteAddr));
  106. m_Socket=0;
  107. return iRet;
  108. }
  109. /*------------------------------------------------------------------------------*/
  110. int CUDPSocket::SendTo(const char *pBuf, int iLen, char *pszRemoteSockAddr,unsigned short uPort) const
  111. {
  112. SOCKADDR_IN RemoteAddr;
  113. RemoteAddr.sin_family=AF_INET;
  114. RemoteAddr.sin_addr.s_addr=Name2Inet(pszRemoteSockAddr);
  115. RemoteAddr.sin_port=htons(uPort);
  116. return sendto(m_Socket,pBuf,iLen,0,(SOCKADDR*)&RemoteAddr,sizeof(SOCKADDR_IN));
  117. }
  118. /*------------------------------------------------------------------------------*/
  119. int CUDPSocket::SendTo(const char *pBuf,int iLen,SOCKADDR* pRemoteAddr) const
  120. {
  121. if(!pRemoteAddr)
  122. pRemoteAddr=(SOCKADDR*)&m_RemoteAddr;
  123. return sendto(m_Socket,pBuf,iLen,0,pRemoteAddr,sizeof(SOCKADDR_IN));
  124. }
  125. /*------------------------------------------------------------------------------*/
  126. int CUDPSocket::RecvFrom(char *pBuf, int iLen,SOCKADDR* pRemoteAddr) const
  127. {
  128. int addrlen=sizeof(SOCKADDR_IN);
  129. SOCKADDR_IN RemoteAddr;
  130. if(!pRemoteAddr)
  131. {
  132. pRemoteAddr=(SOCKADDR*)&RemoteAddr;
  133. }
  134. return recvfrom(m_Socket,pBuf,iLen,0,pRemoteAddr,&addrlen);
  135. }
  136. /*------------------------------------------------------------------------------*/
  137. SOCKET CUDPSocket::GetSocket() const
  138. {
  139. return m_Socket;
  140. }
  141. /*------------------------------------------------------------------------------*/
  142. SOCKADDR_IN CUDPSocket::GetLocalAddr() const
  143. {
  144. return m_LocalAddr;
  145. }
  146. /*------------------------------------------------------------------------------*/
  147. SOCKADDR_IN CUDPSocket::SetRemoteAddr(const SOCKADDR_IN &NewRemoteAddr)
  148. {
  149. SOCKADDR_IN OldAddr;
  150. OldAddr=m_RemoteAddr;
  151. m_RemoteAddr=NewRemoteAddr;
  152. return OldAddr;
  153. }
  154. /*------------------------------------------------------------------------------*/
  155. SOCKADDR_IN CUDPSocket::GetRemoteAddr() const
  156. {
  157. return m_RemoteAddr;
  158. }
  159. /*------------------------------------------------------------------------------*/
  160. u_long CUDPSocket::GetLocal_in_addr()
  161. {
  162. char szHostname[256];
  163. if (gethostname(szHostname, sizeof(szHostname)))
  164. {
  165. return htonl(INADDR_ANY) ;
  166. }
  167. //得到主机信息
  168. HOSTENT* pHostEnt = gethostbyname(szHostname);
  169. if (pHostEnt == NULL)
  170. {
  171. return htonl(INADDR_ANY);
  172. }
  173. //检查地址长度
  174. if (pHostEnt->h_length != 4)
  175. {
  176. return htonl(INADDR_ANY);
  177. }
  178. return   ((struct in_addr *)*pHostEnt->h_addr_list)->s_addr;
  179. }
  180. /*------------------------------------------------------------------------------*/
  181. u_long CUDPSocket::Name2Inet(LPCSTR szAddress)
  182. {
  183. if(szAddress==NULL||strlen(szAddress)<1)
  184. {
  185. return htonl(INADDR_ANY);
  186. }
  187. BOOL name=FALSE;
  188. for (int i=0;szAddress[i];i++)
  189. {
  190. if((!isdigit(szAddress[i]))&&szAddress[i]!='.')
  191. {
  192. name=TRUE;break;
  193. }
  194. }
  195. //不是计算机名,是IP
  196. if(!name)
  197. return inet_addr(szAddress); 
  198. //是计算机名
  199. else 
  200. {
  201. struct hostent*host=gethostbyname(szAddress);
  202. if(host==NULL)
  203. {
  204. return htonl(INADDR_ANY);
  205. }
  206. return *(long*)host->h_addr;
  207. }
  208. }
  209. /*------------------------------------------------------------------------------*/
  210. BOOL CUDPSocket::IsSocket()
  211. {
  212. return (BOOL)m_Socket;
  213. }