{"id":23368,"date":"2023-03-30T06:36:52","date_gmt":"2023-03-30T06:36:52","guid":{"rendered":"https:\/\/www.booksofall.com\/cn\/?post_type=product&#038;p=23368"},"modified":"2023-03-30T06:36:52","modified_gmt":"2023-03-30T06:36:52","slug":"beejs-guide-to-network-programming-using-internet-sockets","status":"publish","type":"product","link":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/","title":{"rendered":"Beej&#8217;s Guide to Network Programming &#8211; Using Internet Sockets"},"content":{"rendered":"<h2>Chapter 2 &#8211;\u00a0What is a socket?<\/h2>\n<p>You hear talk of \u201csockets\u201d all the time, and perhaps you are wondering just what they are exactly. Well, they\u2019re this: a way to speak to other programs using standard Unix file descriptors.<\/p>\n<p>What?<\/p>\n<p>Ok\u2014you may have heard some Unix hacker state, \u201cJeez,\u00a0<i>everything\u00a0<\/i>in <a href=\"https:\/\/en.wikipedia.org\/wiki\/Unix\">Unix<\/a> is a file!\u201d 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\u2019s the catch), that file can be a network connection, a <a href=\"https:\/\/www.investopedia.com\/terms\/f\/fifo.asp\">FIFO<\/a>, a pipe, a terminal, a real on-the-disk file, or just about anything else. Everything in Unix\u00a0<i>is\u00a0<\/i>a file! So when you want to communicate with another program over the Internet you\u2019re gonna do it through a file descriptor, you\u2019d better believe it.<\/p>\n<p>\u201cWhere do I get this file descriptor for network communication, Mr. Smarty-Pants?\u201d is probably the last question on your mind right now, but I\u2019m going to answer it anyway: Youmake 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.<\/p>\n<p>\u201cBut, hey!\u201d you might be exclaiming right about now. \u201cIf it\u2019s a file descriptor, why in the name of Neptune can\u2019t I just use the normal read() and write() calls to communicate through the socket?\u201d The short answer is, \u201cYou can!\u201d The longer answer is, \u201cYou can, but send() and recv() offer much greater control over your data transmission.\u201d<\/p>\n<p>What next? How about this: there are all kinds of sockets. There are <a href=\"https:\/\/en.wikipedia.org\/wiki\/DARPA\">DARPA<\/a> Internet addresses (Internet Sockets), path names on a local node (Unix Sockets), CCITT <a href=\"https:\/\/en.wikipedia.org\/wiki\/X.25\">X.25<\/a> addresses (X.25 Sockets that you can safely ignore), and probably many others depending on which Unix flavor you run. This document deals only with the first: Internet Sockets.<\/p>\n<p><b>2.1 Two Types of Internet Sockets\u00a0<\/b><\/p>\n<p>What\u2019s this? There are two types of Internet sockets? Yes. Well, no. I\u2019m lying. There are more, but I didn\u2019t want to scare you. I\u2019m only going to talk about two types here. Except for this sentence, where I\u2019m going to tell you that \u201cRaw Sockets\u201d are also very powerful and you should look them up.<\/p>\n<p>All right, already. What are the two types? One is \u201cStream Sockets\u201d; the other is \u201cDatagram Sockets\u201d, which may hereafter be referred to as \u201cSOCK_STREAM\u201d and \u201cSOCK_DGRAM\u201d, respectively. Datagram sockets are sometimes called \u201cconnectionless sockets\u201d. (Though they can be connect()\u2019d if you really want. See connect(), below.)<\/p>\n<p>Stream sockets are reliable two-way connected communication streams. If you output two items into the socket in the order \u201c1, 2\u201d, they will arrive in the order \u201c1, 2\u201d at the opposite end. They will also be error-free.<\/p>\n<p>I\u2019m so certain, in fact, they will be error-free, that I\u2019m just going to put my fingers in my ears and chant\u00a0<i>la la la la\u00a0<\/i>if anyone tries to claim otherwise.<\/p>\n<p>What uses stream sockets? Well, you may have heard of the telnet or ssh applications, yes? They use stream sockets. All the characters you type need to arrive in the same order you type them, right? Also, web browsers use the Hypertext Transfer Protocol (HTTP) which uses stream sockets to get pages. Indeed, if you telnet to a web site on port 80, and type \u201cGET \/ HTTP\/1.0\u201d and hit RETURN twice, it\u2019ll dump the HTML back at you!<\/p>\n<p><em>If you don\u2019t have <a href=\"https:\/\/en.wikipedia.org\/wiki\/Telnet\">telnet<\/a> installed and don\u2019t want to install it, or your telnet is being picky about connecting to clients, the guide comes with a telnet-like program called telnot1. This should work well for all the needs of the guide. (Note that telnet is actually a spec\u2019d networking protocol2, and telnot doesn\u2019t implement this protocol at all.)<\/em><\/p>\n<p>How do stream sockets achieve this high level of data transmission quality? They use a protocol called \u201cThe <a href=\"https:\/\/en.wikipedia.org\/wiki\/Transmission_Control_Protocol\">Transmission Control Protocol<\/a>\u201d, otherwise known as \u201cTCP\u201d (see RFC 7933 for extremely detailed info on TCP). TCP makes sure your data arrives sequentially and error-free. You may have heard \u201cTCP\u201d before as the better half of \u201cTCP\/IP\u201d where \u201cIP\u201d stands for \u201cInternet Protocol\u201d (see RFC 7914). IP deals primarily with Internet routing and is not generally responsible for data integrity.<\/p>\n<p>Cool. What about <a href=\"https:\/\/docs.oracle.com\/cd\/E23824_01\/html\/821-1602\/sockets-14.html\">Datagram sockets<\/a>? Why are they called connectionless? What is the deal, here, anyway? Why are they unreliable? Well, here are some facts: if you send a datagram, it may arrive. It may arrive out of order. If it arrives, the data within the packet will be error-free.<\/p>\n<p>Datagram sockets also use IP for routing, but they don\u2019t use TCP; they use the \u201c<a href=\"https:\/\/en.wikipedia.org\/wiki\/User_Datagram_Protocol\">User Datagram Protocol<\/a>\u201d, or \u201cUDP\u201d (see RFC 7685).<\/p>\n<p>Why are they connectionless? Well, basically, it\u2019s because you don\u2019t have to maintain an open connection as you do with stream sockets. You just build a packet, slap an IP header on it with destination information, and send it out. No connection needed. They are generally used either when a TCP stack is unavailable or when a few dropped packets here and there don\u2019t mean the end of the Universe. Sample applications: tftp (trivial file transfer protocol, a little brother to FTP), dhcpcd (a DHCP client), multiplayer games, streaming audio, video conferencing, etc.<\/p>\n<p>\u201cWait a minute! tftp and dhcpcd are used to transfer binary applications from one host to another! Data can\u2019t be lost if you expect the application to work when it arrives! What kind of dark magic is this?\u201d<\/p>\n<p>Well, my human friend, tftp and similar programs have their own protocol on top of UDP. For example, the tftp protocol says that for each packet that gets sent, the recipient has to send back a packet that says, \u201cI got it!\u201d (an \u201cACK\u201d packet). If the sender of the original packet gets no reply in, say, five seconds, he\u2019ll re-transmit the packet until he finally gets an ACK. This acknowledgment procedure is very important when implementing reliable SOCK_DGRAM applications.<\/p>\n<p>For unreliable applications like games, audio, or video, you just ignore the dropped packets, or perhaps try to cleverly compensate for them. (Quake players will know the manifestation this effect by the technical term:\u00a0<i>accursed lag<\/i>. The word \u201caccursed\u201d, in this case, represents any extremely profane utterance.)<\/p>\n<p>Why would you use an unreliable underlying protocol? Two reasons: speed and speed. It\u2019s way faster to fire-and-forget than it is to keep track of what has arrived safely and make sure it\u2019s in order and all that. If you\u2019re sending chat messages, TCP is great; if you\u2019re sending 40 positional updates per second of the players in the world, maybe it doesn\u2019t matter so much if one or two get dropped, and UDP is a good choice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p><iframe style=\"width: 100%; height: 750px; border: none;\" src=\"https:\/\/online.visual-paradigm.com\/share\/book\/beej-s-guide-to-network-programming-1b0ho4rodn?p=1\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n","protected":false},"featured_media":23372,"template":"","meta":{"_yoast_wpseo_title":"","_yoast_wpseo_metadesc":"Network programming involves developing software applications that communicate and exchange data over a computer network. Learn more here!"},"product_brand":[],"product_cat":[279],"product_tag":[],"class_list":{"0":"post-23368","1":"product","2":"type-product","3":"status-publish","4":"has-post-thumbnail","6":"product_cat-networking","8":"first","9":"instock","10":"shipping-taxable","11":"product-type-simple"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Beej&#039;s Guide to Network Programming - Using Internet Sockets - BooksOfAll Simplified Chinese<\/title>\n<meta name=\"description\" content=\"Network programming involves developing software applications that communicate and exchange data over a computer network. Learn more here!\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Beej&#039;s Guide to Network Programming - Using Internet Sockets - BooksOfAll Simplified Chinese\" \/>\n<meta property=\"og:description\" content=\"Network programming involves developing software applications that communicate and exchange data over a computer network. Learn more here!\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/\" \/>\n<meta property=\"og:site_name\" content=\"BooksOfAll Simplified Chinese\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:image\" content=\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg\" \/>\n<meta name=\"twitter:label1\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/\",\"url\":\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/\",\"name\":\"Beej's Guide to Network Programming - Using Internet Sockets - BooksOfAll Simplified Chinese\",\"isPartOf\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg\",\"datePublished\":\"2023-03-30T06:36:52+00:00\",\"description\":\"Network programming involves developing software applications that communicate and exchange data over a computer network. Learn more here!\",\"breadcrumb\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#primaryimage\",\"url\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg\",\"contentUrl\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg\",\"width\":\"827\",\"height\":\"1169\",\"caption\":\"Beej's Guide to Network Programming - Using Internet Sockets\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.booksofall.com\/cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Categories\",\"item\":\"https:\/\/www.booksofall.com\/cn\/categories\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Beej&#8217;s Guide to Network Programming &#8211; Using Internet Sockets\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/#website\",\"url\":\"https:\/\/www.booksofall.com\/cn\/\",\"name\":\"BooksOfAll Simplified Chinese\",\"description\":\"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.\",\"publisher\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.booksofall.com\/cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/#organization\",\"name\":\"BooksOfAll Simplified Chinese\",\"url\":\"https:\/\/www.booksofall.com\/cn\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.booksofall.com\/cn\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2022\/06\/booksofall-logo-2.png\",\"contentUrl\":\"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2022\/06\/booksofall-logo-2.png\",\"width\":166,\"height\":30,\"caption\":\"BooksOfAll Simplified Chinese\"},\"image\":{\"@id\":\"https:\/\/www.booksofall.com\/cn\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Beej's Guide to Network Programming - Using Internet Sockets - BooksOfAll Simplified Chinese","description":"Network programming involves developing software applications that communicate and exchange data over a computer network. Learn more here!","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/","og_locale":"zh_CN","og_type":"article","og_title":"Beej's Guide to Network Programming - Using Internet Sockets - BooksOfAll Simplified Chinese","og_description":"Network programming involves developing software applications that communicate and exchange data over a computer network. Learn more here!","og_url":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/","og_site_name":"BooksOfAll Simplified Chinese","og_image":[{"url":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_image":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg","twitter_misc":{"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/","url":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/","name":"Beej's Guide to Network Programming - Using Internet Sockets - BooksOfAll Simplified Chinese","isPartOf":{"@id":"https:\/\/www.booksofall.com\/cn\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#primaryimage"},"image":{"@id":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#primaryimage"},"thumbnailUrl":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg","datePublished":"2023-03-30T06:36:52+00:00","description":"Network programming involves developing software applications that communicate and exchange data over a computer network. Learn more here!","breadcrumb":{"@id":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/"]}]},{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#primaryimage","url":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg","contentUrl":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2023\/03\/Beejs-Guide-to-Network-Programming.jpg","width":"827","height":"1169","caption":"Beej's Guide to Network Programming - Using Internet Sockets"},{"@type":"BreadcrumbList","@id":"https:\/\/www.booksofall.com\/cn\/beejs-guide-to-network-programming-using-internet-sockets\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.booksofall.com\/cn\/"},{"@type":"ListItem","position":2,"name":"Categories","item":"https:\/\/www.booksofall.com\/cn\/categories\/"},{"@type":"ListItem","position":3,"name":"Beej&#8217;s Guide to Network Programming &#8211; Using Internet Sockets"}]},{"@type":"WebSite","@id":"https:\/\/www.booksofall.com\/cn\/#website","url":"https:\/\/www.booksofall.com\/cn\/","name":"BooksOfAll Simplified Chinese","description":"Biggest IT eBooks library and learning resources - Free eBooks for programming, computing, artificial intelligence and more.","publisher":{"@id":"https:\/\/www.booksofall.com\/cn\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.booksofall.com\/cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":"Organization","@id":"https:\/\/www.booksofall.com\/cn\/#organization","name":"BooksOfAll Simplified Chinese","url":"https:\/\/www.booksofall.com\/cn\/","logo":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.booksofall.com\/cn\/#\/schema\/logo\/image\/","url":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2022\/06\/booksofall-logo-2.png","contentUrl":"https:\/\/www.booksofall.com\/cn\/wp-content\/uploads\/sites\/2\/2022\/06\/booksofall-logo-2.png","width":166,"height":30,"caption":"BooksOfAll Simplified Chinese"},"image":{"@id":"https:\/\/www.booksofall.com\/cn\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product\/23368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product"}],"about":[{"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/types\/product"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/media\/23372"}],"wp:attachment":[{"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/media?parent=23368"}],"wp:term":[{"taxonomy":"product_brand","embeddable":true,"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product_brand?post=23368"},{"taxonomy":"product_cat","embeddable":true,"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product_cat?post=23368"},{"taxonomy":"product_tag","embeddable":true,"href":"https:\/\/www.booksofall.com\/cn\/wp-json\/wp\/v2\/product_tag?post=23368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}