0

I want to write a socket program in Linux. So it'll use glibc system calls like socket(), bind(), listen(), write() etc.

I wonder, can i compile it without any changing in FreeBSD, Solaris or Mac OS? If yes, is it called "posix standards"?

4 Answers4

2

Socket (), bind (), write () are all Posix functions and using them will make your code portable across a wide range of POSIX complaint operating systems.

Linux uses glibc, however other POSIX complaint OS will use any other libc not necessarily glibc. But all the above functions (system calls) will be in implemented with the same signature and functionality and you can compile then and run the same code everywhere.

http://en.wikipedia.org/wiki/Berkeley_sockets#BSD_vs_POSIX

Pradheep
  • 3,553
  • 1
  • 27
  • 35
  • And even on Linux you can use other libc liraries like http://musl-libc.org/ and they do stay Posix compliant. – Basile Starynkevitch Feb 25 '13 at 06:41
  • So, all functions in the glibc are implemented in FreeBSD as well as? – yossi kohen Feb 25 '13 at 11:55
  • There are couple of things here 1.Socket, bind , write are all posix complaint so that means whatever libc (glibc or whatever ) thats installed in the FREEBSD machine will make sure you get all the above functions. – Pradheep Feb 25 '13 at 12:59
1

The socket calls originated with BSD but today all Unix-like OSs support them. Windows also somewhat supports these in its own flavor (called Winsock).

I don't think these are part of Posix but in reality you shouldn't have portability issues.

Btw, when you do a 'man 2 socket' (or whatever call) it shows useful history and standards info at the bottom.

seand
  • 5,168
  • 1
  • 24
  • 37
  • He's talking about sockets calls, not pthreads; its not the same thing, – seand Feb 25 '13 at 02:23
  • There's some information about the incorporation of these BSD standards into POSIX - and how they've changed slightly in the process - at http://en.wikipedia.org/wiki/Berkeley_sockets. – Tony Delroy Feb 25 '13 at 05:40
1

All the systems you mention follow the Single UNIX Specification, and have POSIX:2001 as a common denominator (see the compliance section), so that's what you want to target.

The GNU libc has many functions that are not in POSIX however. To see whether you can use a particular function you can look at the "CONFORMING TO" section of the relevant manpage, or refer to the GNU libc manual. For example we can see in socket(2) that socket conforms to POSIX.1-2001, so you can use it.

For more background on this, read 1.2 Standards and Portability.

0

One of the most portable ways you can do networking is using the Asio library from Boost:

http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio.html

It's easy to use and portable to both Windows as well as Unix/Posix systems (like Linux, Mac, the various BSDs, etc.)

Nikos C.
  • 50,738
  • 9
  • 71
  • 96