当前位置:   article > 正文

网络编程Winsock——socket_winsock和socket的区别

winsock和socket的区别

只列举了部分参数

------------------------

socket 创建套接字。

  1. SOCKET WSAAPI socket(
  2. _In_ int af,//地址族或者协议族,两者一个意思,随便用那种都可以。AF_INET是IPv4,AF_INET6是IPv6
  3. _In_ int type,//
  4. _In_ int protocol
  5. );
参数一:

MSDN的英文API中有这样一句“Note that the values for the AF_ address family and PF_ protocol family constants are identical (for example,AF_INET andPF_INET), so either constant can be used.”意思就是说,用地址族还是协议族差不多一个意思。

MSDN的句子“Applications are encouraged to use AF_INET6 for the af parameter and create a dual-mode socket that can be used with both IPv4 and IPv6.”这句话表示:

强烈建议使用AF_INET6,它可以适用于IPv4和IPv6。

参数二:

SOCK_STREAMTCPIPv4 IPv6
SOCK_DGRAMUDPIPv4 IPv6
SOCK_RDM  

对于SOCK_RAW,要操作IPv4的头要使用IP_HDRINCL,IPv6则是IPV6_HDRINCL。

参数三:

AF_UNSPEC是强烈不建议使用的。

socket创建的默认是有重叠属性的,WSASocket可以创建非重叠的套接字


在MSDN上有一段是这样的:

When selecting a protocol and its supporting service provider this procedure will only choose a base protocol or a protocol chain, not a protocol layer by itself. Unchained protocol layers are not considered to have partial matches ontype oraf either. That is, they do not lead to an error code of WSAEAFNOSUPPORT or WSAEPROTONOSUPPORT if no suitable protocol is found.

不知道这一段在说什么,但是感觉很厉害的样子。


当使用SOCK_RAW的时候,IPv4和IPv6的操作会有不同。而且在NT平台,使用原始套接字是需要管理员权限的。


显示到底哪些服务、协议、套接字类型、地址族可以使用:

netsh winsock show catalog 


下面是一个来自微软MSDN的示例代码:(http://msdn.microsoft.com/en-us/library/windows/desktop/ms740506%28v=vs.85%29.aspx

  1. #ifndef UNICODE
  2. #define UNICODE 1
  3. #endif
  4. // link with Ws2_32.lib
  5. #pragma comment(lib,"Ws2_32.lib")
  6. #include <winsock2.h>
  7. #include <ws2tcpip.h>
  8. #include <stdio.h>
  9. #include <stdlib.h> // Needed for _wtoi
  10. int __cdecl wmain(int argc, wchar_t **argv)
  11. {
  12. //-----------------------------------------
  13. // Declare and initialize variables
  14. WSADATA wsaData = {0};
  15. int iResult = 0;
  16. // int i = 1;
  17. SOCKET sock = INVALID_SOCKET;
  18. int iFamily = AF_UNSPEC;
  19. int iType = 0;
  20. int iProtocol = 0;
  21. // Validate the parameters
  22. if (argc != 4) {
  23. wprintf(L"usage: %s <addressfamily> <type> <protocol>\n", argv[0]);
  24. wprintf(L"socket opens a socket for the specified family, type, & protocol\n");
  25. wprintf(L"%ws example usage\n", argv[0]);
  26. wprintf(L" %ws 0 2 17\n", argv[0]);
  27. wprintf(L" where AF_UNSPEC=0 SOCK_DGRAM=2 IPPROTO_UDP=17\n", argv[0]);
  28. return 1;
  29. }
  30. iFamily = _wtoi(argv[1]);
  31. iType = _wtoi(argv[2]);
  32. iProtocol = _wtoi(argv[3]);
  33. // Initialize Winsock
  34. iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
  35. if (iResult != 0) {
  36. wprintf(L"WSAStartup failed: %d\n", iResult);
  37. return 1;
  38. }
  39. wprintf(L"Calling socket with following parameters:\n");
  40. wprintf(L" Address Family = ");
  41. switch (iFamily) {
  42. case AF_UNSPEC:
  43. wprintf(L"Unspecified");
  44. break;
  45. case AF_INET:
  46. wprintf(L"AF_INET (IPv4)");
  47. break;
  48. case AF_INET6:
  49. wprintf(L"AF_INET6 (IPv6)");
  50. break;
  51. case AF_NETBIOS:
  52. wprintf(L"AF_NETBIOS (NetBIOS)");
  53. break;
  54. case AF_BTH:
  55. wprintf(L"AF_BTH (Bluetooth)");
  56. break;
  57. default:
  58. wprintf(L"Other");
  59. break;
  60. }
  61. wprintf(L" (%d)\n", iFamily);
  62. wprintf(L" Socket type = ");
  63. switch (iType) {
  64. case 0:
  65. wprintf(L"Unspecified");
  66. break;
  67. case SOCK_STREAM:
  68. wprintf(L"SOCK_STREAM (stream)");
  69. break;
  70. case SOCK_DGRAM:
  71. wprintf(L"SOCK_DGRAM (datagram)");
  72. break;
  73. case SOCK_RAW:
  74. wprintf(L"SOCK_RAW (raw)");
  75. break;
  76. case SOCK_RDM:
  77. wprintf(L"SOCK_RDM (reliable message datagram)");
  78. break;
  79. case SOCK_SEQPACKET:
  80. wprintf(L"SOCK_SEQPACKET (pseudo-stream packet)");
  81. break;
  82. default:
  83. wprintf(L"Other");
  84. break;
  85. }
  86. wprintf(L" (%d)\n", iType);
  87. wprintf(L" Protocol = %d = ", iProtocol);
  88. switch (iProtocol) {
  89. case 0:
  90. wprintf(L"Unspecified");
  91. break;
  92. case IPPROTO_ICMP:
  93. wprintf(L"IPPROTO_ICMP (ICMP)");
  94. break;
  95. case IPPROTO_IGMP:
  96. wprintf(L"IPPROTO_IGMP (IGMP)");
  97. break;
  98. case IPPROTO_TCP:
  99. wprintf(L"IPPROTO_TCP (TCP)");
  100. break;
  101. case IPPROTO_UDP:
  102. wprintf(L"IPPROTO_UDP (UDP)");
  103. break;
  104. case IPPROTO_ICMPV6:
  105. wprintf(L"IPPROTO_ICMPV6 (ICMP Version 6)");
  106. break;
  107. default:
  108. wprintf(L"Other");
  109. break;
  110. }
  111. wprintf(L" (%d)\n", iProtocol);
  112. sock = socket(iFamily, iType, iProtocol);
  113. if (sock == INVALID_SOCKET)
  114. wprintf(L"socket function failed with error = %d\n", WSAGetLastError() );
  115. else {
  116. wprintf(L"socket function succeeded\n");
  117. // Close the socket to release the resources associated
  118. // Normally an application calls shutdown() before closesocket
  119. // to disables sends or receives on a socket first
  120. // This isn't needed in this simple sample
  121. iResult = closesocket(sock);
  122. if (iResult == SOCKET_ERROR) {
  123. wprintf(L"closesocket failed with error = %d\n", WSAGetLastError() );
  124. WSACleanup();
  125. return 1;
  126. }
  127. }
  128. WSACleanup();
  129. return 0;
  130. }
转载请注明出处: http://blog.csdn.net/wlsgzl/article/details/17044901

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/147130
推荐阅读
相关标签
  

闽ICP备14008679号