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.
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.
Changing UNIX sockets code to Winsock requires an amazingly small number of changes. That is, of course, assuming that the rest of the program doesn't use features that are peculiar to UNIX (see the "Caveat" section below).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++ 6.0 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.
- The "Before Picture" at http://cs.baylor.edu/~donahoo/PocketSocket/textcode.html (UNIX source)
- The "After Picture" at http://cs.baylor.edu/~donahoo/PocketSocket/winsock.html (WinSock version of the UNIX source)
The following is a basic overview of Winsock programming, concentrating mainly on the basic steps required to convert a UNIX sockets program to WinSock for Win32. The individual topics covered are:
- Linking the Winsock Library to Your Project
- Absolutely Necessary Changes
- Winsock Error Codes
- Optional but Advisable Changes
- The WSA* Extended Functions
Linking 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++ v6
Library Names: wsock32.lib, ws2_32.lib (for Winsock2)
Procedure:
- On the main menu, click on Project, then on Settings.
- On the Project Settings dialog, click on the Link tab.
- In the Object/Library Modules text box, add the Winsock library to the end of the list already there.
- 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:
- On the main menu, click on Project, then on Project Options.
- On the Project Options dialog, click on the General tab (it should be the first one displayed).
- Click on the Add Library or Object button, which opens a Open common dialog box set to list library files (*.lib,*.a).
- Navigate to the Dev-Cpp lib directory and add the libwsock32.a file (or libws2_32.a for Winsock2).
- Then click the OK button to complete the action and close the dialog.
Absolutely Necessary Changes
- Replace the UNIX-specific header includes with the single include statement
#include <winsock.h>or, for Winsock2:#include <winsock2.h>- Within the program, run the Winsock initialization with the following code
The parameter,
- Include the data declaration:
WSADATA wsaData;
- Add the function call and completion test:
if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
{
// insert error-handling code here
}MAKEWORD(2, 0)
, specifies the WinSock version number (version 2.0 in this example) and formats it correctly.
- Replace the UNIX
close()
function call withclosesocket()
.
- At the end of the program, exit the WinSock DLL by calling the function,
WSACleanup()
.
- Read error codes with
WSAGetLastError()
.
- Winsock Error Codes
WSAGetLastError()
only returns a numeric value for the error code instead of any explanatory text. The authoritative reference for what those numbers mean is in the Microsoft Developers' Network Library: Windows Sockets Error Codes.
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:
- Not all BSD socket functions were carried over, e.g.,
readv()
.
- There are some non-sockets features of UNIX programming that are not supported by Windows; e.g., signals, process forking, UNIX domain sockets.
- Applications that rely on forking processes will not translate over to Windows and will need to be redesigned; e.g., using threading or
select()
.
- Win32 does support multi-threading, but the syntax is different, so you will need to convert the POSIX threads (AKA "pthreads") syntax to Win32 multi-threading.
- WinSock's handling of the
select()
function is slightly different, in that UNIX handles nearly everything as a file descriptor, including sockets, and Windows does not.Specifically, with WinSock you can only use
select()
with sockets, but not with files (such as stdin and stdout). Therefore, when a UNIX program uses theselect()
function to read the sockets and the keyboard input (stdin), for Windows you will need to provide a separate test for stdin.
- The standard UNIX approach of treating a socket's file descriptor as an integer won't work in WinSock. WinSock defines a special integer type called SOCKET and a special value of INVALID_SOCKET. A SOCKET can have a value of zero, whereas in UNIX that value is reserved for the standard input file, stdin. Also, SOCKET is unsigned, so the
socket()
function cannot return -1 for error as in UNIX. Instead, it returns INVALID_SOCKET which must be tested for explicitly; e.g:SOCKET sock;
sock = socket(...);
if (sock == INVALID_SOCKET)
{
// error processing
}
- WinSock's handling of the
shutdown()
function is not as graceful as in UNIX and can cause unexpected errors.
- WinSock 1.1 under Win16 works differently and has its own set of caveats for you to worry about, including making sure to use far pointers and not having WinSock automatically provided with Windows. Since most WinSock applications that we will write will be for Win32, this should not be much of a problem.
The good news is that Win32 supports existing Win16 TCP/IP applications.
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:
- The Windows Sockets 2 API specification is available as a Microsoft Word document at the WinSock Development Information site.
- The article, Winsock's Compatibility With BSD Sockets by Warren Young, on the Winsock Programmer's FAQ site.
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
Microsoft Developers Network (MSDN) Library This is the "horse's mouth" with articles and reference documentation their supported software development tools. It's a must-have link for all programmers developing software for a Windows environment. A few pertinent links for Winsock are:
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:
- Before: http://cs.baylor.edu/~donahoo/PocketSocket/textcode.html (UNIX source)
- After: http://cs.baylor.edu/~donahoo/PocketSocket/winsock.html (WinSock version)
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
Share and enjoy!
First uploaded on 2003 July 26.
Updated on 2011 July 18.