Changed on May 21/1998
This FAQ is maintained by Vic Metcalfe ( email@example.com), with lots of assistance from Andrew Gierth ( firstname.lastname@example.org). 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 for sockets was needed.
The FAQ is available at the following locations:
news.answers, comp.answers, comp.unix.answers, comp.unix.programmer
http://www.ibrado.com/sock-faq http://kipper.york.ac.uk/~vic/sock-faq http://www.ntua.gr/sock-faq
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.
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.
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
unix IPC (interprocess communication). As stated earlier, this FAQ deals
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
write() calls may actually
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
source code that accompanies the faq.
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 ( email@example.com)
Publisher: Prentice Hall, Inc.
Title: Power Programming with RPC
Author: John Bloomer
Publisher: O'Reilly & Associates, Inc.
Lokmanm Merican (firstname.lastname@example.org)
Title: UNIX PROGRAM DEVELOPMENT for IBM PC'S Including OSF/Motif
Author: Thomas Yager
Publisher: Addison Wesley, 1991
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 http://www.ibrado.com/sock-faq.
There is a good TCP/IP FAQ maintained by George Neville-Neil ( email@example.com) which can be found at http://www.visi.com/~khayes/tcpipfaq.html