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

我的CPP之路

路漫漫其修遠(yuǎn)兮
隨筆 - 42, 文章 - 0, 評論 - 16, 引用 - 0
數(shù)據(jù)加載中……

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 閱讀(356) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發(fā)表評論。
網(wǎng)站導(dǎo)航: 博客園   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>
            日韩写真在线| 午夜精品短视频| 免费欧美电影| 欧美大片91| 夜夜嗨av一区二区三区| 一区二区黄色| 国产午夜精品一区二区三区欧美 | 久久久精品国产一区二区三区 | 久久久久久久久久久成人| 国产亚洲成av人在线观看导航| 久久精品99国产精品日本| 久久精品国产亚洲aⅴ| 樱桃视频在线观看一区| 亚洲二区免费| 国产精品高清在线| 久久精品最新地址| 欧美国产日韩一区二区| 亚洲视频视频在线| 欧美在线视频免费| 亚洲精品美女免费| 亚洲专区在线| 亚洲激情一区| 亚洲在线网站| 亚洲国产成人精品女人久久久| 亚洲欧洲日韩女同| 国产亚洲综合精品| 亚洲免费av电影| 国产亚洲福利社区一区| 最新亚洲视频| 红桃视频成人| 亚洲视频精选在线| 亚洲精品一区二区三区在线观看 | 欧美大片va欧美在线播放| 欧美日韩另类一区| 美女免费视频一区| 国产精品久久久爽爽爽麻豆色哟哟| 久久综合伊人77777蜜臀| 欧美久久99| 欧美成ee人免费视频| 国产欧美大片| av成人老司机| 亚洲精品在线观| 久久亚洲不卡| 久久黄色影院| 国产精品一区二区三区成人| 亚洲国产精品一区二区久 | 欧美日韩第一区| 免费视频一区| 国内揄拍国内精品久久| 中国亚洲黄色| 在线亚洲+欧美+日本专区| 美玉足脚交一区二区三区图片| 久久gogo国模啪啪人体图| 欧美日韩国产小视频| 欧美激情一区二区三区四区| 一区二区三区在线视频播放| 亚洲一区在线看| 亚洲欧美国产高清| 欧美日韩在线播放一区| 91久久精品美女高潮| 在线播放日韩专区| 久久久www成人免费毛片麻豆| 欧美中文字幕在线播放| 国产精品一区二区在线观看网站| 99国内精品久久| 在线视频日韩精品| 欧美日韩另类一区| 中文日韩欧美| 亚洲欧美日韩国产一区二区三区| 欧美天天影院| 亚洲综合成人在线| 久久精品国产一区二区三区免费看| 国产精品久久久久毛片软件 | 亚洲午夜日本在线观看| 亚洲色图综合久久| 国产精品久久久久999| 亚洲综合久久久久| 欧美一区中文字幕| 国产综合自拍| 美日韩精品视频| 亚洲裸体俱乐部裸体舞表演av| 一区二区欧美精品| 国产精品九九久久久久久久| 午夜精品久久久久久久99水蜜桃| 久久久久久国产精品mv| **性色生活片久久毛片| 欧美福利视频在线观看| 一区二区欧美激情| 久久精品毛片| 亚洲精品日本| 国产精品看片你懂得| 久久精品视频免费播放| 亚洲国产婷婷香蕉久久久久久| 一本一本a久久| 国产日韩久久| 欧美精品xxxxbbbb| 亚洲欧美精品一区| 欧美成人精品一区二区| 亚洲一区二区三区精品在线| 国产一区二区三区久久久久久久久| 久久久久网址| 在线亚洲自拍| 欧美国产日产韩国视频| 亚洲欧美国产一区二区三区| 国产婷婷色一区二区三区| 欧美国产成人在线| 小处雏高清一区二区三区| 欧美成人资源| 欧美一区二区高清| 亚洲乱码一区二区| 国模 一区 二区 三区| 欧美日韩不卡视频| 久久久久久久国产| 夜夜爽av福利精品导航 | 久久免费视频在线| 亚洲一级黄色片| 亚洲国产婷婷香蕉久久久久久99| 国产精品久久久久一区二区三区共 | 小黄鸭精品密入口导航| 亚洲人精品午夜| 激情欧美一区| 国产三区精品| 国产精品普通话对白| 欧美另类一区二区三区| 久久久免费精品视频| 亚洲欧美日本视频在线观看| 亚洲精品久久久蜜桃| 蜜臀久久99精品久久久画质超高清| 亚洲永久免费精品| 一区二区国产在线观看| 亚洲激情中文1区| 亚洲电影在线播放| 激情欧美一区二区| 国产亚洲一区二区三区在线观看| 国产精品红桃| 国产精品美女| 国产精品久久久久久久久久免费看 | 国产日韩欧美中文| 国产精品主播| 国产精品一级| 国产欧美视频一区二区| 国产精品欧美风情| 国产精品卡一卡二| 国产美女精品免费电影| 国产欧美日韩免费| 国产精品综合久久久| 国产欧美一区二区精品秋霞影院| 国产精品美女主播| 国产精品午夜av在线| 亚洲高清二区| 亚洲国产小视频在线观看| 欧美与欧洲交xxxx免费观看| 一区二区欧美亚洲| 亚洲中字在线| 亚洲综合二区| 久久国产视频网| 免费一级欧美片在线播放| 亚洲高清视频中文字幕| 亚洲高清视频在线| 欧美在线视频一区二区三区| 欧美日韩国产综合在线| 好吊妞这里只有精品| 亚洲自拍高清| 亚洲国产精品一区二区第一页 | 久久综合伊人77777麻豆| 国产精品va在线播放我和闺蜜| 亚洲国产成人在线| 久久国产免费| 亚洲一区二区三区高清| 欧美乱妇高清无乱码| 在线日韩视频| 欧美尤物巨大精品爽| 99国内精品久久久久久久软件| 米奇777超碰欧美日韩亚洲| 国外成人性视频| 欧美一区二区视频在线观看2020| 亚洲最新视频在线播放| 欧美绝品在线观看成人午夜影视 | 久久精品亚洲| 亚洲一区二区三区国产| 欧美色播在线播放| 日韩午夜在线电影| 亚洲国产成人午夜在线一区 | 久久精品综合网| 国产亚洲精品久久久久婷婷瑜伽| 销魂美女一区二区三区视频在线| 夜夜躁日日躁狠狠久久88av| 欧美巨乳波霸| 亚洲色图综合久久| 99av国产精品欲麻豆| 欧美激情麻豆| 日韩一区二区精品葵司在线| 欧美韩日一区二区三区| 久久综合给合| 亚洲精品久久久久久久久久久久 | 亚洲精品小视频| 欧美剧在线观看| 亚洲在线成人| 欧美一级播放| 亚洲第一区在线观看|