40

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!

AnonymousJohn
  • 1,587
  • 3
  • 12
  • 15
  • 1
    Cygwin has most of the the POSIX headers, including sys/socket.h. It is strange that you didn't find it. My Cygwin installation certainly has it. – Sergei Tachenov Jan 09 '11 at 12:06
  • 1
    @Sergey, he may have installed MinGW directly, without installing it through Cygwin. – Michael Aaron Safyan Jan 09 '11 at 21:30
  • Do you need it? Windows defines a nearly identical API you can use instead. And if you need cross platform portability, libraries such as Boost.Asio or ACE might be better options. – jalf Jan 09 '11 at 23:23
  • @Michael, he says "somewhere said to install Cygwin instead, as it comes with lots of libraries. Okay, so I installed it." I can't imagine how it is possible to install Cygwin with no sys/socket.h. Perhaps some devel package is missing? Cygwin is a really good thing for compiling native Unix applications for Windows, unless they use fork(), which is buggy and will always be. – Sergei Tachenov Jan 10 '11 at 05:31
  • @jalf, good point. Or maybe Qt. It has good sockets support too. And these high-level libraries are easier to use than Berkley socket API anyway. – Sergei Tachenov Jan 10 '11 at 05:33
  • @SergeyTachenov, Actually isn't windows cygwin just buggy for everything? The stability can never be compared to on unix isn't it? – Pacerier Mar 04 '17 at 01:15
  • @jalf, nearly identical API? seriously? – Pacerier Mar 04 '17 at 01:16

4 Answers4

58

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.

Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
14

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.

Community
  • 1
  • 1
Michael Aaron Safyan
  • 93,612
  • 16
  • 138
  • 200
  • 2
    The subsystem you're referring to is called [Interix](http://en.wikipedia.org/wiki/Interix) +1 – Billy ONeal Jan 09 '11 at 22:02
  • 2
    @BillyONeal, The wiki page suggests that, *Microsoft announced in 2011 that Interix will not be included in Windows versions after Windows 8 and customers should start migrating their applications to an alternative solution.* – iammilind Jan 14 '15 at 11:30
  • @iammilind notice anything about the date of that comment? :) – Billy ONeal Jan 14 '15 at 14:33
  • 1
    @BillyONeal, Yes I saw that the question was asked 4 years back. However thought that, seeing my comment you may come up with other alternative ;) – iammilind Jan 14 '15 at 14:42
3

I would like just to add that if you want to use windows socket library you have to :

  • at the beginning : call WSAStartup()

  • at the end : call WSACleanup()

Regards;

Adamos19
  • 49
  • 5
0

Try to reinstall cygwin with selected package:gcc-g++ : gnu compiler collection c++ (from devel category), openssh server and client program (net), make: the gnu version (devel), ncurses terminal (utils), enhanced vim editors (editors), an ANSI common lisp implementation (math) and libncurses-devel (lib).

This library files should be under cygwin\usr\include

Regards.

Adamos19
  • 49
  • 5