?? theory.html
字號:
<HTML><HEAD><TITLE>What is a socket?</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.70"><LINKREL="HOME"TITLE="Beej's Guide to Network Programming"HREF="index.html"><LINKREL="PREVIOUS"TITLE="Intro"HREF="intro.html"><LINKREL="NEXT"TITLE="structs and Data Handling"HREF="structs.html"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=utf-8"></HEAD><BODYCLASS="sect1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Beej's Guide to Network Programming</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="intro.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="structs.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="sect1"><H1CLASS="sect1"><ANAME="theory">2. What is a socket?</A></H1><P>You hear talk of "sockets" all the time, and perhaps you arewondering just what they are exactly. Well, they're this: a way tospeak to other programs using standard Unix file descriptors.</P><P>What?</P><P>Ok--you may have heard some Unix hacker state, "Jeez,<EM>everything</EM> in Unix is a file!" What that personmay have been talking about is the fact that when Unix programs do anysort of I/O, they do it by reading or writing to a file descriptor. Afile 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, apipe, a terminal, a real on-the-disk file, or just about anything else.Everything in Unix <EM>is</EM> a file! So when you want tocommunicate with another program over the Internet you're gonna do itthrough a file descriptor, you'd better believe it.</P><P>"Where do I get this file descriptor for network communication,Mr. Smarty-Pants?" is probably the last question on your mind rightnow, but I'm going to answer it anyway: You make a call to the<TTCLASS="function">socket()</TT> system routine. It returns the socketdescriptor, and you communicate through it using the specialized<TTCLASS="function">send()</TT> and <TTCLASS="function">recv()</TT>(<BCLASS="command"><AHREF="http://linux.com.hk/man/showman.cgi?manpath=/man/man2/send.2.inc"TARGET="_top">mansend</A></B>, <BCLASS="command"><AHREF="http://linux.com.hk/man/showman.cgi?manpath=/man/man2/recv.2.inc"TARGET="_top">man recv</A></B>) socketcalls.</P><P>"But, hey!" you might be exclaiming right about now. "If it's afile descriptor, why in the name of Neptune can't I just use the normal<TTCLASS="function">read()</TT> and <TTCLASS="function">write()</TT> calls tocommunicate through the socket?" The short answer is, "You can!" Thelonger answer is, "You can, but <TTCLASS="function">send()</TT> and<TTCLASS="function">recv()</TT> offer much greater control over your datatransmission."</P><P>What next? How about this: there are all kinds of sockets. Thereare DARPA Internet addresses (Internet Sockets), path names on a localnode (Unix Sockets), CCITT X.25 addresses (X.25 Sockets that you cansafely ignore), and probably many others depending on which Unix flavoryou run. This document deals only with the first: InternetSockets.</P><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="twotypes">2.1. Two Types of Internet Sockets</A></H2><P>What's this? There are two types of Internet sockets? Yes.Well, no. I'm lying. There are more, but I didn't want to scare you.I'm only going to talk about two types here. Except for this sentence,where I'm going to tell you that "Raw Sockets" are also very powerfuland you should look them up.</P><P>All right, already. What are the two types? One is "StreamSockets"; the other is "Datagram Sockets", which may hereafter bereferred to as "<TTCLASS="constant">SOCK_STREAM</TT>" and"<TTCLASS="constant">SOCK_DGRAM</TT>", respectively. Datagram sockets aresometimes called "connectionless sockets". (Though they can be<TTCLASS="function">connect()</TT>'d if you really want. See <AHREF="syscalls.html#connect"><TTCLASS="function">connect()</TT></A>, below.)</P><P>Stream sockets are reliable two-way connected communicationstreams. If you output two items into the socket in the order "1, 2",they will arrive in the order "1, 2" at the opposite end. They willalso be error free. Any errors you do encounter are figments of yourown deranged mind, and are not to be discussed here.</P><P>What uses stream sockets? Well, you may have heard of the<BCLASS="command">telnet</B> application, yes? It uses stream sockets.All the characters you type need to arrive in the same order you typethem, right? Also, web browsers use the HTTP protocol which uses streamsockets to get pages. Indeed, if you telnet to a web site on port 80,and type "<TTCLASS="computeroutput">GET /</TT>", it'll dump the HTMLback at you!</P><P>How do stream sockets achieve this high level of data transmissionquality? They use a protocol called "The Transmission ControlProtocol", otherwise known as "TCP" (see <AHREF="http://www.rfc-editor.org/rfc/rfc793.txt"TARGET="_top">RFC-793</A> for extremely detailed infoon TCP.) TCP makes sure your data arrives sequentially and error-free.You may have heard "TCP" before as the better half of "TCP/IP" where"IP" stands for "Internet Protocol" (see <AHREF="http://www.rfc-editor.org/rfc/rfc791.txt"TARGET="_top">RFC-791</A>.) IP deals primarily withInternet routing and is not generally responsible for dataintegrity.</P><P>Cool. What about Datagram sockets? Why are they calledconnectionless? What is the deal, here, anyway? Why are theyunreliable? Well, here are some facts: if you send a datagram, it mayarrive. It may arrive out of order. If it arrives, the data within thepacket will be error-free.</P><P>Datagram sockets also use IP for routing, but they don't use TCP;they use the "User Datagram Protocol", or "UDP" (see <AHREF="http://www.rfc-editor.org/rfc/rfc768.txt"TARGET="_top">RFC-768</A>.)</P><P>Why are they connectionless? Well, basically, it's because youdon't have to maintain an open connection as you do with stream sockets.You just build a packet, slap an IP header on it with destinationinformation, and send it out. No connection needed. They are generallyused for packet-by-packet transfers of information. Sampleapplications: <BCLASS="command">tftp</B>, <BCLASS="command">bootp</B>,etc.</P><P>"Enough!" you may scream. "How do these programs even work ifdatagrams might get lost?!" Well, my human friend, each has it's ownprotocol on top of UDP. For example, the tftp protocol says that foreach packet that gets sent, the recipient has to send back a packet thatsays, "I got it!" (an "ACK" packet.) If the sender of the originalpacket gets no reply in, say, five seconds, he'll re-transmit the packetuntil he finally gets an ACK. This acknowledgment procedure is veryimportant when implementing <TTCLASS="constant">SOCK_DGRAM</TT>applications.</P></DIV><DIVCLASS="sect2"><H2CLASS="sect2"><ANAME="lowlevel">2.2. Low level Nonsense and Network Theory</A></H2><P>Since I just mentioned layering of protocols, it's time to talkabout how networks really work, and to show some examples of how<TTCLASS="constant">SOCK_DGRAM</TT> packets are built. Practically, you canprobably skip this section. It's good background, however.</P><DIVCLASS="figure"><ANAME="figure1"></A><P><B>Figure 1. Data Encapsulation.</B></P><DIVCLASS="mediaobject"><P><IMGSRC="dataencap.gif"ALT="[Encapsulated Protocols Diagram]"></IMG></P></DIV></DIV><P>Hey, kids, it's time to learn about <EM><AHREF="theory.html#figure1">DataEncapsulation</A></EM>! This is very very important. It's soimportant that you might just learn about it if you take the networkscourse here at Chico State <TTCLASS="computeroutput">;-)</TT>.Basically, it says this: a packet is born, the packet is wrapped("encapsulated") in a header (and rarely a footer) by the first protocol(say, the TFTP protocol), then the whole thing (TFTP header included) isencapsulated again by the next protocol (say, UDP), then again by thenext (IP), then again by the final protocol on the hardware (physical)layer (say, Ethernet).</P><P>When another computer receives the packet, the hardware strips theEthernet header, the kernel strips the IP and UDP headers, the TFTPprogram strips the TFTP header, and it finally has the data.</P><P>Now I can finally talk about the infamous <EM>LayeredNetwork Model</EM>. This Network Model describes a system ofnetwork functionality that has many advantages over other models. Forinstance, you can write sockets programs that are exactly the samewithout caring how the data is physically transmitted (serial, thinEthernet, AUI, whatever) because programs on lower levels deal with itfor you. The actual network hardware and topology is transparent to thesocket programmer.</P><P>Without any further ado, I'll present the layers of the full-blownmodel. Remember this for network class exams:</P><P> <P></P><UL><LI><P>Application</P></LI><LI><P>Presentation</P></LI><LI><P>Session</P></LI><LI><P>Transport</P></LI><LI><P>Network</P></LI><LI><P>Data Link</P></LI><LI><P>Physical</P></LI></UL></P><P>The Physical Layer is the hardware (serial, Ethernet, etc.). TheApplication Layer is just about as far from the physical layer as youcan imagine--it's the place where users interact with thenetwork.</P><P>Now, this model is so general you could probably use it as anautomobile repair guide if you really wanted to. A layered model moreconsistent with Unix might be:</P><P> <P></P><UL><LI><P>Application Layer (<EM>telnet, ftp,etc.</EM>)</P></LI><LI><P>Host-to-Host Transport Layer (<EM>TCP,UDP</EM>)</P></LI><LI><P>Internet Layer (<EM>IP androuting</EM>)</P></LI><LI><P>Network Access Layer (<EM>Ethernet, ATM, orwhatever</EM>)</P></LI></UL></P><P>At this point in time, you can probably see how these layerscorrespond to the encapsulation of the original data.</P><P>See how much work there is in building a simple packet? Jeez!And you have to type in the packet headers yourself using"<BCLASS="command">cat</B>"! Just kidding. All you have to do for streamsockets is <TTCLASS="function">send()</TT> the data out. All you have to dofor datagram sockets is encapsulate the packet in the method of yourchoosing and <TTCLASS="function">sendto()</TT> it out. The kernel buildsthe Transport Layer and Internet Layer on for you and the hardware doesthe Network Access Layer. Ah, modern technology.</P><P>So ends our brief foray into network theory. Oh yes, I forgot totell you everything I wanted to say about routing: nothing! That'sright, I'm not going to talk about it at all. The router strips thepacket to the IP header, consults its routing table, blah blah blah.Check out the <AHREF="http://www.rfc-editor.org/rfc/rfc791.txt"TARGET="_top">IP RFC</A> if youreally really care. If you never learn about it, well, you'lllive.</P></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="intro.html">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="structs.html">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Intro</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"> </TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><TTCLASS="type">struct</TT>s and Data Handling</TD></TR></TABLE></DIV></BODY></HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -