1

Is there any C wrapper or library for operations on raw sockets (fd, select, ...)? No rocket science, just a readable, nice wrapper around FD_SET etc.

boost::asio is for C++, so useless here.

I know Beej's guide to network programming and how to operate on sockets, I look for the wrapper before I start wrapping it myself

Related

Community
  • 1
  • 1
Jakub M.
  • 32,471
  • 48
  • 110
  • 179

3 Answers3

1

On Linux, just use appropriate syscalls wrapped by the standard GNU libc library. Use socket(2) to get the fd on the raw socket, then use other syscalls like recv(2), poll(2) etc etc.

You probably need root privilege to use raw sockets.

You should prefer poll(2) to select(2) which is becoming obsolete. (Read more about the C10K issue).

See also socket(7) & packet(7) man pages.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • Well, that is want I wanted to wrap a bit - especially `FD_` operations and `select` – Jakub M. Oct 18 '12 at 13:03
  • Use `poll(2)` or `ppoll(2)` not `select(2)` which is obsolete (only able to handle file descriptors less than 256 or 1024). – Basile Starynkevitch Oct 18 '12 at 13:05
  • The notes of http://www.kernel.org/doc/man-pages/online/pages/man2/select.2.html and the `FD_SETSIZE` constant in system headers. You really should forget about `select(2)` and use `poll(2)` – Basile Starynkevitch Oct 18 '12 at 13:34
1

take a look at libdnet. Details: http://libdnet.sourceforge.net/

mzet
  • 577
  • 2
  • 7
1

You might want to take a look at rn by Dan Kegel (the c10k guy): http://www.kegel.com/rn/. It has plain-old-c interface around multiple select-like syscalls such as poll/epoll/sigio. There are significant performance difference between them at high fd counts and the best interfaces (epoll/kqueue) are non-portable.

The rn api is epoll-ish: you only add/remove individual fds instead of passing the whole list around like for select. Good old select with a FD_SET is limited at compile time to a number of sockets and copies the entire list from user-to-kernel space at every call. Using epoll you have separate syscalls to add/remove individual FDs which are fast even when you are waiting on 100K idle sockets.

All modern linux systems should support epoll. If you don't care about portability you can use it directly.

cdleonard
  • 6,570
  • 2
  • 20
  • 20