windows udp socket
1 #if !defined(__PRIMITIVESOCKET_H__) 2 #define __PRIMITIVESOCKET_H__ 3 4 #include <WINSOCK2.H> 5 6 #define WM_SOCKET (WM_USER + 100) 7 8 int CommonLib_IsValidIP(char* pstr ); 9 char* CommonLib_Trim(char* p); 10 11 int PrimitiveSocket_Init(void); 12 void PrimitiveSocket_DeInit(void); 13 int PrimitiveSocket_BindSocket(SOCKET s , char* pIPAddr, int nPort,int* pErrorCode ); 14 int PrimitiveSocket_CreateUDPSocket(int* pErrorCode ); 15 int PrimitiveSocket_RecvUDPData( SOCKET s ,char* pBuffer,int pBufferLen , sockaddr_in* pSocketAddr ,int* pErrorCode ); 16 int PrimitiveSocket_SendUDPData(SOCKET s, char* pIPAddr, int nPort, char* pBuffer,int nBufferLen,int* pErrorCode ); 17 int PrimitiveSocket_SetSocketNoBlock(SOCKET s ,int* pErrorCode ); 18 int PrimitiveSocket_CloseSocket(SOCKET s ); 19 int PrimitiveSocket_CreateUDPSocketAndBind(char* pIPAddr, int nPort,int* pErrorCode ); 20 21 // UDP广播 22 // 创建广播SOCKET 23 int PrimitiveSocket_CreateUDPBoardCastSocket(int* pErrorCode ); 24 int PrimitiveSocket_SendUDPData(SOCKET s,int nPort, char* pBuffer,int nBufferLen,int* pErrorCode ); 25 26 27 28 // TCP相关 29 int PrimitiveSocket_CreateTCPSocket(int* pErrorCode ); 30 int PrimitiveSocket_CreateTCPSocketAndListen(char* pIPAddr, int nListenPort,int IsBlock, int* pErrorCode ); 31 32 #endif // !defined(__PRIMITIVESOCKET_H__) 33 34 35 36 37 38 #include "stdafx.h" 39 #include "PrimitiveSocket.h" 40 #include "ComonLib.h" 41 42 #include <WINSOCK2.H> 43 #pragma comment(lib,"WS2_32") 44 45 static int socketlib_inited=0; 46 47 48 49 50 51 int PrimitiveSocket_Init(void) 52 { 53 WSADATA wd = {0}; 54 int rc ; 55 if( socketlib_inited == 1 ) 56 { 57 return 0; 58 } 59 rc = WSAStartup(MAKEWORD(2,2 ),&wd); 60 if( rc == 0 ) 61 { 62 socketlib_inited=1; 63 } 64 return rc; 65 } 66 67 68 void PrimitiveSocket_DeInit(void) 69 { 70 if( socketlib_inited ) 71 { 72 socketlib_inited=0; 73 WSACleanup(); 74 } 75 } 76 77 78 // 创建UDP SOCKET 79 int PrimitiveSocket_CreateUDPSocket(int* pErrorCode ) 80 { 81 SOCKET s = INVALID_SOCKET; 82 if( socketlib_inited == 0 ) 83 { 84 PrimitiveSocket_Init(); 85 } 86 s = socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP); 87 if ( INVALID_SOCKET == s ) 88 { 89 if( pErrorCode != NULL ) 90 { 91 *pErrorCode = WSAGetLastError(); 92 } 93 return INVALID_SOCKET; 94 } 95 return s; 96 } 97 98 99 int PrimitiveSocket_CreateTCPSocket(int* pErrorCode ) 100 { 101 SOCKET s = INVALID_SOCKET; 102 if( socketlib_inited == 0 ) 103 { 104 PrimitiveSocket_Init(); 105 } 106 s = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); 107 if ( INVALID_SOCKET == s ) 108 { 109 if( pErrorCode != NULL ) 110 { 111 *pErrorCode = WSAGetLastError(); 112 } 113 return INVALID_SOCKET; 114 } 115 return s; 116 } 117 118 119 int PrimitiveSocket_BindLocalAddr(SOCKET s , int nPort,int* pErrorCode ) 120 { 121 sockaddr_in addr = {0}; 122 int nBind ; 123 if( s == INVALID_SOCKET ) 124 { 125 return -1; 126 } 127 addr.sin_family = AF_INET; 128 addr.sin_port = htons(nPort); 129 addr.sin_addr.s_addr = inet_addr(0); 130 131 nBind = bind( s ,(sockaddr *)&addr,sizeof(addr)); 132 if (0 != nBind) 133 { 134 if( pErrorCode != NULL ) 135 { 136 *pErrorCode = WSAGetLastError(); 137 } 138 return -1; 139 } 140 return nBind; 141 } 142 143 144 // 绑定SOCKET 145 int PrimitiveSocket_BindSocket(SOCKET s , char* pIPAddr, int nPort,int* pErrorCode ) 146 { 147 sockaddr_in addr = {0}; 148 int nBind ; 149 if( s == INVALID_SOCKET ) 150 { 151 return -1; 152 } 153 if( !CommonLib_IsValidIP(pIPAddr )) 154 { 155 return -1; 156 } 157 addr.sin_family = AF_INET; 158 addr.sin_port = htons(nPort); 159 addr.sin_addr.s_addr = inet_addr(pIPAddr); 160 161 nBind = bind( s ,(sockaddr *)&addr,sizeof(addr)); 162 if ( nBind < 0 ) 163 { 164 if( pErrorCode != NULL ) 165 { 166 *pErrorCode = WSAGetLastError(); 167 } 168 return -1; 169 } 170 return nBind; 171 } 172 173 174 // 接收UDP数据 175 int PrimitiveSocket_RecvUDPData( SOCKET s ,char* pBuffer,int nBufferLen , sockaddr_in* pSocketAddr ,int* pErrorCode ) 176 { 177 sockaddr_in Mysockaddr = {0}; 178 int nFromLen = sizeof(sockaddr); 179 int len = recvfrom( s , pBuffer , nBufferLen ,0 , (sockaddr *)&Mysockaddr,&nFromLen); 180 if( len > 0 ) 181 { 182 if( pSocketAddr != NULL ) 183 { 184 *pSocketAddr = Mysockaddr; 185 } 186 } 187 else 188 { 189 *pErrorCode = WSAGetLastError(); 190 } 191 return len ; 192 } 193 194 195 int PrimitiveSocket_SendUDPData(SOCKET s, char* pIPAddr, int nPort, char* pBuffer,int nBufferLen ,int* pErrorCode ) 196 { 197 sockaddr_in addr = {0}; 198 int nSendLen = 0; 199 if( s == INVALID_SOCKET ) 200 { 201 return -1; 202 } 203 if( !CommonLib_IsValidIP(pIPAddr )) 204 { 205 return -1; 206 } 207 addr.sin_family = AF_INET; 208 addr.sin_port = htons(nPort); 209 addr.sin_addr.s_addr = inet_addr(pIPAddr); 210 211 nSendLen = sendto(s ,pBuffer, nBufferLen ,0,(sockaddr *)&addr,sizeof(sockaddr)); 212 if( nSendLen < 0 ) 213 { 214 if( pErrorCode != NULL ) 215 { 216 *pErrorCode = WSAGetLastError(); 217 } 218 } 219 return nSendLen; 220 } 221 222 223 int PrimitiveSocket_SetSocketNoBlock(SOCKET s ,int* pErrorCode ) 224 { 225 //设置为非阻塞方式连接 226 unsigned long ul = 1; 227 int ret = ioctlsocket(s, FIONBIO, (unsigned long*)&ul); 228 if(ret == SOCKET_ERROR) 229 { 230 if( pErrorCode != NULL ) 231 { 232 *pErrorCode = WSAGetLastError(); 233 } 234 return -1; 235 } 236 return ret; 237 } 238 239 240 int PrimitiveSocket_CloseSocket(SOCKET s ) 241 { 242 if( s != INVALID_SOCKET ) 243 { 244 shutdown(s,SD_BOTH); 245 closesocket(s); 246 return 0; 247 } 248 return -1; 249 } 250 251 int PrimitiveSocket_CreateUDPSocketAndBind(char* pIPAddr, int nPort,int* pErrorCode ) 252 { 253 SOCKET socketLocal; 254 int nErrorCode = 0; 255 int rc ; 256 PrimitiveSocket_Init(); 257 258 socketLocal = PrimitiveSocket_CreateUDPSocket( &nErrorCode ); 259 if( socketLocal < 0 ) 260 { 261 PrimitiveSocket_DeInit(); 262 if( pErrorCode != NULL ) 263 { 264 *pErrorCode = nErrorCode; 265 } 266 return -1; 267 } 268 269 rc = PrimitiveSocket_BindSocket( socketLocal ,pIPAddr , nPort , &nErrorCode ); 270 if( rc < 0 ) 271 { 272 PrimitiveSocket_CloseSocket(socketLocal); 273 PrimitiveSocket_DeInit(); 274 if( pErrorCode != NULL ) 275 { 276 *pErrorCode = nErrorCode; 277 } 278 return -1; 279 } 280 return socketLocal; 281 } 282 283 284 int PrimitiveSocket_CreateTCPSocketAndListen(char* pIPAddr, int nListenPort,int IsBlock, int* pErrorCode ) 285 { 286 SOCKET socketLocal; 287 int nErrorCode = 0; 288 int rc ; 289 PrimitiveSocket_Init(); 290 291 socketLocal = PrimitiveSocket_CreateTCPSocket( &nErrorCode ); 292 if( socketLocal < 0 ) 293 { 294 PrimitiveSocket_DeInit(); 295 return -1; 296 } 297 298 if( IsBlock ) 299 { 300 rc = PrimitiveSocket_SetSocketNoBlock(socketLocal ,&nErrorCode ); 301 if( rc < 0 ) 302 { 303 return -1; 304 } 305 } 306 rc = PrimitiveSocket_BindSocket( socketLocal ,pIPAddr , nListenPort , &nErrorCode ); 307 if( rc < 0 ) 308 { 309 PrimitiveSocket_CloseSocket(socketLocal); 310 PrimitiveSocket_DeInit(); 311 return -1; 312 } 313 314 rc = listen(socketLocal,5); 315 if( rc < 0 ) 316 { 317 PrimitiveSocket_CloseSocket(socketLocal); 318 PrimitiveSocket_DeInit(); 319 return -1; 320 } 321 return socketLocal; 322 } 323 324 325 326 // 创建广播SOCKET 327 int PrimitiveSocket_CreateUDPBoardCastSocket(int* pErrorCode ) 328 { 329 int nErrorCode = 0; 330 int rc ; 331 const int broadcastOpt = 1; 332 int sock = PrimitiveSocket_CreateUDPSocket(&nErrorCode ); 333 if( sock < 0 ) 334 { 335 if( pErrorCode != NULL ) 336 { 337 *pErrorCode = nErrorCode; 338 } 339 return -1; 340 } 341 rc = setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(char *)&broadcastOpt,sizeof(broadcastOpt)); 342 if( rc < 0 ) 343 { 344 nErrorCode = WSAGetLastError(); 345 PrimitiveSocket_CloseSocket(sock); 346 if( pErrorCode != NULL ) 347 { 348 *pErrorCode = nErrorCode; 349 } 350 return -1; 351 } 352 return sock; 353 } 354 355 int PrimitiveSocket_SendUDPData(SOCKET s,int nPort, char* pBuffer,int nBufferLen,int* pErrorCode ) 356 { 357 struct sockaddr_in addrto={0}; 358 int nErrorCode = 0; 359 int len ; 360 361 addrto.sin_family=AF_INET; 362 addrto.sin_addr.s_addr=htonl(INADDR_BROADCAST); 363 addrto.sin_port=htons(nPort); 364 365 len = sendto(s ,pBuffer , nBufferLen ,0,(sockaddr*)&addrto,sizeof(addrto) ); 366 if( len < 0 ) 367 { 368 nErrorCode = WSAGetLastError(); 369 if( pErrorCode != NULL ) 370 { 371 *pErrorCode = nErrorCode; 372 } 373 } 374 return len; 375 }
联系方式:heshengjun@tinywsn.com