6

I made a server with C using socket on a Linux machine and it's working fine but when I tried to run it on windows machine using visual studio, I'm getting an error:

fatal error C1083: Cannot open include file: 'sys/socket.h': No such file or directory

The ide telling me that this header files are not found.

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
Mahmoud
  • 65
  • 1
  • 1
  • 8
  • 5
    These are Unix headers which are not available on Windows. – Yksisarvinen May 27 '21 at 16:20
  • 4
    Yes, there is no `sys/socket.h` on Windows. You can start reading Windows documentation here: https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-start-page-2 – SergeyA May 27 '21 at 16:21
  • 1
    `` is a POSIX header. https://stackoverflow.com/questions/2952733/using-sys-socket-h-functions-on-windows – annoyingnoob May 27 '21 at 16:21
  • somewhat , unfortunately network programming is one of the areas between windows and linux that is quite different. If you need portable cross-platform networking code, of course you're welcome to roll it yourself, but it's already been done. I've used (and been satisfied with) [Poco sockets](https://pocoproject.org/) in the past. It requires c++, but will build and run on linux and windows from a single implementation. – yano May 27 '21 at 16:27
  • You can use [WSL](https://learn.microsoft.com/en-us/windows/wsl/install-win10) on Windows. – Eljay May 27 '21 at 16:31
  • 1
    @yano I'd argue that sometimes they are not. Windows has good support for BSD sockets, and a lot of code could be lifted and shifted between two systems (especially if you use `select`). It is only when you get deep into the woods (i.e. attempt to port edge-triggered `epoll`-based systems to Windows), but than, chances are, the interop library you have used would not have support for this in the first place. – SergeyA May 27 '21 at 16:36
  • @SergeyA I was thinking more along the lines of setup and error reporting. Beyond that I agree. As OP has discovered, entirely different set of header files, for example. – yano May 27 '21 at 16:40
  • Use Boost.Asio. in the future it will be replaced by ISO Standard Networking TS, which will be an easy migration. – rustyx May 27 '21 at 17:35

2 Answers2

11

For Windows, you have to use winsock.h or winsock2.h and sys/types.h. Forget about unistd.h, arpa/inet.h and netinet.h. Use a conditional compilation to include the correct header according to the platform.

Also, to use socket under Windows, you application must first call WSAStartup.

Most of the call are the same between Windows and Linux. But most performance will require to avoid select() (It works) and use Windows functions. See the documentation.

fpiette
  • 11,983
  • 1
  • 24
  • 46
  • "*For Windows, you have to use `winsock.h`*" - or `winsock2.h`, which replaces `winsock.h` – Remy Lebeau May 27 '21 at 17:56
  • @RemyLebeau, did you mean "For Windows, you have to use `winsock.h`" - or `winsock2.h`, which replaces **`sys/socket.h`**"? – Ricardo Mar 18 '22 at 10:40
  • @Ricardo It is probably easier to ask than to read the documentation or to trey or look at the content of the include files. winsock.h is legacy windows socket and winsock2.h is version 2 (More functions). – fpiette Mar 18 '22 at 15:42
  • 1
    @Ricardo "*did you mean ...*" - no. I meant what I said. When using Winsock APIs on Windows, `winsock2.h` should be used instead of `winsock.h`. `sys/socket.h` does not exist on Windows. – Remy Lebeau Mar 18 '22 at 16:38
2

You should use the headers

winsock2.h

ws2tcpip.h

ws2spi.h

Also, before importing any of these, you should define _WIN32_WINNT to 0x501 to include all features for Windows XP or above and 0x601 for features for Windows 7 or above( In case they aren't already defined in any of the header included)