简体   繁体   中英

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM