Reputation: 31
Hello I am trying socket programming in c++. I need confirmation or say rejection for this logic. I think socket.h
header files are designed for UNIX systems whereas for Windows everything is done with winsock.h
.
Is this correct?
Upvotes: 2
Views: 4253
Reputation: 224942
For windows, you need winsock2.h
and ws2tcpip.h
.
On Linux, you need sys/socket.h
and sys/types.h
for the socket functions and netinet/in.h
for the IP related structs.
Some other differences:
SOCKET
for socket descriptors while Linux uses int
closesocket()
to close sockets, while Linux uses close()
WSAStartup()
before calling any socket functions, and WSACleanup()
when you are done using sockets.perror()
or strerror()
. On Windows, you have to call WSAGetLastError()
to get the error code and FormatMessage
to get the error text.Upvotes: 7
Reputation: 597941
Most platforms implement a BSD-compatible socket API, however different platforms do use different .h
files to declare their API. So, to answer your question, Yes, Windows uses winsock.h
(and winsock2.h
), whereas POSIX-based platforms like Unix/Linux use sys/socket.h
instead (socket.h
is defined as part of the POSIX standard, but Windows is not a POSIX-compatible platform). If you want to write cross-platform code, you have to take this into account. As well as other differences, which @dbush outlined in his answer.
Upvotes: 1
Reputation: 7
Every header file starting with "sys/..." is designed for UNIX environment. As for windows, they tend to use "win" as a prefix to every header file they use.
If you are interested in windows sockets (winsocks) i guess you should start from here.
As for UNIX sockets, this site seems very interesting and easy going :)
Upvotes: -2