ASSOCIATE_NAMERES_CONTEXT_INPUT 結構 (mstcpip.h)
ASSOCIATE_NAMERES_CONTEXT_INPUT結構包含完整功能變數名稱的傳輸設定標識碼和句柄。
語法
typedef struct _ASSOCIATE_NAMERES_CONTEXT_INPUT {
TRANSPORT_SETTING_ID TransportSettingId;
UINT64 Handle;
} ASSOCIATE_NAMERES_CONTEXT_INPUT, *PASSOCIATE_NAMERES_CONTEXT_INPUT;
成員
TransportSettingId
傳輸設定標識碼。
Handle
完整功能變數名稱的句柄。
備註
一般而言,您可以使用 ASSOCIATE_NAMERES_CONTEXT_INPUT ,根據完整功能變數名稱 (FQDN) 強制執行原則,而不只是IP位址。 您可以使用 addinfoex4 結構,透過呼叫 GetAddrInfoEx 來擷取 FQDN 的句柄,即可這麼做。 您可以從該處使用 ASSOCIATE_NAMERES_CONTEXT_INPUT 中的 句柄,並使用 SIO_APPLY_TRANSPORT_SETTINGioctl 呼叫 WSAIoctl。
範例
下列程式代碼描述使用 addinfoex4 結構呼叫 GetAddrInfoEx ,以擷取 FQDN 的句柄。 然後範例會使用 ASSOCIATE_NAMERES_CONTEXT_INPUT 結構呼叫 WSAIoctl。
//
// Connect to a server using its IPv4 addresses
//
VOID
ConnectServer(
PCWSTR server)
{
int iResult;
PADDRINFOEX4 pResult = NULL;
ADDRINFOEX3 hints = { 0 };
PADDRINFOEX4 pCur = NULL;
WSADATA wsaData;
SOCKET connectSocket = INVALID_SOCKET;
ULONG bytesReturned = 0;
ASSOCIATE_NAMERES_CONTEXT_INPUT input = { 0 };
SOCKADDR_IN clientService;
wchar_t ipstringbuffer[46];
String string;
DWORD dwRetval;
//
// Initialize Winsock
//
iResult = WSAStartup(
MAKEWORD(2, 2),
&wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
goto Exit;
}
//
// Create a SOCKET for connection
//
connectSocket = socket(
AF_UNSPEC,
SOCK_STREAM,
IPPROTO_TCP);
if (connectSocket == INVALID_SOCKET)
{
printf("socket failed: %d\n", WSAGetLastError());
goto Exit;
}
//
// Do name resolution
//
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_EXTENDED | AI_FQDN | AI_CANONNAME | AI_RESOLUTION_HANDLE;
hints.ai_version = ADDRINFOEX_VERSION_4;
dwRetval = GetAddrInfoExW(
server,
NULL,
NS_DNS,
NULL,
(const ADDRINFOEXW*)&hints,
(PADDRINFOEXW*)&pResult,
NULL,
NULL,
NULL, NULL);
if (dwRetval != 0) {
printf("GetAddrInfoEx failed with error: %d\n", dwRetval);
goto Exit;
}
input.TransportSettingId.Guid = ASSOCIATE_NAMERES_CONTEXT;
input.Handle = pResult->ai_resolutionhandle;
//
// Associate socket with the handle
//
if (WSAIoctl(
connectSocket,
SIO_APPLY_TRANSPORT_SETTING,
(VOID *)&input,
sizeof(input),
NULL,
0,
&bytesReturned,
NULL,
NULL) == SOCKET_ERROR)
if (iResult != 0){
printf("WSAIoctl failed: %d\n", WSAGetLastError());
goto Exit;
}
//
// Connect to server
//
pCur = pResult;
while (pCur != NULL)
{
if (pCur->ai_addr->sa_family == AF_INET)
{
clientService = *(const sockaddr_in*)pCur->ai_addr;
clientService.sin_port = htons(80);
if (connect(
connectSocket,
(const SOCKADDR *)&clientService,
sizeof(clientService)) == SOCKET_ERROR)
{
printf("connect failed: %d\n", WSAGetLastError());
goto Exit;
}
}
pCur = pCur->ai_next;
}
Exit:
if (connectSocket != INVALID_SOCKET)
{
closesocket(connectSocket);
}
if (pResult)
{
FreeAddrInfoExW((ADDRINFOEXW*)pResult);
}
WSACleanup();
return;
}
規格需求
需求 | 值 |
---|---|
最低支援的用戶端 | Windows 10 [僅限傳統型應用程式] |
最低支援的伺服器 | Windows Server 2016 [僅限傳統型應用程式] |
標頭 | mstcpip.h |