添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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>
                Yes, there is no sys/socket.h on Windows. You can start reading Windows documentation here: learn.microsoft.com/en-us/windows/win32/winsock/…
– SergeyA
                May 27, 2021 at 16:21
                somewhat <O/T>, 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 in the past. It requires c++, but will build and run on linux and windows from a single implementation.
– yano
                May 27, 2021 at 16:27
                @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, 2021 at 16:36

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.

@RemyLebeau, did you mean "For Windows, you have to use winsock.h" - or winsock2.h, which replaces sys/socket.h"? – Ricardo Mar 18, 2022 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, 2022 at 15:42 @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, 2022 at 16:38

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.