Beej's Guide to Network Programming
Using Internet Sockets
Version 1.5.4 (17-May-1998)
Hey! Socket programming got you down? Is this stuff just a little too difficult to figure out from the man pages? You want to do cool Internet programming, but you don't have time to wade through a gob of structs trying to figure out if you have to call bind() before you connect(), etc., etc. Well, guess what! I've already done this nasty business, and I'm dying to share the information with everyone! You've come to the right place. This document should give the average competent C programmer the edge s/he needs to get a grip on this networking noise. Audience
This document has been written as a tutorial, not a reference. It is probably at its best when read by individuals who are just starting out with socket programming and are looking for a foothold. It is certainly not the complete guide to sockets programming, by any means. Hopefully, though, it'll be just enough for those man pages to start making sense... :-) Platform and Compiler
Most of the code contained within this document was compiled on a Linux PC using Gnu's gcc compiler. It was also found to compile on HPUX using gcc. Note that every code snippet was not individually tested.
What is a socket?
Two Types of Internet Sockets
Low level Nonsense and Network Theory
structs--Know these, or aliens will destroy the planet!
Convert the Natives!
IP Addresses and How to Deal With Them
socket()--Get the File Descriptor!
bind()--What port am I on?
listen()--Will somebody please call me?
1 of 26 12.03.99 01:21
Beej's Guide to Network Programming file:///C|/Eigene Dateien/Manualz/not ad...ramming; Using Internet Sockets/net.html accept()--"Thank you for calling port 3490."
send() and recv()--Talk to me, baby!
sendto() and recvfrom()--Talk to me, DGRAM-style
close() and shutdown()--Get outta my face!
getpeername()--Who are you?
gethostname()--Who am I?
DNS--You say "whitehouse.gov", I say "22.214.171.124"
A Simple Stream Server
A Simple Stream Client
select()--Synchronous I/O Multiplexing. Cool!
Disclaimer and Call for Help
What is a socket?
You hear talk of "sockets" all the time, and perhaps you are wondering just what they are exactly. Well, they're this: a way to speak to other programs using standard Unix file descriptors. What?
Ok--you may have heard some Unix hacker state, "Jeez, everything in Unix is a file!" What that person may have been talking about is the fact that when Unix programs do any sort of I/O, they do it by reading or writing to a file descriptor. A file descriptor is simply an integer associated with an open file. But (and here's the catch), that file can be a network connection, a FIFO, a pipe, a terminal, a real on-the-disk file, or just about anything else. Everything in Unix is a file! So when you want to communicate with another program over the Internet you're gonna do it through a file descriptor, you'd better believe it.
"Where do I get this file descriptor for network communication, Mr. Smarty-Pants?" is probably the last question on your mind right now, but I'm going to answer it anyway: You make a call to the socket() system routine. It returns the socket descriptor, and you communicate through it using the specialized send() and recv() ("man send", "man recv") socket calls. "But, hey!" you might be exclaiming right about now. "If it's a file descriptor, why in the hell can't I just use the normal read() and write() calls to communicate through the socket?" The short answer is, "You can!" The longer answer is, "You can, but send() and recv() offer much greater control over your data transmission."
What next? How about this: there are all kinds of sockets. There are DARPA Internet addresses (Internet Sockets), path names on a local node (Unix Sockets), CCITT X.25...
References: Enough of this--back to coding! ;-)
Copyright © 1995, 1996 by Brian "Beej" Hall
Please join StudyMode to read the full document