Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
Guide to Network Programming

Guide to Network Programming

Ratings: (0)|Views: 74|Likes:
Published by shafiqshaheen
Guide to Network Programming
Guide to Network Programming

More info:

Published by: shafiqshaheen on Oct 30, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

07/01/2010

pdf

text

original

 
Beej's Guide to Network Programming
Using Internet Sockets
Brian “Beej Jorgensen” Hallbeej@beej.us
Version 3.0.13March 23, 2009
Copyright © 2009 Brian “Beej Jorgensen” Hall
 
Thanks to everyone who has helped in the past and future with me getting this guide written. Thanks to Ashley forhelping me coax the cover design into the best programmer art I could. Thank you to all the people who produce theFree software and packages that I use to make the Guide: GNU, Linux, Slackware, vim, Python, Inkscape, Apache FOP,Firefox, Red Hat, and many others. And finally a big thank-you to the literally thousands of you who have written inwith suggestions for improvements and words of encouragement.I dedicate this guide to some of my biggest heroes and inpirators in the world of computers: Donald Knuth, BruceSchneier, W. Richard Stevens, and The Woz, my Readership, and the entire Free and Open Source SoftwareCommunity.This book is written in XML using the vim editor on a Slackware Linux box loaded with GNU tools. The cover “art”and diagrams are produced with Inkscape. The XML is converted into HTML and XSL-FO by custom Python scripts.The XSL-FO output is then munged by Apache FOP to produce PDF documents, using Liberation fonts. The toolchainis composed of 100% Free and Open Source Software.
Unless otherwise mutually agreed by the parties in writing, the author offers the work as-is and makes no representationsor warranties of any kind concerning the work, express, implied, statutory or otherwise, including, without limitation, warranties of title, merchantibility, fitness for a particular purpose, noninfringement, or the absence of latent or otherdefects, accuracy, or the presence of absence of errors, whether or not discoverable.Except to the extent required by applicable law, in no event will the author be liable to you on any legal theory for anyspecial, incidental, consequential, punitive or exemplary damages arising out of the use of the work, even if the authorhas been advised of the possibility of such damages.This document is freely distributable under the terms of the Creative Commons Attribution-Noncommercial-No DerivativeWorks 3.0 License. See the Copyright and Distribution section for details.
Copyright © 2009 Brian “Beej Jorgensen” Hall
 
iii
Contents
1. Intro................................................................................................................................................................1
1.1. Audience11.2. Platform and Compiler11.3. Official Homepage and Books For Sale11.4. Note forSolaris/SunOS Programmers11.5. Note forWindows Programmers11.6. Email Policy31.7. Mirroring31.8. Note forTranslators31.9. Copyright and Distribution3
2. What is a socket?.........................................................................................................................................4
2.1. Two Types of Internet Sockets42.2. Low levelNonsense and Network Theory5
3. IP Addresses,
struct
s, and Data Munging..............................................................................................7
3.1. IP Addresses, versions 4 and 673.2. Byte Order93.3.
struct
s103.4. IP Addresses, Part Deux12
4. Jumping from IPv4 to IPv6......................................................................................................................155. System Calls orBust..................................................................................................................................16
5.1.
getaddrinfo()
socket()
bind()
 — What port am I on?195.4.
connect()
listen()
 Will somebody please call me?225.6.
accept()
send()
and
recv()
 Talk to me, baby!235.8.
sendto()
 —Talk tome, DGRAM-style245.9.
close()
 and
 Get outta my face!255.10.
getpeername()
 —Whoare you?255.11.
gethostname()
6. Client-Server Background.........................................................................................................................27
6.1. A SimpleStream Server276.2. A SimpleStream Client296.3. Datagram Sockets31
7. Slightly Advanced Techniques...................................................................................................................35
7.1. Blocking357.2.
select()
 —Synchronous I/O Multiplexing357.3. HandlingPartial
s407.4. Serialization—How to Pack Data417.5. Son of Data Encapsulation497.6. BroadcastPackets—Hello, World!51

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->