Question:
I am aware that an ANSI standard C program can be compiled on both Windows and Linux.
But when it comes to using sockets ? Isn't this part of the ANSI C standard? Because when I use sockets on Windows, I have to use a library called "Winsock.h"
, on Linux this library doesn't exist, and it uses another library called "socket.h"
. In this case, a program on Windows that uses the "Winsock.h"
library will give an error when compiled on Linux, and vice versa. However, Sockets aren't in the default?
Another thing is the fork()
function, this function only exists in Linux to make threads I believe. So this function, being non-existent in Windows, is outside the ANSI C standard?
Answer:
You're right. Socket libraries are not part of ANSI. This standard defines just about the language and everything that is considered the standard library of the language that has only very basic operations, to do the minimum necessary and that doesn't usually get involved with the operating system except in very simple and standardized things like terminal and file access, for example.
Even these accesses to the operating system are done in a limited way. When you want more sophisticated access, you already need non-standard libraries that may or may not be available for various compilers, operating systems or even processors.
The same goes for functions that handle processes like fork()
.
In these cases there are usually libraries that abstract the operating system. That is, they internally know how to work with each operating system where it will be used but externally it works the same way.
Examples:
All of these work at least with Windows sockets and POSIX sockets.
All allow working with threads on both POSIX and Windows systems. There is even a port of the pThreads library , which is POSIX, for Windows.