Where does one get the "sys/socket.h" header/source file?
Asked Answered
I

4

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!

Intransigeance answered 9/1, 2011 at 10:1 Comment(7)
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.Viradis
@Sergey, he may have installed MinGW directly, without installing it through Cygwin.Hitlerism
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.Aguish
@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.Viradis
@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.Viradis
@SergeyTachenov, Actually isn't windows cygwin just buggy for everything? The stability can never be compared to on unix isn't it?Altdorfer
@jalf, nearly identical API? seriously?Altdorfer
F
59

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.

Fredenburg answered 9/1, 2011 at 22:10 Comment(2)
Don't you need the windows version of closing socket too? And a couple more other things?Altdorfer
Yes, under windows you need to call closesocket() instead of close(). And probably other things also.Fredenburg
H
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.

Hitlerism answered 9/1, 2011 at 21:26 Comment(4)
The subsystem you're referring to is called Interix +1Detour
@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.Coble
@Coble notice anything about the date of that comment? :)Detour
@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 ;)Coble
C
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;

Corncob answered 3/11, 2017 at 20:24 Comment(0)
C
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.

Corncob answered 5/11, 2017 at 22:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.