30

I was just trying to build netcat in MSYS using MinGW and realized that MinGW never really ported all of the BSD socket stuff to Windows (eg sys/socket.h). I know you can use Windows Sockets in MinGW, but why did they never make a Windows port of the BSD sockets? I noticed quite a few programs using #ifdef's to workaround the issue. Is there a Windows port of the BSD sockets somewhere that can be used instead?

Here are the errors when doing a make for netcat in MSYS:


gcc -DLOCALEDIR=\"\/usr/local/share/locale\" -DHAVE_CONFIG_H -I. -I. -I.. -g -O2 -Wall -c `test -f 'core.c' || echo './'`core.c
In file included from core.c:29:
netcat.h:38:24: sys/socket.h: No such file or directory
netcat.h:39:63: sys/uio.h: No such file or directory
netcat.h:41:24: netinet/in.h: No such file or directory
netcat.h:42:55: arpa/inet.h: No such file or directory

There are no #ifdef's for MinGW. Is there a library/package I can add to MSYS to make everything compile without errors?

Note: You can download netcat here and browse the CVS repo here

User1
  • 39,458
  • 69
  • 187
  • 265
  • Winsock itself was originally a port of the Berkeley sockets API. – ChrisW Oct 05 '09 at 00:29
  • @ChrisW, I've never understood what's the point of making Winsock different from UNIX? – Pacerier Mar 04 '17 at 00:39
  • @Pacerier I think that Winsock is simpler in that it has fewer header files to include. See e.g. [Transitioning from UNIX to Windows Socket Programming](http://cs.baylor.edu/~donahoo/practical/CSockets/WindowsSockets.pdf) for further details. – ChrisW Mar 04 '17 at 00:46

6 Answers6

27

BSD sys/socket.h is a POSIX header and the win32 API doesn't support it. MinGW headers are just a reimplementation of native win32 headers and don't offer additional POSIX compatibility.

If you are looking for sys/socket.h support, try either GNU gnulib's sys/socket.h replacement or go with Cygwin, which provides a POSIX compatibility wrapper on Windows.

kalev
  • 1,925
  • 1
  • 15
  • 12
  • about gnulib for mingw : "some modules are currently unsupported on mingw: mgetgroups, getugroups, idcache, userspec, openpty, login_tty, forkpty, pt_chown, grantpt, pty, savewd, mkancesdirs, mkdir-p, euidaccess, faccessat.", "mingw in 64-bit mode is not tested and low priority so far" http://www.gnu.org/software/gnulib/manual/gnulib.html#Target-Platforms – Jérôme Radix Dec 04 '12 at 08:43
  • @kalev, I never understood this. What's the point of gnulib when there's already cygwin? – Pacerier Mar 04 '17 at 00:41
  • @Pacerier Cygwin is a Unix emulator for Windows whereas gnulib is a library which lets you port most of the traditional Unix packages to virtually any OS. – Terry Jan 28 '19 at 10:51
11

WinSock and WinSock2 have different function names from the BSD Sockets. If I wish to write cross-platform applications, then I have code a lot of work-arounds just to keep Microsoft happy.

It would be so much easier if there were special "socket.h" and "socket.c" files included with MinGW that simply translated stuff by calling the respective WinSock2 counter-parts.

I'm just starting to learn C programming, so I'm unable to do this myself, but I'm surprised that nobody seems to have even attempted this so far.

5

These comments from another answer served as the answer I needed to get a piece of simple bsd socket code to compile with mingw on windows.

Replace all of those includes with #include as that would be the equivalent header for winsock, then see what happens.

You will also need to link against ws2_32 and use WSAStartup/WSACleanup. Which might get you up and running.

EDIT: I also ended up having to replace close with shutdown / closesocket and write with send. The code compiled fine but didn't actually work without those additional changes.

davenpcj
  • 12,508
  • 5
  • 40
  • 37
3

As ChrisW said, Winsock2 is a port of BSD sockets. Which part of winsock are you trying to use which differs from BSD sockets ? (other than the WSAStartup and WSACleanup)

Andrew Keith
  • 7,515
  • 1
  • 25
  • 41
  • 2
    I'm just trying to get netcat to compile. I'll update the question with the current errors. – User1 Oct 05 '09 at 01:34
  • 1
    Replace all of those includes with `#include ` as that would be the equivalent header for winsock, then see what happens. – GRB Oct 05 '09 at 03:12
  • 3
    if you do that, you will also need to link against ws2_32 and use WSAStartup/WSACleanup. Which *might* get you up and running. – tmpvar Jan 08 '10 at 21:08
3

MingWin is minimalist, and that is the most important aspect of it. Because it makes it easier to understand, at the end it is the developer's responsibility to write the application. MingWin only makes things easier but does no magic in turing nix apps to windows.

user237890
  • 31
  • 2
  • 1
    So we're supposed to write two copies of code for every single functionality? When does this make any sense? – Pacerier Mar 04 '17 at 00:42
3

See the stackoverflow link : Where does one get the "sys/socket.h" header/source file?

The answer/solution is more explicit.

Community
  • 1
  • 1
jlguenego
  • 1,192
  • 15
  • 23