HostedDB - Dedicated UNIX Servers

Programming UNIX Sockets in C - Frequently Asked Questions: General Information and Concepts Previous Next Table of Contents

1. General Information and Concepts

1.1 About this FAQ

This FAQ is maintained by Vic Metcalfe (, with lots of assistance from Andrew Gierth ( I am depending on the true wizards to fill in the details, and correct my (no doubt) plentiful mistakes. The code examples in this FAQ are written to be easy to follow and understand. It is up to the reader to make them as efficient as required. I started this faq because after reading comp.unix.programmer for a short time, it became evident that a FAQ was needed.

The FAQ is available at the following locations:

Usenet: (Posted on the 21st of each month)

news.answers, comp.answers, comp.unix.answers, comp.unix.programmer



Please email me if you would like to correct or clarify an answer. I would also like to hear from you if you would like me to add a question to the list. I may not be able to answer it, but I can add it in the hopes that someone else will submit an answer. Every hour I seem to be getting even busier, so if I am slow to respond to your email, please be patient. If more than a week passes you may want to send me another one as I often put messages aside for later and then forget about them. I'll have to work on dealing with my mail better, but until then feel free to pester me a little bit.

1.2 Who is this FAQ for?

This FAQ is for C programmers in the Unix environment. It is not intended for WinSock programmers, or for Perl, Java, etc. I have nothing against Windows or Perl, but I had to limit the scope of the FAQ for the first draft. In the future, I would really like to provide examples for Perl, Java, and maybe others. For now though I will concentrate on correctness and completeness for C.

This version of the FAQ will only cover sockets of the AF_INET family, since this is their most common use. Coverage of other types of sockets may be added later.

1.3 What are Sockets?

Sockets are just like "worm holes" in science fiction. When things go into one end, they (should) come out of the other. Different kinds of sockets have different properties. Sockets are either connection-oriented or connectionless. Connection-oriented sockets allow for data to flow back and forth as needed, while connectionless sockets (also known as datagram sockets) allow only one message at a time to be transmitted, without an open connection. There are also different socket families. The two most common are AF_INET for internet connections, and AF_UNIX for unix IPC (interprocess communication). As stated earlier, this FAQ deals only with AF_INET sockets.

1.4 How do Sockets Work?

The implementation is left up to the vendor of your particular unix, but from the point of view of the programmer, connection-oriented sockets work a lot like files, or pipes. The most noticeable difference, once you have your file descriptor is that read() or write() calls may actually read or write fewer bytes than requested. If this happens, then you will have to make a second call for the rest of the data. There are examples of this in the source code that accompanies the faq.

1.5 Where can I get source code for the book [book title]?

Here is a list of the places I know to get source code for network programming books. It is very short, so please mail me with any others you know of.

Title: Unix Network Programming
Author: W. Richard Stevens (
Publisher: Prentice Hall, Inc.
ISBN: 0-13-949876-1

Title: Power Programming with RPC
Author: John Bloomer
Publisher: O'Reilly & Associates, Inc.
ISBN: 0-937175-77-3

Recommended by: Lokmanm Merican (
Author: Thomas Yager
Publisher: Addison Wesley, 1991
ISBN: 0-201-57727-5

1.6 Where can I get more information?

I keep a copy of the resources I know of on my socks page on the web. I don't remember where I got most of these items, but some day I'll check out their sources, and provide ftp information here. For now, you can get them at

There is a good TCP/IP FAQ maintained by George Neville-Neil ( which can be found at

Previous Next Table of Contents