C/C++ Good way to learn basics of network programming in FreeBSD

decuser

Active Member

Reaction score: 30
Messages: 137

I've looked at the Handbook, Developer's handbook, and pored through a number of google searches and haven't found what I'm looking for. I would like to learn to write network programs on FreeBSD, in C. What is a good way for me to learn the basics? I have seen lots of books, but I'm a little skeptical about the currency/FreeBSD relevance of many of them. Take one example - Stevens, 1994, Unix Network Programming... is this how we would program network programs in FreeBSD in 2020? If not Stevens, 1994, how about Stevens, 2004 , Unix Network Programming Vol 1 3rd Edition, The Sockets Networking API... One volume, just for Sockets? Seems like there might be something a teensy bit more concise that would be current?

Anyhow, this is a little confusing to a BSD newcomer and nascent network programmer. The manpages are stellar for detailed information about a function, but I'm needing something more introductory and would appreciate y'alls shared wisdom. Either a book recommendation, a link to a great resource, some documentation that's on the system, a youtube series on FreeBSD network programming basics, a map of the relevant manpages, pretty much any constructive help would be greatly appreciated.
 

ralphbsz

Daemon

Reaction score: 1,553
Messages: 2,485

What's your goal? To get something to work, to solve an existing problem? To learn the general layout of how sockets work, so you can become a journeyman programmer with some networking skills? Or to study the gory details and all edge cases, including protocol design, to become a guru?

If you are trying to solve a problem, then sockets are way too low level. You'll have to deal with designing your own protocol, you'll have to handle many edge cases, too much work. Much easier to use a pre-built RPC mechanism. I am very fond of gRPC these days, which I use at home (from Python) for my home hardware monitoring and control system (with both FreeBSD and Linux, on a mix of normal platforms and RPi). Using an RPC mechanism allows you to focus on real-world problems, namely sending messages with well-defined semantics to other nodes and getting replies, without bogging down in the numerous details.

If you are just trying to do the simple stuff but insist on learning sockets, then I don't know a simple book or guide for that. Sure, examples are all over the net, and with a little elbow grease, you'll even get then to work. I've always found that way of doing it very frustrating.

If you want all the details, the Stevens book is unbeatable. And even still correct, since the basics have not changed. You seem to be confused about there being different books, that's easy to fix: The 1994 book is the first edition, where everything was in one volume. The later editions split it into two volumes, the first one being just sockets, the second one how to use sockets, build protocols on them, and build communications out of that. I would go with the most recent edition (which is probably the 2004 you mentioned); Stevens passed away sometime in the early 2000s, so new editions seem unlikely.
 
OP
decuser

decuser

Active Member

Reaction score: 30
Messages: 137

What's your goal? To get something to work, to solve an existing problem? To learn the general layout of how sockets work, so you can become a journeyman programmer with some networking skills? Or to study the gory details and all edge cases, including protocol design, to become a guru?
Great question. I want to better understand networking and I do best by doing. So, I'd like to be able to create a client server application from the ground up (so to speak). I guess I'd say I want to be able to write Application Layer programs that were able to communicate locally or on the local network, or across the internet. I am a teacher, so I'd like to know it well enough to be able to pass it along, but I don't need to be a guru - let's say I want to strive to the journeyman level of understanding... in a pinch be able to grab a reference and write a program that talks to an SMTP server (send ehlo, get, etc over port 25), or a client/server app, that kind of thing.

The problem I have with the Stevens book is it's humongous, particularly the 2004 edition where he split it up into two volumes. But if the 1994 edition is still reasonably accurate (and BSD friendly), then even though it's hefty, I could probably get through that.
 

kpedersen

Daemon

Reaction score: 848
Messages: 1,843

I quite like the following:

- Nice examples that you know are probably going to work well on FreeBSD since it is from our own documentation.

- Since Linux is an emulation of a good operating system, it also uses similar APIs making the tutorials relevant. I started with this many years back. It also has a good book listing at the end.

- And this is a bit more "spoon feedy" but a great introduction
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 9,119
Messages: 33,687

"TCP/IP Illustrated" is also recommended. All three volumes. But those are also quite large volumes. They do describe everything in excruciating detail though. They're my go-to resource when it comes to TCP/IP details.
 

Jose

Well-Known Member

Reaction score: 158
Messages: 302

...in a pinch be able to grab a reference and write a program that talks to an SMTP server (send ehlo, get, etc over port 25)...
You can fake this using the good ol' telnet(1) client. You mess with SMTP, IMAP, and even HTTP servers to some extent in this way. It'll give you a feel for how the old school text-over-sockets protocols work.

I used Python The last time I needed something quick and dirty to test a slightly more complex protocol.

I've used Java/Jetty in the past if I needed to create a somewhat more robust server.

I only dive down into C if I absolutely need max performance and can't have the garbage collector mucking things up whenever it feels like it.
 
OP
decuser

decuser

Active Member

Reaction score: 30
Messages: 137

At this point, I'm reading the dev handbook and some of the Stevens 1994 edition and another book I had on my shelves - Kochan and Wood, 1989, Unix Networking. I'll see how far that gets me.

Just how different is network programming in BSD 4.3 vs BSD 4.4 or its descendants (FreeBSD 12.1) in terms of sockets and system calls?
 
OP
decuser

decuser

Active Member

Reaction score: 30
Messages: 137

Turns out the old code works with very few modifications. I'll start with the classics and work my way forward :).
 

kpedersen

Daemon

Reaction score: 848
Messages: 1,843

One thing I would stay away from is gethostbyname.
(https://linux.die.net/man/3/gethostbyname)

It is deprecated and yet still tempting to use because it is convenient. However as soon as you try to move on to non-blocking connecting, it soon becomes unmanageable. Most tutorials unfortunately still use this which is annoying.

Instead you should use getaddrinfo() passing in hints, and then iterate through the results and attempt a connection at each one until one succeeds (or in the case of non blocking, none of them are EINPROGRESS).

Ironically, I find the following Microsoft Winsock example very good: https://docs.microsoft.com/en-us/windows/win32/winsock/complete-client-code

Since Winsock was inspired by (an old version of) BSD sockets the code is fairly identical. Just miss off the WSAStartup stuff.
 
OP
decuser

decuser

Active Member

Reaction score: 30
Messages: 137

kpedersen, Thanks for the heads up. I wouldn't have known it could be an issue. I'll keep it in mind.

When I clicked the link, I was suddenly transported back in time to my days messing with Windows programming, C++, ASP.NET, and Visual Basic - yuck :). iResult, WSACleanup... I don't miss that style of coding a bit. What was it called? Hungarian? OP shivers...

That said, it's a great example!
 

Paul Floyd

Active Member

Reaction score: 27
Messages: 100

I wouldn't say there will never be new editions of Steven's TCP books. The copyright is owned by Addison-Wesley (part of Pearsons).

There is precedent for updated books. Steven's other masterpiece, 'Advanced Programming in the Unix Environment' or APUE has had a couple of newer editions with updates and additions from Stephen Rago.
 
Top