Microsoft Knowledge Base Email Alertz

(192120) - The Winsock 2 API, WSASocket, allows you to create a socket based on the contents of a passed in WSAPROTOCOL_INFO structure. This readily allows you to create sockets from different installed Winsock 2 Service Providers. You obtain A WSAPROTOCOL_INFO...

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 192120 - Last Review: July 11, 2005 - Revision: 1.3

How To Use the WSAPROTOCOL_INFO Structure to Create a Socket

This article was previously published under Q192120

On This Page

SUMMARY

The Winsock 2 API, WSASocket, allows you to create a socket based on the contents of a passed in WSAPROTOCOL_INFO structure. This readily allows you to create sockets from different installed Winsock 2 Service Providers. You obtain A WSAPROTOCOL_INFO structure by calling the WSAEnumProtocols API and choosing the service provider that you want from the list of WSAPROTOCOL_INFO structures that is returned.

MORE INFORMATION

To choose the correct service provider, examine the capabilities supported by each service provider and choose the one that matches your needs. For example, the following code snippet demonstrates how to obtain a service provider that supports TCP/IP and Quality of Service. It is worth noting that the names of Microsoft service providers differ between the various Windows platforms. Therefore, this article should not form the basis of choosing the appropriate service provider.

Sample Code

   if (WSAStartup(MAKEWORD(2,2), &WSAData))
      printf("WSAStartup %d", WSAGetLastError());
   else
      {
      // First, have WSAEnumProtocols tell you how big a buffer you need.
      nRet = WSAEnumProtocols(NULL, lpProtocolBuf, &dwBufLen);
      if (SOCKET_ERROR != nRet)
         printf("WSAEnumProtocols: should not have succeeded\n");
      else if (WSAENOBUFS != (dwErr = WSAGetLastError()))
         // WSAEnumProtocols failed for some reason not relating to buffer
         // size - also odd.
         printf("WSAEnumProtocols(1): %d\n", WSAGetLastError());
      else
         {
         // WSAEnumProtocols failed for the "expected" reason. Therefore,
         // you need to allocate a buffer of the appropriate size.
         lpProtocolBuf = (WSAPROTOCOL_INFO *)malloc(dwBufLen);
         if (lpProtocolBuf)
            {
            // Now you can call WSAEnumProtocols again with the expectation
            // that it will succeed because you have allocated a big enough
            // buffer.
            nRet = WSAEnumProtocols(NULL, lpProtocolBuf, &dwBufLen);
            if (SOCKET_ERROR == nRet)
               printf("WSAEnumProtocols(3): %d\n", WSAGetLastError());
            else
               {
               // Loop through protocols, looking for the first service
               // provider that meets the matching criteria.
               bProtocolFound = FALSE;
               for (i=0; i<nRet; i++)
                  {
                  if ((IPPROTO_TCP == lpProtocolBuf[i].iProtocol) &&
                      (XP1_QOS_SUPPORTED == (XP1_QOS_SUPPORTED &
                       lpProtocolBuf[i].dwServiceFlags1)))
                     {
                     bProtocolFound = TRUE;
                     break;
                     }
                  }
               }
            }
         }
      }
				
Once you have chosen a service provider, the following sample code demonstrates how to actually create a socket using a supplied WSAPROTOCOL_INFO structure from the service provider you have chosen:
    sd = WSASocket(
        FROM_PROTOCOL_INFO,
        FROM_PROTOCOL_INFO,
        FROM_PROTOCOL_INFO,
        &lpProtocolBuf[i],
        0,
        WSA_FLAG_OVERLAPPED);
    if (INVALID_SOCKET == sd)
        printf("WSASocket %d", WSAGetLastError());
				
Please note that specifying WSA_FLAG_OVERLAPPED is very useful and is recommended. For additional information on specifying WSA_FLAG_OVERLAPPED, please see the following article in the Microsoft Knowledge Base:
179942  (http://kbalertz.com/Feedback.aspx?kbNumber=179942/EN-US/ ) INFO: WSA_FLAG_OVERLAPPED Is Needed for Non-Blocking Sockets
181610  (http://kbalertz.com/Feedback.aspx?kbNumber=181610/EN-US/ ) INFO: WSA_FLAG_OVERLAPPED Needed for Timeout on WSASocket
Also, do not forget to free the lpProtocolInfo buffer when you are finished using it.

APPLIES TO
  • Microsoft Platform Software Development Kit-January 2000 Edition, when used with:
    • Microsoft Windows 95
    • Microsoft Windows 98 Standard Edition
    • Microsoft Windows NT 4.0
Keywords: 
kbhowto kbgqos kbwinsock kbnetwork KB192120
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate