16

I am looking to do socket communications (listen, accept, connect, recv, send, disconnect) in both linux and windows. My project is in C, so unless someone can think of a way for me to integrate C++ libraries into a C project the library will have to be in C as well.

Ultimately, I would like the library to have ipv6 support and non-blocking mode, however, these things are not essential.

Does anyone know of any libraries/cross-platform example code? Even just large code snippets would help. So far the few socket libraries I have found have been in C++.

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
64bit_twitchyliquid
  • 894
  • 2
  • 10
  • 22

6 Answers6

10

Off-hand, I can think of four libraries:

Christoph
  • 164,997
  • 36
  • 182
  • 240
  • Thankyou very much, thats an awesome start. At the moment, im looking at libuv, as it seems to have a good balance between size/simplicity and efficiency. Out of curiosity, what were your search terms that yielded those results? or did you know of them already? – 64bit_twitchyliquid Feb 08 '12 at 21:34
  • 1
    @64bit_twitchyliquid: I don't remember when I first came across GLib, but in my mind, it occupies the place of a standard lib for C more in tune with what other languages offer; I found APR and NSPR when I looked for a platform-abstraction lib and I know of libuv because it's used by the Rust programming language; actually, all of these except libuv are listed on http://www.shlomifish.org/open-source/portability-libs/ , the first result of a google search for *C cross platform abstraction lib* – Christoph Feb 08 '12 at 21:58
8

The plibsys library provides all the requested features: cross-platform and portable, lightweight, provides socket IPv4 and IPv6 support as well as many other useful things like multithreading. Works with sockets in non-blocking mode (though you can switch to a blocking one, too). Has quite a good documentation with the test code examples.

3

I don't know one library that complies both Windows and Linux but I think winsock is similar enough to Linux socket programming.

In particular it supplies you 'select()' and the other functions mentioned. I guess you will need a very thin #ifdef wrapper to avoid type casting warnings.

See here the winsock page for select

eyalm
  • 3,366
  • 19
  • 21
0

check HS Sockets C Source Library for Windows and Linux

Darshana
  • 2,462
  • 6
  • 28
  • 54
Chuck
  • 192
  • 2
  • 11
-2

Ptlib provides cross platform C++ code that works well for sockets. www.opalvoip.org

Copes nicely with threads. Some support for mac and BSD. Under active development and maintainance. MPL Core library in Ekiga - the preferred desktop app that does SIP & H.323

For windows - it compiles with MSVC. for linux, it compiles with gcc autoconf make etc.

OK, it is C++, but you can work with that... Have a the source inside ptlib/samples - there are many examples of using ptlib there.

-3

The Boost library includes these features.

Community
  • 1
  • 1
Salvo
  • 1