Hacker News new | past | comments | ask | show | jobs | submit login

Socket programming never clicked for me before I learned about the protocols themselves first. My brain just rejected using an API for something I didn't understand. So if you're like me, I highly recommend reading Comer's "Internetworking with TCP/IP" first, and sitting down to socket programming later.

That's three volumes... Anything shorter you can recommend?

Volume 1 of TCP/IP Illustrated by W. Richard Stevens is a good one. If you're very pressed for time, start with the picture of the TCP state machine and work outwards.

You really only need the first one.

Maybe it's just me but it took a long time before I found another "big" API as horrible as BSD sockets is.

Well, some APIs are not meant to be used directly, but hidden behind a class or more application-specific functions.

Maybe, but in C land that high level socket API never materialized in the standard library. And you are right that it is awful. I've been doing network programming for over 20 years now and I still have to look up the manual when making a TCP/UDP connection in C.

I've made my own wrapper a few times but often don't get to use it for various reasons.

  int tcp_connect(char* remote_host, short int port, options* options, static char** error_string);

  int tcp_listen(short int port, options* options, static char** error_string);

  int udp_listen(short int port, options* options, static char** error_string);
Does any necessary lookups for you and connects sets up the socket. The options structure can be NULL if you want the sane defaults, or you can zero it out and set only the options you care about like the connect timeout or flags like SO_REUSEADDR or O_NONBLOCK. Returns the socket number or -1 if an error occurs. In the event of an error the error_string is also set. The _listen variants spit out a socket ready for an accept() call.

If you need something weird then you're back to the low level BSD socket stuff, but this covers about 95% of my use cases. While it's only saving a handful of system calls, it ends up being quite a bit of boilerplate and error handling. Especially if you specify a connect timeout, since that is kind of awkward to do with the BSD socket API.

ptrace(2) comes to mind.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact