Sockets in MinGW

后端 未结 7 1254
猫巷女王i
猫巷女王i 2020-12-30 23:26

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 u

相关标签:
7条回答
  • 2020-12-30 23:57

    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.

    0 讨论(0)
  • 2020-12-30 23:58

    Is there a library/package I can add to MSYS to make everything compile without errors?

    MSYS is a fork of Cygwin. It provides a BSD sockets API just like Cygwin. If you have MSYS, you should just be able to compile your code like this:

    gcc -c -o test.o test.c -I/include
    

    The -I/include is needed to force the compiler to look in the MSYS include directory.

    And link like this:

    gcc -o test.exe test.o /bin/msys-1.0.dll
    
    0 讨论(0)
  • 2020-12-31 00:01

    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.

    0 讨论(0)
  • 2020-12-31 00:03

    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.

    0 讨论(0)
  • 2020-12-31 00:11

    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)

    0 讨论(0)
  • 2020-12-31 00:13

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

    The answer/solution is more explicit.

    0 讨论(0)
提交回复
热议问题