Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Know of any cross platform socket library (windows & Linux) in C?

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++.

like image 483
64bit_twitchyliquid Avatar asked Feb 08 '12 20:02

64bit_twitchyliquid


People also ask

Are sockets cross platform?

The regular sockets (those in AF_INET address family) which you need to build a socket server are equally supported on all platforms.

Do sockets work on Windows?

Run-time requirementsWindows Sockets 2 can be used on all Windows platforms.

How is the socket programming in Linux different from that in Windows?

This means that on Linux and macOS, you can generally use all of the general purpose file functions with socket handles (e.g. read() , write() ). On Windows, socket handles can only be used with special socket functions.

What are socket libraries?

A socket library is a library that implements sockets so that you can use them in your program for Internet communication.


2 Answers

Off-hand, I can think of four libraries:

  • GLib Channels from the GLib framework can abstract socket usage, but you'll need platform-specific code for socket creation.

  • libuv is a platform abstraction layer for node.js and handles, among other things, sockets and async IO.

  • The Apache Portable Runtime also contains network routines.

  • The Netscape Portable Runtime does socket manipulation as well.

like image 75
Christoph Avatar answered Sep 21 '22 14:09

Christoph


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.

like image 22
Alexander Saprykin Avatar answered Sep 21 '22 14:09

Alexander Saprykin