Beej's Guide to Network Programming

Using Internet Sockets

Brian "Beej" Hall


beej@piratehaven.org

Revision History
Revision Version 1.0.0August, 1995Revised by: beej
Initial version.
Revision Version 1.5.5January 13, 1999Revised by: beej
Latest HTML version.
Revision Version 2.0.0March 6, 2001Revised by: beej
Converted to DocBook XML, corrections, additions.
Revision Version 2.0.1March 7, 2001Revised by: beej
Minor corrections.
Revision Version 2.0.2March 16, 2001Revised by: beej
inet_ntoa() should have been inet_aton() in some places.
Revision Version 2.0.3April 2, 2001Revised by: beej
inet_aton() return values corrected, selectserver changed, typos fixed, added recvtimeout().
Revision Version 2.1.0May 3, 2001Revised by: beej
Fixed buffer overruns in client.c and listener.c, made server.c robustly reap zombies, added email policy.
Revision Version 2.2.0June 24, 2001Revised by: beej
Updates QnA, Windows, talker.c, PF_INET info, accept() demo, no more bzero()s, Solaris setsockopt().
Revision Version 2.2.1June 25, 2001Revised by: beej
Minor QnA update, added Amazon affiliate stuff.

Table of Contents
1. Intro
1.1. Audience
1.2. Platform and Compiler
1.3. Official Homepage
1.4. Note for Solaris/SunOS Programmers
1.5. Note for Windows Programmers
1.6. Email Policy
1.7. Mirroring
1.8. Note for Translators
1.9. Copyright and Distribution
2. What is a socket?
2.1. Two Types of Internet Sockets
2.2. Low level Nonsense and Network Theory
3. structs and Data Handling
3.1. Convert the Natives!
3.2. IP Addresses and How to Deal With Them
4. System Calls or Bust
4.1. socket()--Get the File Descriptor!
4.2. bind()--What port am I on?
4.3. connect()--Hey, you!
4.4. listen()--Will somebody please call me?
4.5. accept()--"Thank you for calling port 3490."
4.6. send() and recv()--Talk to me, baby!
4.7. sendto() and recvfrom()--Talk to me, DGRAM-style
4.8. close() and shutdown()--Get outta my face!
4.9. getpeername()--Who are you?
4.10. gethostname()--Who am I?
4.11. DNS--You say "whitehouse.gov", I say "198.137.240.92"
5. Client-Server Background
5.1. A Simple Stream Server
5.2. A Simple Stream Client
5.3. Datagram Sockets
6. Slightly Advanced Techniques
6.1. Blocking
6.2. select()--Synchronous I/O Multiplexing
6.3. Handling Partial send()s
6.4. Son of Data Encapsulation
7. More References
7.1. man Pages
7.2. Books
7.3. Web References
7.4. RFCs
8. Common Questions
9. Disclaimer and Call for Help