Network Programming 2

CS 493/693 Lecture, Dr. Lawlor, 2006/01/25

Writing TCP Code

One can imagine lots of programming interfaces for talking to the network, and there are in fact lots of totally different interfaces for talking on NetBIOS, AppleTalk, etc.  But suprisingly there's basically only one major programming interface used for talking on a TCP/IP network: Berkeley sockets.

The Berkely sockets interface is implemented in:
Brian Hall, or "Beej", maintains the definitive readable introduction to Berkeley sockets programming, Beej's Guide to Network Programming.  He's got a zillion examples and a readable style.  Go there.

Bare Berkeley sockets are pretty tricky and ugly, especially for creating connections.  The problem is Berkeley sockets support all sorts of other protocols, addressing modes, and other features like raw sockets that have serious security implications.  But when I write TCP code, I find it a lot easier to use my own little library of public domain utility routines called "osl/socket.h".  It's way too nasty to write portable Berkeley code for basic TCP, so I'll give examples using my library. 

My library uses a few funny datatypes:
To connect to a server "serverName" at TCP port 8080, and send some data to it, you'd call:
Easy, right?  The same program is a great deal longer in pure Berkeley sockets, since you've got to deal with error handling (and not all errors are fatal!), a long and complicated address setup process, etc.

To create a network server, you'd call:
Check out tinyclient.cpp and tinyserver.cpp in the hw1 support directory, which do nothing but make these exact calls.