A short summary about porting TCP/IP software to Win32 ------------------------------------------------------ version 1.3-oct-29-2007 (c) Jun-2007 Steffen Wendzel (steffen (at) ploetner-it (dot) de) (see http://www.wendzel.de for updates) This text is published under the GNU Free Documentation License (see http://www.gnu.org/copyleft/fdl.html for details). Changelog --------- 1.3-oct-29-2007: * no poll() * recv() flags * ioctlsocket() 1.2-oct-24-2007: * winsocket init code * add closesocket() description * errno -> WSAGetLastError() 1.1-sep-11-2007: * add a hint about loading functions from dll files 1.0-jul-10-2007: * first version released Introduction ------------ I wrote this short summary while porting my NNTP server WendzelNNTPd to Windows. Like you will see, it realy sux to port code from Linux/ BSD/Unix-like systems to 32-Bit Windows. 0. What you NEED to know is that ... ------------------------------------ ... you can check the OS on compile time. If you want to make sure that some code only gets compiled under windows you can use #ifdef: #ifdef __WIN32__ /* place windows code here */ #endif 1. Signals ---------- There are no Signals in Windows. 2. syslogd() ------------ Forget syslog(). It is nice under Linux but windoze does not have it. Write your own logging functions instead :-) 3. Free compiler and build toolchain ------------------------------------ I used Dev-C++, pthread Win32 build and different additional Dev-C++ packages (like Gtk+ and sqlite3 libs and headers) plus additional "GnuWin32" compiled packages like flex and bison to port my software to this damn OS. Dev-C++ also comes with gdb and make. Even autotools are available on that way. 4. CVS and SVN -------------- Seems to work but made some problems on my Win2k system. There are different Win32 builds of CVS and Subversion software... be careful, it can hurt if something wents wrong! 5. Child Processes ------------------ There isn't really a fork() under Win32. You can use CreateProcess() and something else but... If you, for example, wrote a network daemon and you want to fork a child process to use a connection with a socket file descriptor from the parent process ... forget it :-( Windows is not able to do that. I rewrote the whole fork() stuff and now use pthreads. 6. Sockets ---------- 6.1 Header files Use and initialize winsock. Then the most things should run... But don't think that you can work pretty well with SOCK_RAW. (We are still under Windoze ...) 6.2 Init Winsock Before you can use sockets under windoze, you have to initialize the winsocket system. Here is the init code: The Microsoft MSDN says for WSAStartup(): "(...) initiates use of the Winsock DLL by a process." WORD wVersionRequested; WSADATA wsaData; wVersionRequested = MAKEWORD(1, 1); if (WSAStartup(wVersionRequested, &wsaData) != 0) { DoSomeErrorHandling("Winsock Init Error"); ... } You maybe want to know, what WSAStartup() exactly does ;-) The Micro- soft MSDN says it "initiates use of the Winsock DLL by a process". And: "The WSAStartup function must be the first Windows Sockets function called by an application or DLL. It allows an application or DLL to specify the version of Windows Sockets required and retrieve details of the specific Windows Sockets implementation. The application or DLL can only issue further Windows Sockets functions after successfully calling WSAStartup." Read [2] for additional details. 6.3 Closing Sockets Closing sockets works with closesocket() what can be used like close() with the socket file descriptor. 6.4 Catching Errors For using errno, Microsoft recommends to define a Macro for the WSAGetLastError() function. Take a look at [1] for details. 6.5 recv() flags Not all of the recv() flags available under *nix systems are supported by Win32. At least MSG_DONTWAIT is missed. I didn't check *all* other flags. It is possible, that another flag isn't supported too. A simple workaround for MSG_DONTWAIT is to use select(). 6.6 poll() It seems that there is no poll() available. Use select() instead. 6.7 ioctlsocket Want to know how many bytes are available for recv()? Want to use the well known ioctl() syscall you used for years? This will not work but you can use ioctlsocket() on the socket file descriptor on a similar way. 7. IPv6 ------- There are patches ... but aehmm... hmm.... Georgy sent me this interesting link: http://www.ipv6.org/impl/windows.html 8. inet_pton()/inet_ntop() -------------------------- Both functions are not available. You have to use inet_ntoa(). If you don't know why this is a bead idea: read Richard Stevens' "Unix Network Programming" (AFAIR somewhere within the first chapters). inet_aton() does also not exist. You have to use inet_addr() instead (but don't forget to check for the return value for 0xffffffff!). 9. struct sockaddr_storage -------------------------- Not found. Use struct sockaddr_in for IPv4 instead. 10. bzero() ----------- There is no bzero(). But this does not hurt too much. I wrote a simple workaround: #ifdef __WIN32__ #define bzero(a, b) memset(a, 0x0, b) #endif 11. select() I/O multiplexing ----------------------------- Seems to work fine ... 12. Using functions from DLLs and loading DLLs at runtime --------------------------------------------------------- You can load a DLL file at runtime by using LoadLibrary(). HINSTANCE hinstLib = LoadLibrary("whatever.dll"); To use a function from a loaded DLL: Use the function GetProcAddress(). f = (cast for FunctionName) GetProcAddress(hinstLib, "FunctionName"); 13. Some other notes ------------------- * Forget shell scripts: You need to use windoze batch script stuff you really can put into your favorite trashcan. * There exists an Rx package for Dev-C++ that includes regex functions. * Also available: SQLite3, Gtk+ and many many other dev packages * Qt from trolltech also compiles very well under win32 14. Links --------- http://devpaks.org http://sf.net/projects/devpaks 15. Sources ----------- [1] http://msdn2.microsoft.com/en-us/library/ms737828.aspx [2] http://msdn2.microsoft.com/en-us/library/ms742213.aspx X. Thanks --------- to Georgy Berdyshev for his helpful devpaks+IPv6 hint.