青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

我的CPP之路

路漫漫其修遠兮
隨筆 - 42, 文章 - 0, 評論 - 16, 引用 - 0
數據加載中……

Les Notes d'Etude de Programmation TCP/IP

Ce sont les notes des livres à propos de Programmation TCP/IP.

=========
Everything in Unix is a file!


When Unix programs do any sort of I/O, they do it by reading or writing to a file descriptor.

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 to get this file descriptor for network communication?

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.

We can just use the normal read() and write() calls to communicate through the socket, but send() and recv() offer much greater control over your data transmission.

There are all kinds of sockets, DARPA Internet addresses (Internet Sockets), path names on a local node (Unix Sockets), CCITT X.25 addresses (X.25 Sockets that you can safely ignore), and so on.

“Raw Sockets” are also very powerful and we should look them up.

Two types of Internet sockets, “Stream Sockets” and “Datagram Sockets”, which may hereafter be referred to as “SOCK_STREAM” and “SOCK_DGRAM”, respectively.

Datagram sockets are sometimes called “connectionless sockets”. (Though they can be connect()'d if you really want. See connect(), below.)

Stream sockets are reliable two-way connected communication streams. 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 will also be error-free.

Indeed, if you telnet to a web site on port 80, and type “GET / HTTP/1.0” and hit RETURN twice, it'll dump the HTML back at you!

Stream sockets use a protocol called “The Transmission Control Protocol”, otherwise known as “TCP” (see RFC 7936 for extremely detailed info on TCP).

TCP makes sure your data arrives sequentially and error-free.

“IP” stands for “Internet Protocol” (see RFC 7917).

IP deals primarily with Internet routing and is not generally responsible for data integrity.

Datagram sockets are called connectionless. They are unreliable. If you send a datagram, it may arrive out of order. If it arrives, the data within the packet will be error-free.

Datagram sockets also use IP for routing. They use the “User Datagram Protocol”, or “UDP”. You don'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 destination information, and send it out. No connection needed.

Why would you use an unreliable underlying protocol? Two reasons: speed and speed. If you're sending chat messages, TCP is great; if you're sending 40 positional updates per second of the players in the world, maybe it doesn't matter so much if one or two get dropped, and UDP is a good choice.

A layered model more consistent with Unix might be:
• Application Layer (telnet, ftp, etc.)
• Host-to-Host Transport Layer (TCP, UDP)
• Internet Layer (IP and routing)
• Network Access Layer (Ethernet, wi-fi, or whatever)

All you have to do for stream sockets is send() the data out. All you have to do for datagram sockets is encapsulate the packet in the method of your choosing and sendto() it out. The kernel builds the Transport Layer and Internet Layer on for you and the hardware does the Network Access Layer.

The router strips the packet to the IP header, consults its routing table.

=========
The address ::1 is the loopback address. It always means “this machine I'm running on now”. In IPv4, the loopback address is 127.0.0.1.

To represent the IPv4 address 192.0.2.33 as an IPv6 address, you use the following notation: “::ffff:192.0.2.33”.

The network portion of the IP address is described by something called the netmask, which you bitwise-AND with the IP address to get the network number out of it. The netmask usually looks something like 255.255.255.0. (E.g. with that netmask, if your IP is 192.0.2.12, then your network is 192.0.2.12 AND 255.255.255.0 which gives 192.0.2.0.)

So you might have a netmask of, say 255.255.255.252, which is 30 bits of network, and 2 bits of host allowing for four hosts on the network. (Note that the netmask is ALWAYS a bunch of 1-bits followed by a bunch of 0-bits.)

But it's a bit unwieldy to use a big string of numbers like 255.192.0.0 as a netmask. You just put a slash after the IP address, and then follow that by the number of network bits in decimal. Like this: 192.0.2.12/30. Or, for IPv6, something like this: 2001:db8::/32 or 2001:db8:5413:4028::9db9/64.

The port number, it's a 16-bit number that's like the local address for the connection. Different services on the Internet have different well-known port numbers. You can see them all in the Big IANA Port List 12 or, if you're on a Unix box, in your /etc/services file. HTTP (the web) is port 80, telnet is port 23, SMTP is port 25, the game DOOM13  used port 666, etc. and so on. Ports under 1024 are often considered special, and usually require special OS privileges to use.

If you want to represent the two-byte hex number, say b34f, you'll store it in two sequential bytes b3 followed by 4f.This number, stored with the big end first, is called Big-Endian.

The storage method, by which b34f would be stored in memory as the sequential bytes 4f followed by b3, is called Little-Endian.

The more-sane Big-Endian is also called Network Byte Order because that's the order us network types like.

Your computer stores numbers in Host Byte Order. If it's an Intel 80x86, Host Byte Order is Little-Endian. If it's a Motorola 68k, Host Byte Order is Big-Endian. If it's a PowerPC, Host Byte Order is...well, it depends!

A lot of times when you're building packets or filling out data structures you'll need to make sure your two- and four-byte numbers are in Network Byte Order. But how can you do this if you don't know the native Host Byte Order? Good news! You just get to assume the Host Byte Order isn't right, and you always run the value through a function to set it to Network Byte Order. The function will do the magic conversion if it has to, and this way your code is portable to machines of differing endianness.

htons() host to network short
htonl() host to network long
ntohs() network to host short
ntohl() network to host long

Basically, you'll want to convert the numbers to Network Byte Order before they go out on the wire, and convert them to Host Byte Order as they come in off the wire.

=========
A socket descriptor is just a regular int.

struct addrinfo is one of the first things you'll call when making a connection.

You can force it to use IPv4 or IPv6 in the ai_family field, or leave it as AF_UNSPEC to use whatever. This is cool because your code can be IP version-agnostic.

Oftentimes, a call to getaddrinfo() to fill out your struct addrinfo for you is all you'll need.

To deal with struct sockaddr, programmers created a parallel structure: struct sockaddr_in (“in” for “Internet”) to be used with IPv4.

sin_zero (which is included to pad the structure to the length of a struct sockaddr) should be set to all zeros with the function memset().

The sin_port must be in Network Byte Order (by using htons()!)

inet_pton(), converts an IP address in numbers-and-dots notation into either a struct in_addr or a struct in6_addr. The old way of doing things used a function called inet_addr() or another function called inet_aton(); these are now obsolete and don't work with IPv6.

inet_ntop() (“ntop” means “network to presentation”—you can call it “network to printable” if that's easier to remember). The old way of doing things: the historical function to do this conversion was called inet_ntoa(). It's also obsolete and won't work with IPv6.

Often times, the firewall translates “internal” IP addresses to “external” (that everyone else in the world knows) IP addresses using a process called Network Address Translation, or NAT.

The details of which private network numbers are available for you to use are outlined in RFC 191815, but some common ones you'll see are 10.x.x.x and 192.168.x.x, where x is 0-255, generally. Less common is 172.y.x.x, where y goes between 16 and 31.

=========
The most correct thing to do is to use AF_INET in your struct sockaddr_in and PF_INET in your call to socket().

Another thing to watch out for when calling bind(): don't go underboard with your port numbers. All ports below 1024 are RESERVED (unless you're the superuser)! You can have any port number above that, right up to 65535 (provided they aren't already being used by another program.)

We need to call bind() before we call listen() so that the server is running on a specific port. (You have to be able to tell your buddies which port to connect to!)


posted on 2010-12-27 11:21 yanvenhom 閱讀(354) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲视频在线看| 欧美日韩免费一区| 欧美成年人网站| 久久久久久久久岛国免费| 亚洲综合欧美| 99精品国产99久久久久久福利| 国产综合av| 国模一区二区三区| 一区二区在线视频| 亚洲国产欧美国产综合一区| 在线观看成人小视频| 亚洲精品国精品久久99热| aaa亚洲精品一二三区| 亚洲视频在线观看免费| 香蕉成人啪国产精品视频综合网| 久久狠狠一本精品综合网| 免费成人美女女| 91久久精品美女| 亚洲精品欧美极品| 亚洲免费视频观看| 久久99伊人| 欧美久久影院| 国产自产高清不卡| 夜夜精品视频| 久久久噜噜噜久噜久久| 亚洲精品一区久久久久久| 午夜综合激情| 欧美日韩国产限制| 黄页网站一区| 亚洲欧美中文日韩在线| 欧美激情一区二区| 亚洲欧美日韩高清| 欧美第一黄色网| 国产亚洲精品久久久久婷婷瑜伽 | 亚洲欧美中文日韩在线| 久久久久欧美| 国产精品久久久久久久久果冻传媒| 国产欧美日韩在线观看| 一本一本久久a久久精品综合妖精| 久久精品一本久久99精品| 亚洲日本aⅴ片在线观看香蕉| 性伦欧美刺激片在线观看| 国产精品海角社区在线观看| 亚洲高清自拍| 国产亚洲精品资源在线26u| 亚洲精品久久久久久下一站| 欧美在线亚洲一区| 99re8这里有精品热视频免费| 欧美一级网站| 国产伦精品一区二区三区高清版| 一本一道久久综合狠狠老精东影业 | 久久丁香综合五月国产三级网站| 欧美日韩理论| 亚洲精品在线电影| 久久综合久色欧美综合狠狠| 亚洲影音一区| 国产精品xxxxx| 欧美日本在线视频| 欧美va天堂| 久久riav二区三区| 国产三区二区一区久久 | 欧美在线视频免费| 亚洲一区二区欧美日韩| 欧美日韩直播| 亚洲综合社区| 亚洲欧美成人精品| 国产人成一区二区三区影院| 亚洲欧美日韩精品久久奇米色影视 | 另类天堂视频在线观看| 久久久久久久久岛国免费| 国产视频自拍一区| 久久人人爽人人| 久久免费观看视频| 亚洲国产精品一区制服丝袜| 欧美激情1区2区3区| 欧美精品二区| 亚洲一区在线免费| 久久久亚洲综合| 久久国产成人| 亚洲大胆在线| 99热精品在线| 国产乱码精品一区二区三区av| 欧美一区二区视频网站| 欧美亚洲色图校园春色| 在线日韩精品视频| 一区二区免费在线视频| 国产精品亚洲第一区在线暖暖韩国| 欧美亚洲综合另类| 久久久久久久999精品视频| 日韩亚洲欧美成人| 亚洲一品av免费观看| 国产性色一区二区| 亚洲第一区在线| 国产精品h在线观看| 免费91麻豆精品国产自产在线观看| 欧美高清在线| 欧美一区二区三区在线视频 | 国产精品网红福利| 久久亚洲国产精品一区二区 | 欧美精品手机在线| 欧美一级艳片视频免费观看| 久热爱精品视频线路一| 亚洲一区二区三区四区五区黄 | 亚洲综合电影| 亚洲日韩中文字幕在线播放| 亚洲一区二区三区乱码aⅴ蜜桃女| 国产亚洲欧美另类中文| 久久九九热re6这里有精品| 欧美激情va永久在线播放| 欧美中文在线字幕| 美女免费视频一区| 国产婷婷一区二区| 欧美成人午夜| 国产精品系列在线| 亚洲三级影院| 国产在线精品二区| 日韩午夜免费视频| 在线观看视频一区二区欧美日韩| 亚洲欧洲一区二区天堂久久| 国产综合色产在线精品| 99pao成人国产永久免费视频| 精品1区2区3区4区| 午夜在线成人av| 亚洲免费一级电影| 欧美成人精品| 玖玖精品视频| 国产亚洲精品7777| 亚洲一区3d动漫同人无遮挡| 亚洲老板91色精品久久| 久久免费视频在线| 久久精品国产一区二区三区免费看 | 欧美激情免费在线| 国产午夜精品久久久| 一区二区欧美激情| 一本大道久久a久久精品综合| 欧美高清视频一区二区三区在线观看 | 麻豆成人综合网| 国产专区欧美精品| 久久精品国产久精国产爱| 久久国产精品免费一区| 国产欧美日韩一级| 欧美怡红院视频一区二区三区| 欧美一区二区三区精品电影| 欧美性猛交xxxx乱大交退制版| 亚洲日本视频| 在线视频欧美日韩精品| 欧美日韩在线播放三区四区| 夜夜爽夜夜爽精品视频| 亚洲一区视频在线| 国产日韩精品一区观看| 欧美一区=区| 毛片一区二区| 亚洲精品123区| 欧美久久久久久久久| 一区二区三区四区国产| 久久精品国产亚洲aⅴ| 国产一区自拍视频| 老色批av在线精品| 一本色道久久99精品综合| 午夜精品久久久久久久99樱桃| 国产精品手机视频| 久久看片网站| 一本色道久久综合狠狠躁篇的优点| 校园春色国产精品| 精品99一区二区三区| 麻豆久久婷婷| 亚洲精品小视频在线观看| 欧美日韩美女| 久久国产精品久久久久久电车| 免费看成人av| 亚洲男人影院| 伊人一区二区三区久久精品| 欧美激情成人在线| 亚洲欧美日韩爽爽影院| 欧美成人有码| 欧美综合国产精品久久丁香| 亚洲欧洲综合另类| 国产色视频一区| 久久女同精品一区二区| 亚洲一级在线| 亚洲欧洲精品一区二区三区波多野1战4| 亚洲性图久久| 狠狠久久综合婷婷不卡| 欧美日韩一区二区三区视频| 久久久青草青青国产亚洲免观| 亚洲精品国产视频| 卡通动漫国产精品| 午夜在线精品| 亚洲视频第一页| 亚洲高清免费视频| 国产日韩精品入口| 欧美日韩在线视频首页| 久久久久国产精品www| 亚洲女同精品视频| 亚洲精品国产精品国自产在线 | 亚洲欧美色一区| 99精品久久久| 亚洲国产欧美一区二区三区久久 | 免播放器亚洲| 久久人人97超碰人人澡爱香蕉|