DWISE1's Sockets Programming Page

DWise1's Page on Converting Sockets Programming to WinSock


Cartoon depicting Bill Gates making two different public statements at different times:
  • in 1994: "There is no need for Microsoft to support TCP/IP."

  • in 1995: "Microsoft has invented a new protocol. We're calling it TCP/IP."

I'm sure that I'm not the only programmer to find himself in this situation: all of my network programming projects are for Windows, but almost all of the information and examples out there are for Unix. Well, thanks to sockets programming and WinSock, many Unix sockets programs convert fairly easily to Windows.

On this page, I will review the basic changes that are needed to convert a Unix sockets program to a working Winsock program and I will provide links to other sites that provide more information on Winsock.


Topics


A Little History

A protocol stack consists of hardware and software for all the layers. One of the problems this caused is that there was little or no standardization from one vendor's stack to another's, which complicated software development and would tie software applications to a specific vendor's protocol stack.

Around 1990, several groups from different companies developed a single API for Windows that could run on any vendor's stack. They based it on another existing API, BSD sockets, and called it Windows Sockets, AKA "WinSock 1.1". In the process, they carried many of the concepts and functions straight over from BSD sockets (lucky for us!) and then extended them with additional functions. The Winsock extensions are prefixed with "WSA" while the BSD functions retain their original names.

Then in 1994, they came out with the Winsock 2 specification which changed the API architecture to provide even more enhanced features, including the ability to simultaneously access multiple transport protocols and protocol stacks other than TCP/IP. WinSock 2 also incorporated the Windows Open System Architecture (WOSA), which requires each vendor to be compatible with the standard WinSock DLL. Before this, each vendor's interface between the WinSock DLL and the stack was different and proprietary and so you needed a different DLL for each stack you had installed. Now you can access stacks from different vendors simultaneously using the same WinSock DLL, a definite improvement.

The current Winsock specification version, 2.2.2, was released on 07 August 1997 and can be obtained at ftp://ftp.microsoft.com/bussys/winsock/winsock2.


The Basic How-to

Changing a UNIX sockets program to Winsock requires an amazingly small number of changes. The basic steps required to convert a UNIX sockets program to WinSock for Win32 are:
  1. Link the WinSock library to your project.
    The actual procedure and name of the library name will vary between different compilers, so you will need to verify what that procedure is and the library name it uses. The two that I use are:
    • Visual C++
      Library Names: wsock32.lib, ws2_32.lib (for Winsock2)
      Procedure:
      1. On the main menu, click on Project, then on Settings.
      2. On the Project Settings dialog, click on the Link tab.
      3. In the Object/Library Modules text box, add the Winsock library to the end of the list already there.
      4. Then click the OK button to complete the action and close the dialog.

    • MinGW gcc
      Library Names: libwsock32.a, libws2_32.a (for Winsock2)
      Procedure: On the command line or in the Makefile, include the library option, -lwsock32 (or -lws2_32 for Winsock2).

      The Dev-C++ IDE from Bloodshed Software uses the MinGW compiler and so also uses the same library files.
      Procedure:

      1. On the main menu, click on Project, then on Project Options.
      2. On the Project Options dialog, click on the General tab (it should be the first one displayed).
      3. Click on the Add Library or Object button, which opens a Open common dialog box set to list library files (*.lib,*.a).
      4. Navigate to the Dev-Cpp lib directory and add the libwsock32.a file (or libws2_32.a for Winsock2).
      5. Then click the OK button to complete the action and close the dialog.

  2. Replace the UNIX-specific header includes with the single include statement
    #include <winsock.h>
    or, for Winsock2:
    #include <winsock2.h>
  3. Within the program, run the Winsock initialization with the following code
    1. Include the data declaration:
      WSADATA wsaData;

    2. Add the function call and completion test:
      if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
      {
      // insert error-handling code here
      }
    The parameter, MAKEWORD(2, 0), specifies the WinSock version number (version 2.0 in this example) and formats it correctly.

  4. Replace the UNIX close() function call with closesocket().

  5. At the end of the program, exit the WinSock DLL by calling the function, WSACleanup().

  6. Read error codes with WSAGetLastError().

A good source for information on converting UNIX sockets code to Windows is the document, Transitioning from UNIX to Windows Socket Programming, by Paul O'Steen. This PDF file also gives complete instructions with pictures for creating in Visual C++ a Win32 console application (equivalent to a DOS application) that supports WinSock.

Examples of UNIX applications that have been converted to Windows can be found at the site for the book, The Pocket Guide to TCP/IP Sockets: C Version, by Michael J. Donahoo and Kenneth L. Calvert:

Similar examples for the second edition, TCP/IP Sockets in C: Practical Guide for Programmers, can be found through that book's page.


Caveat Programmer

Since so many of the BSD socket functions were carried over directly to WinSock, most UNIX sockets programming should translate easily over to Windows. However, you should keep a few caveats in mind:


Those are just the main differences and caveats that the beginner is likely to have to deal with. You can find more detailed information on issues of compatibility between WinSock and BSD sockets and between WinSock 1.1 and WinSock 2 at:


Resources

Books

  • Network Programming for Microsoft Windows
    By Anthony Jones and Jim Ohlund, Microsoft Press, 1999, 675 pages, ISBN 0-7356-0560-2.

    This book covers a fairly wide range of topics, mainly using Winsock but not MFC. It even gets into raw sockets (for the highly experienced only). There's a lot of material for a beginner to absorb as his first exposure to the subject. This was the first book I picked up on the subject (besides a come-up-to-speed-quick book on TCP/IP -- see below) and I intend to come back to it as I progress.

    The second edition was published in 2002 and is listed here on Amazon.com (800 pages, ISBN 0-7356-1579-9). This edition reportedly concentrates more on sockets and on Microsoft's new whiz-bang networking services, relegating the first book's coverage of "legacy APIs" (eg, NetBIOS, Mailslots, and Named Pipes) to the CD (one reviewer's complaint is that this was only mentioned in the introduction). Another complaint is that the examples use XP's new name resolution functions and so will not compile unless you have downloaded the latest SDK.


  • Windows Sockets Network Programming
    By Bob Quinn, Dave Shute (Contributor), David K. Shute, Addison-Wesley Advanced Windows Series, 1995, 637 pages, ISBN 0-2016-3372-8.

    This book is well-enough received on Amazon.com, but it is criticized mainly for being dated and for concentrating more on Win16 sockets programming than on Win32. On the plus side, if you are going to be doing Win16 sockets programming, then this book may be of more use to you than the more recent offerings.

    This book provided much of the material at the SOCKETS: WinSock Development Information site. The book's web site is at http://www.sockets.com/wsnp.htm.


  • Win32 Network Programming: Windows(R) 95 and Windows NT Network Programming Using MFC
    By Ralph Davis, Addison-Wesley, 1996, 675 pages, ISBN 0-7356-0560-2.

    Reviewed on Amazon.com


  • Internetworking with TCP/IP Vol. III Client-Server Programming and Applications-Windows Sockets Version
    By Douglas E. Comer, David L. Stevens (Contributor), Prentice Hall, 1997, 512 pages, hardcover, ISBN 0-1384-8714-6.

    Reviewed on Amazon.com


  • Web Sites

  • Winsock Programmer's FAQ
    This is the FAQ for the alt.winsock.programming and comp.os.ms-windows.programmer.tools.winsock newsgroups. It serves as a repository of Winsock programming information and links.

  • The UNIX Socket FAQ
    Almost more of a newsgroup than a FAQ. Users post their questions and other users try to help them.
    Check the rules before you reveal yourself to them as a newbie.

  • WinSock Development Information
    Source code, development tools, and other resources.

  • The Windows Sockets 2 API Specification
    Available as a Microsoft Word document at the WinSock Development Information site. This document details the differences between Winsock and BSD sockets.

  • Winsock's Compatibility With BSD Sockets by Warren Young
    An article on the Winsock Programmer's FAQ site which delves more deeply into the compatibility issues.

  • Transitioning from UNIX to Windows Socket Programming by Paul O'Steen.
    A good source for information on converting UNIX sockets code to Windows. This PDF file also gives complete instructions with pictures for creating a Win32 console application (equivalent to a DOS application) that supports WinSock.

  • Examples of UNIX applications that have been converted to Windows
    Examples of UNIX applications that have been converted to Windows can be found at the site for the book, The Pocket Guide to TCP/IP Sockets: C Version, by Michael J. Donahoo and Kenneth L. Calvert:

  • The Winsock Tutorial
    This appears to be a fairly good introductory tutorial for writing WinSock applications. Its example programs include both TCP and UDP client and server programs and a DNS name resolver.

  • Return to Top of Page
    Return to DWise1's Sockets Programming Page
    Return to DWise1's Programming Page
    Return to DWise1's Home Page


    Share and enjoy!

    First uploaded on 2003 July 26.
    Updated on 2004 February 01.

    E-Mail Address: dwise1@aol.com.