I've been trying to write a server in C++ Unix style, but I'm stuck on a Windows machine. I started with MinGW, but it didn't compile right and told me that it couldn't find the "sys/socket.h" file. Which, of course, is necessary for the server to even work. I went searching for it, and I think somewhere said to install Cygwin instead, as it comes with lots of libraries. Okay, so I installed it. Every single library it could possibly offer me. Then I went to compile again, and it STILL can't find it. I went searching through the ENTIRE includes folder, and couldn't find the file. So I was a little miffed (3 hours down the drain for extra functionality I don't need), but I continued to search. I can't find it ANYWHERE. I find multiple references to using it, but I can't find ANYWHERE to download it. I've been searching for the past few hours now, and I've become very frustrated with everything because there are NO references to where I can get it (and I will NOT use winsock. That breaks compatibility if I remember right).
So, long story short, where can I download the 'socket.h'/'socket.c'/'socket.cpp' files? It would make my life (and I'm sure many others' lives) SO much easier, and I would really appreciate it!
On Debian and it's derivates (Ubuntu, ...) are basic header files (including sys/socket. h ) in package libc6-dev . The place under /usr/include is arch dependent.
DESCRIPTION. The <sys/socket. h> header shall define the type socklen_t, which is an integer type of width of at least 32 bits; see APPLICATION USAGE. The <sys/socket.
Given that Windows has no sys/socket.h, you might consider just doing something like this:
#ifdef __WIN32__ # include <winsock2.h> #else # include <sys/socket.h> #endif
I know you indicated that you won't use WinSock, but since WinSock is how TCP networking is done under Windows, I don't see that you have any alternative. Even if you use a cross-platform networking library, that library will be calling WinSock internally. Most of the standard BSD sockets API calls are implemented in WinSock, so with a bit of futzing around, you can make the same sockets-based program compile under both Windows and other OS's. Just don't forget to do a
#ifdef __WIN32__ WORD versionWanted = MAKEWORD(1, 1); WSADATA wsaData; WSAStartup(versionWanted, &wsaData); #endif
at the top of main()... otherwise all of your socket calls will fail under Windows, because the WSA subsystem wasn't initialized for your process.
Since you've labeled the question C++, you might be interested in using boost::asio, ACE, or some other cross-platform socket library for C++ or for C. Some other cross-platform libraries may be found in the answers to C++ sockets library for cross-platform and Cross platform Networking API.
Assuming that using a third party cross-platform sockets library is not an option for you...
The header <sys/socket.h> is defined in IEEE Std. 1003.1 (POSIX), but sadly Windows is non-compliant with the POSIX standard. The MinGW compiler is a port of GCC for compiling Windows applications, and therefore does not include these POSIX system headers. If you install GCC using Cygwin, then it will include these system headers to emulate a POSIX environment on Windows. Be aware, however, that if you use Cygwin for sockets that a.) you will need to put the cygwin DLL in a place where your application can read it and b.) Cygwin headers and Windows headers don't interact very well (so if you plan on including windows.h
, then you probably don't want to be including sys/socket.h
).
My personal recommendation would be to download a copy of VirtualBox, download a copy of Ubuntu, install Ubuntu into VirtualBox, and then do your coding and testing on Ubuntu. Alternatively, I am told that Microsoft sells a "UNIX subsystem" and that it is pre-installed on certain higher-end editions of Windows, although I have no idea how compliant this system is (and, if it is compliant, with which edition of the UNIX standard it is compliant). Winsockets are also an option, although they can behave in subtly different ways than their POSIX counterparts, even though the signatures may be similar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With