다음을 통해 공유


EnumProtocolsA 함수(nspapi.h)

EnumProtocols 함수는 로컬 호스트에서 활성 상태인 지정된 네트워크 프로토콜 집합에 대한 정보를 검색합니다.

참고EnumProtocols 함수는 Windows Sockets 1.1 사양에 대한 Microsoft 전용 확장입니다. 이 함수는 사용되지 않습니다. Windows Sockets 1.1 개발자의 편의를 위해 참조 자료가 포함되어 있습니다. WSAEnumProtocols 함수는 Windows 소켓 2에서 동등한 기능을 제공합니다.
 

구문

INT EnumProtocolsA(
  [in, optional] LPINT   lpiProtocols,
  [out]          LPVOID  lpProtocolBuffer,
  [in, out]      LPDWORD lpdwBufferLength
);

매개 변수

[in, optional] lpiProtocols

프로토콜 식별자의 null로 끝나는 배열에 대한 포인터입니다. EnumProtocols 함수는 이 배열에서 지정한 프로토콜에 대한 정보를 검색합니다.

lpiProtocolsNULL인 경우 함수는 사용 가능한 모든 프로토콜에 대한 정보를 검색합니다.

다음 프로토콜 식별자 값이 정의됩니다.

의미
IPPROTO_TCP
연결 지향 스트림 프로토콜인 TCP(Transmission Control Protocol)입니다.
IPPROTO_UDP
연결 없는 데이터그램 프로토콜인 UDP(사용자 데이터그램 프로토콜)입니다.
ISOPROTO_TP4
ISO 연결 지향 전송 프로토콜입니다.
NSPROTO_IPX
IPX(인터넷 패킷 교환) 프로토콜, 연결 없는 데이터그램 프로토콜입니다.
NSPROTO_SPX
연결 지향 스트림 프로토콜인 SPX(Sequenced Packet Exchange) 프로토콜입니다.
NSPROTO_SPXII
연결 지향 스트림 프로토콜인 SPX(Sequenced Packet Exchange) 프로토콜 버전 2입니다.

[out] lpProtocolBuffer

함수가 PROTOCOL_INFO 데이터 구조의 배열로 채우는 버퍼에 대한 포인터입니다.

[in, out] lpdwBufferLength

입력 시 lpProtocolBuffer가 가리키는 버퍼의 크기를 바이트 단위로 지정하는 변수에 대한 포인터입니다.

출력 시 함수는 이 변수를 요청된 모든 정보를 검색하는 데 필요한 최소 버퍼 크기로 설정합니다. 함수가 성공하려면 버퍼가 적어도 이 크기여야 합니다.

반환 값

함수가 성공하면 반환 값은 lpProtocolBuffer가 가리키는 버퍼에 기록된 PROTOCOL_INFO 데이터 구조의 수입니다.

함수가 실패하면 반환 값은 SOCKET_ERROR(–1)입니다. 확장 오류 정보를 얻으려면 다음 확장 오류 코드를 반환하는 GetLastError를 호출합니다.

오류 코드 의미
ERROR_INSUFFICIENT_BUFFER
lpProtocolBuffer가 가리키는 버퍼가 너무 작아 관련 PROTOCOL_INFO 구조를 모두 받을 수 없습니다. *lpdwBufferLength에서 반환된 값만큼 버퍼를 사용하여 함수를 호출합니다.

설명

다음 샘플 코드에서 EnumProtocols 함수는 시스템에서 사용할 수 있는 모든 프로토콜에 대한 정보를 검색합니다. 그런 다음 코드는 각 프로토콜을 자세히 검사합니다.

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <Nspapi.h>
#include <stdlib.h>
#include <stdio.h>


// Need to link with Ws2_32.lib and Mswsock.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")

int FindProtocol(BOOL Reliable, 
    BOOL MessageOriented, BOOL StreamOriented, 
    BOOL Connectionless, DWORD *ProtocolUsed); 

int __cdecl main(int argc, char **argv)
{
    WSADATA wsaData;

    int ProtocolError = SOCKET_ERROR;
    int iResult;
    
    BOOLEAN bReliable = FALSE;
    BOOLEAN bMessageOriented = FALSE;
    BOOLEAN bStreamOriented = TRUE;
    BOOLEAN bConnectionless = FALSE;
    DWORD *pProtocols = NULL;
    
    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s servicename\n", argv[0]);
        return 1;
    }

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    }
    
    ProtocolError = FindProtocol( bReliable, bMessageOriented,
        bStreamOriented, bConnectionless, pProtocols);
    if (ProtocolError == SOCKET_ERROR) {
        printf("Unable to find a protocol to support the parameters requested\n");
        return 1;
    }
    
    // Connect to the servicename ...    
    
    return 0;

}

#define MAX_PROTOCOLS 1024

int FindProtocol ( 
    BOOL Reliable, 
    BOOL MessageOriented, 
    BOOL StreamOriented, 
    BOOL Connectionless, 
    DWORD *ProtocolUsed 
    ) 
{ 
    // local variables 
    INT protocols[MAX_PROTOCOLS+1]; 
    BYTE buffer[2048]; 
    DWORD bytesRequired; 
    INT err; 
    PPROTOCOL_INFO protocolInfo; 
    INT protocolCount; 
    INT i; 
    DWORD protocolIndex; 
//    PCSADDR_INFO csaddrInfo; 
//    INT addressCount; 
//    SOCKET s; 
 
    // First look up the protocols installed on this computer. 
    // 
    bytesRequired = sizeof(buffer); 
    err = EnumProtocols( NULL, buffer, &bytesRequired ); 
    if ( err <= 0 ) 
        return SOCKET_ERROR; 
 
    // Walk through the available protocols and pick out the ones which 
    // support the desired characteristics. 
    // 
    protocolCount = err; 
    protocolInfo = (PPROTOCOL_INFO)buffer; 
 
    for ( i = 0, protocolIndex = 0; 
        i < protocolCount && protocolIndex < MAX_PROTOCOLS; 
        i++, protocolInfo++ ) { 
 
        // If connection-oriented support is requested, then check if 
        // supported by this protocol.  We assume here that connection- 
        // oriented support implies fully reliable service. 
        // 
 
        if ( Reliable ) { 
            // Check to see if the protocol is reliable.  It must 
            // guarantee both delivery of all data and the order in 
            // which the data arrives. 
            // 
            if ( (protocolInfo->dwServiceFlags & 
                    XP_GUARANTEED_DELIVERY) == 0 
                || 
                    (protocolInfo->dwServiceFlags & 
                    XP_GUARANTEED_ORDER) == 0 ) { 
 
                continue; 
            } 
 
            // Check to see that the protocol matches the stream/message 
            // characteristics requested. 
            // 
            if ( StreamOriented && 
                (protocolInfo->dwServiceFlags & XP_MESSAGE_ORIENTED) 
                    != 0 && 
                (protocolInfo->dwServiceFlags & XP_PSEUDO_STREAM) 
                     == 0 ) { 
                continue; 
            } 
 
            if ( MessageOriented && 
                    (protocolInfo->dwServiceFlags & XP_MESSAGE_ORIENTED) 
                              == 0 ) { 
                continue; 
            } 
 
        } 
        else if ( Connectionless ) { 
            // Make sure that this is a connectionless protocol. 
            // 
            if ( (protocolInfo->dwServiceFlags & XP_CONNECTIONLESS) 
                     != 0 ) 
                continue; 
        } 
 
        // This protocol fits all the criteria.  Add it to the list of 
        // protocols in which we're interested. 
        // 
        protocols[protocolIndex++] = protocolInfo->iProtocol; 
     }

     *ProtocolUsed = (INT) protocolIndex;
     return 0;
}

참고

nspapi.h 헤더는 ENumProtocols를 유니코드 전처리기 상수의 정의에 따라 이 함수의 ANSI 또는 유니코드 버전을 자동으로 선택하는 별칭으로 정의합니다. 인코딩 중립 별칭을 인코딩 중립이 아닌 코드와 혼합하면 컴파일 또는 런타임 오류가 발생하는 불일치가 발생할 수 있습니다. 자세한 내용은 함수 프로토타입에 대한 규칙을 참조하세요.

요구 사항

요구 사항
지원되는 최소 클라이언트 Windows 2000 Professional[데스크톱 앱만]
지원되는 최소 서버 Windows 2000 Server[데스크톱 앱만]
대상 플랫폼 Windows
헤더 nspapi.h
라이브러리 Mswsock.lib
DLL Mswsock.dll

추가 정보

GetAddressByName

PROTOCOL_INFO

Winsock 함수

Winsock 참조