How can I use sys/socket.h on Windows?

Viewed 13491

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>
2 Answers

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.

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

Related