Microsoft Asia Limited
Developer Support Division
Windows development technology group
Overview of WinSock Applications
Windows XP supports WinSock 1.1 and later. In order to maintain compatibility, API (Application Programming Interface) provided by WinSock 1.1 has functions with the same name as those used by Socket communication in BSD (Berkeley Software Distribution) UNIX. WinSock 2.0 and later provide Windows native API suited for Windows Platform (Windows native WinSock 2.0 also provides extended functions for socket communication, but we will not discuss these extended functions in this article). For developing applications that supports IPv6, we will be using WinSock version 2.0 or later.
WinSock applications communicate using server-client model. WinSock offers two types of transport service, connection-oriented and connectionless. In general, connection-oriented communication is associated with TCP (Transmission Control Protocol) and connectionless communication is associated with UDP (User Datagram Protocol). The rest of the article will explain how to upgrade connection-oriented application from IPv4 to IPv6.
Environment required for the development of WinSock application
In order to use the sample codes mentioned here and develop WinSock application, you will need the following tools.
Compiler and linker
This needs to be a compiler and a linker that supports X86 architecture.
Example: Visual C++, Visual Studio 6.0, and Visual Studio.NET
Platform SDK
Header files and libraries necessary for making WinSock applications are available as Platform SDK. Platform SDK can be installed using CD-ROM/DVD-ROM distributed by MSDN (Microsoft Developer Network), or you can install the latest version via network from the following URL.
Windows SDK (Core SDK)
http://www.microsoft.com/msdownload/
platformsdk/sdkupdate/
Configuration of Connection-Oriented IPv4 application
In a typical WinSock application using server-client model, WinSock functions will be called in a following sequence (See Figure 1).

Figure 1 (in the case of IPv4)
Processing at the server side
- Load WinSock related DLLs onto the process in order to use WinSock.
API:FWSAStartup()
- Create a socket for the communication
API:Fsocket()
- Bind the socket to the IP address of the server
AP:Fbind()
- Listen for incoming connections from clients
API:Flisten()
- Accept the connection from the client
API:Faccept()
- Send and receive data to/from the client that has established the connection
API:Fsend() / recv()
- Shutdown the communication with the client
API:Fshutdown()
- Close the socket that was used for the communication with the client
API:Fclosesocket()
- Unload WinSock related DLLs from the application
API:FWSACleanup()
- Load WinSock related DLLs onto the process in order to use WinSock
API:FWSAStartup()
- Retrieve the name and address of the server you want to connect to.
API:Fgethostbyname() , or gethostbyaddr()
- Create a socket for the communication
API:Fsocket()
- Issue connection request to the server
API:Fconnect()
- Send and receive data to/from the server that established the connection
API:Fsend() / recv() - Shutdown the communication with the server
API:Fshutdown()
- Close the socket that was used for the communication with the client (server?)
API:Fclosesocket()
- Unload WinSock related DLLs from the application
API:FWSACleanup()
- Sample server code (IPv4)
http://msdn.microsoft.com/library/default.asp?url=
/library/en-us/winsock/winsock
/ipv4_only_server_code_2.asp
- Sample client code (IPv4)
http://msdn.microsoft.com/library/default.asp?url=
/library/en-us/winsock/winsock
/ipv4_only_client_code_2.asp
|
Figure 2 Example of the output of checkv4.exe
|
|
The flow of the IPv6 application created by following the instructions produced by checkv4.exe will be as follows. (Please see Figure 3) Processing on the server side (IPv6)
![]() Figure 3(In the case of IPv6) I hope the above explanations helped you grasp the overall flow of modifying the IPv4 application into IPv6 application. The readers who are actually planning to write applications are probably thinking of writing ones that can function in both IPv4 and IPv6 environment. So I would like to introduce you a simple sample code that will tell you which protocol stack is installed in the OS. The sample shown in Figure 7 first tries to obtain information regarding the protocol supported by each network interface using IPv6 options. If it fails to obtain protocol information using IPv6, it switches to IPv4 and try to obtain the information. By checking the protocol family of the network interface, it is possible to find out whether IPv6 protocol stack is installed in the Windows XP the application is running on. Putting this type of process inside the application will allow that application to support both protocol stacks smoothly. Newly registered functions New functions and data structures were added to support IPv6, and these are used in the sample code shown above. (Please see Figure 4 and 5) Addrinfo structure obtained using getaddrinfo function must be freed using freeaddrinfo function. Because of this, repeated use of getaddrinfo function in the loop or in multiple threads should be avoided. Every time you call getaddrinfo function, memory for addrinfo structure will be allocated. After the call you must use freeaddrinfo function to free the memory that was allocated. For the detailed explanation of these functions please refer to MSDN, or the following URLs. Windows Sockets Functions http://msdn.microsoft.com/library/default.asp?url= /library/en-us/winsock/winsock /windows_sockets_functions_2.asp Internet Protocol Helper http://msdn.microsoft.com/library/default.asp?url= /library/en-us/iphlp/iphlp /ip_helper_start_page.asp Summary As you can see from the IPv6 version of the sample code, you can easily modify IPv4 application to support IPv6 just by changing the name resolution (address resolution) procedures. Furthermore, functions and data structures newly added to support IPv6 can also support IPv4, and it is possible to support both addressing modes by using these functions and data structures. In this article we intentionally avoided the detailed explanations of the implementation of IPv6 at the TCP/IP stack of Windows XP. The reason for this omission is that actual processing of IPv6 protocol is all performed by TCP/IP driver in the lower layer, and WinSock applications at the higher layer do not need to worry about the details regarding TCP/IP stack. For detailed information on the implementation of IPv6 in Windows XP, please refer to the following URL. |
|
Figure 4 Newly added functions
Figure 5 Newly added structure
|
|
Coexistence of IPv4 and IPv6, and migration from IPv4 to IPv6. http://www.microsoft.com/japan/windowsxp /pro/techinfo/administration/ipv6/ipv6ipv4.asp How IPv6 is configured in Windows XP and IPv6 test laboratory http://www.microsoft.com/japan/windowsxp /pro/techinfo/administration/ipv6/ipv6configs.asp Internet protocol version 6: Request for Comments and Internet draft http://www.microsoft.com/japan/windowsxp /pro/techinfo/administration/ipv6/ipv6rfc.asp FAQ regarding Windows XP IPv6 protocol http://www.microsoft.com/japan/windowsxp /pro/techinfo/administration/ipv6/default.asp Networking and Wireless Technologies http://www.microsoft.com/hwdev/tech/network/default.asp Finally, the following site is not a Microsoft Web site, but contains various information regarding WinSock application development. Please use it as a reference when developing the application. Reference site WinSock Development Information http://www.sockets.com |
|
Figure 6 Sample server code that supports IPv6 Error processing
Sample code for the server Main processing
Figure 7 Protocol stack assessment sample Main function processing
Processing for assessment of the protocol
Copied with permission from IPv6 magazine No.4 (published by Impress) |
この記事のトラックバックURL
http://www.ipv6style.jp/trackback/603



