赞
踩
只列举了部分参数
------------------------
socket 创建套接字。
- SOCKET WSAAPI socket(
- _In_ int af,//地址族或者协议族,两者一个意思,随便用那种都可以。AF_INET是IPv4,AF_INET6是IPv6
- _In_ int type,//
- _In_ int protocol
- );
参数一:
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_STREAM | TCP | IPv4 IPv6 |
SOCK_DGRAM | UDP | IPv4 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)
- #ifndef UNICODE
- #define UNICODE 1
- #endif
-
- // link with Ws2_32.lib
- #pragma comment(lib,"Ws2_32.lib")
-
- #include <winsock2.h>
- #include <ws2tcpip.h>
- #include <stdio.h>
- #include <stdlib.h> // Needed for _wtoi
-
-
- int __cdecl wmain(int argc, wchar_t **argv)
- {
-
- //-----------------------------------------
- // Declare and initialize variables
- WSADATA wsaData = {0};
- int iResult = 0;
-
- // int i = 1;
-
- SOCKET sock = INVALID_SOCKET;
- int iFamily = AF_UNSPEC;
- int iType = 0;
- int iProtocol = 0;
-
- // Validate the parameters
- if (argc != 4) {
- wprintf(L"usage: %s <addressfamily> <type> <protocol>\n", argv[0]);
- wprintf(L"socket opens a socket for the specified family, type, & protocol\n");
- wprintf(L"%ws example usage\n", argv[0]);
- wprintf(L" %ws 0 2 17\n", argv[0]);
- wprintf(L" where AF_UNSPEC=0 SOCK_DGRAM=2 IPPROTO_UDP=17\n", argv[0]);
- return 1;
- }
-
- iFamily = _wtoi(argv[1]);
- iType = _wtoi(argv[2]);
- iProtocol = _wtoi(argv[3]);
-
- // Initialize Winsock
- iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (iResult != 0) {
- wprintf(L"WSAStartup failed: %d\n", iResult);
- return 1;
- }
-
- wprintf(L"Calling socket with following parameters:\n");
- wprintf(L" Address Family = ");
- switch (iFamily) {
- case AF_UNSPEC:
- wprintf(L"Unspecified");
- break;
- case AF_INET:
- wprintf(L"AF_INET (IPv4)");
- break;
- case AF_INET6:
- wprintf(L"AF_INET6 (IPv6)");
- break;
- case AF_NETBIOS:
- wprintf(L"AF_NETBIOS (NetBIOS)");
- break;
- case AF_BTH:
- wprintf(L"AF_BTH (Bluetooth)");
- break;
- default:
- wprintf(L"Other");
- break;
- }
- wprintf(L" (%d)\n", iFamily);
-
- wprintf(L" Socket type = ");
- switch (iType) {
- case 0:
- wprintf(L"Unspecified");
- break;
- case SOCK_STREAM:
- wprintf(L"SOCK_STREAM (stream)");
- break;
- case SOCK_DGRAM:
- wprintf(L"SOCK_DGRAM (datagram)");
- break;
- case SOCK_RAW:
- wprintf(L"SOCK_RAW (raw)");
- break;
- case SOCK_RDM:
- wprintf(L"SOCK_RDM (reliable message datagram)");
- break;
- case SOCK_SEQPACKET:
- wprintf(L"SOCK_SEQPACKET (pseudo-stream packet)");
- break;
- default:
- wprintf(L"Other");
- break;
- }
- wprintf(L" (%d)\n", iType);
-
- wprintf(L" Protocol = %d = ", iProtocol);
- switch (iProtocol) {
- case 0:
- wprintf(L"Unspecified");
- break;
- case IPPROTO_ICMP:
- wprintf(L"IPPROTO_ICMP (ICMP)");
- break;
- case IPPROTO_IGMP:
- wprintf(L"IPPROTO_IGMP (IGMP)");
- break;
- case IPPROTO_TCP:
- wprintf(L"IPPROTO_TCP (TCP)");
- break;
- case IPPROTO_UDP:
- wprintf(L"IPPROTO_UDP (UDP)");
- break;
- case IPPROTO_ICMPV6:
- wprintf(L"IPPROTO_ICMPV6 (ICMP Version 6)");
- break;
- default:
- wprintf(L"Other");
- break;
- }
- wprintf(L" (%d)\n", iProtocol);
-
- sock = socket(iFamily, iType, iProtocol);
- if (sock == INVALID_SOCKET)
- wprintf(L"socket function failed with error = %d\n", WSAGetLastError() );
- else {
- wprintf(L"socket function succeeded\n");
-
- // Close the socket to release the resources associated
- // Normally an application calls shutdown() before closesocket
- // to disables sends or receives on a socket first
- // This isn't needed in this simple sample
- iResult = closesocket(sock);
- if (iResult == SOCKET_ERROR) {
- wprintf(L"closesocket failed with error = %d\n", WSAGetLastError() );
- WSACleanup();
- return 1;
- }
- }
-
- WSACleanup();
-
- return 0;
- }
转载请注明出处:
http://blog.csdn.net/wlsgzl/article/details/17044901
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。