winsock(一)

// win32console.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"


// Module Name: Enum.c
//
// Description:
//    Enumerate the protocol entries and print their characteristics.
//
// Compile:
//    cl -o enum enum.c ws2_32.lib
//
// Command Line Options:
//    NONE
//
//#include <windows.h>
#include <winsock2.h>
#include <stdio.h>
#include <stdlib.h>


//#define _WIN32_WINNT


#include "af_irda.h"


#include <ws2atm.h>
#include <wsipx.h>
#include "atalkwsh.h"
#pragma comment (lib,"Ws2_32.lib")

// Function: EnumearteProtocols
//
// Description:
//    Perform the actual enumeration of protocol entries stored by
//    Winsock.
//
BOOL EnumerateProtocols(WSAPROTOCOL_INFO **wsapi, DWORD *dwCount)
{
    DWORD             dwErr,
                      dwRet,
                      dwBufLen=0;
    int               i;


    *dwCount = 0;
    *wsapi = NULL;
    if (SOCKET_ERROR != WSAEnumProtocols(NULL, *wsapi, 
        &dwBufLen))
    {
        // This should never happen as there is a NULL buffer
        //
printf("WSAEnumProtocols failed!\n");
return FALSE;
    }
    else if (WSAENOBUFS != (dwErr = WSAGetLastError()))
    {
// We failed for some reason not relating to buffer size - 
        // also odd
        //
printf("WSAEnumProtocols failed: %d\n", dwErr);
return FALSE;
    }
    // Allocate the correct buffer size for WSAEnumProtocols as
    // well as the buffer to return
    //
    *wsapi = (WSAPROTOCOL_INFO *) GlobalAlloc(GMEM_FIXED, dwBufLen);


    if (*wsapi == NULL)
    {
printf("GlobalAlloc failed: %d\n", GetLastError());
return FALSE;
    }
    dwRet = WSAEnumProtocols(NULL, *wsapi, &dwBufLen);
    if (dwRet == SOCKET_ERROR)
    {
printf("WSAEnumProtocols failed: %d\n", WSAGetLastError());
GlobalFree(*wsapi);
return FALSE;
    }
    *dwCount = dwRet;
    return TRUE;
}


//
// Function: main
//
// Description:
//    Load the Winsock library and call the protocol enumeration and
//    printing routines. Then unload and clean up.
//
int main(int argch, char **argv)
{
    WSAPROTOCOL_INFO   *wsapi=NULL;
    WSADATA             wsd;
    DWORD               dwCount;
    int i;
    if ( WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
    {
        printf("Unable to load Winsock2 DLL!\n");
        return 1;
    }
    if (EnumerateProtocols(&wsapi, &dwCount) == FALSE)
    {
        printf("Failed to enumerate protocols!\n");
        return 1;
    }
     WSACleanup();
system("pause");
    return 0;
}

1.WSADATA

大致说来,在W S A D ATA结构中,返回的唯一有用的信息是w Ve r s i o n和w H i g h Ve r s i o n。
属于最大套接字和最大U D P长度的条目应该从自己正在使用的特定协议目录条目中获取。
上一小节曾谈到这一点。

2.WSAPROTOCOL_INFO

最常用的字段是dwServiceFlags1可以查看该协议的属性.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值