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

我的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 閱讀(354) 評論(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>
            亚洲欧美中文字幕| 在线播放豆国产99亚洲| 亚洲一区二区三区三| 欧美激情影院| 欧美v日韩v国产v| 麻豆久久婷婷| 亚洲电影免费观看高清完整版在线观看| 亚洲欧美高清| 先锋亚洲精品| 久久夜精品va视频免费观看| 这里只有精品电影| 亚洲一区二区三区在线播放| 亚洲欧美日韩专区| 久久aⅴ国产紧身牛仔裤| 久久久久久婷| 欧美高清不卡| 99热精品在线观看| 欧美在线影院| 免费不卡欧美自拍视频| 欧美精品七区| 国产欧美一区二区三区在线看蜜臀| 国产偷久久久精品专区| 亚洲福利视频免费观看| 亚洲无线视频| 老司机久久99久久精品播放免费 | 亚洲国产精品成人| 一区二区欧美精品| 久久电影一区| 亚洲国产一区二区a毛片| 亚洲色图在线视频| 久久网站热最新地址| 久久在线精品| 亚洲欧美日韩国产中文| 亚洲欧美一区二区三区在线| 久久狠狠亚洲综合| 欧美在线www| 久久手机精品视频| 亚洲国产精品美女| 在线亚洲电影| 久久人人精品| 欧美理论大片| 99精品视频一区二区三区| 亚洲伊人一本大道中文字幕| 蜜桃久久精品一区二区| 国产精品免费小视频| 最新成人av网站| 久久精品三级| 一本色道久久88综合日韩精品| 久久久综合网站| 国产精品一区二区三区久久久| 亚洲精品久久久一区二区三区| 久久久www| 麻豆精品精华液| 一本色道久久综合亚洲精品婷婷| 久久综合九色欧美综合狠狠| 国产女精品视频网站免费 | 欧美黄污视频| 久久丁香综合五月国产三级网站| 欧美日韩国产专区| 91久久中文| 亚洲国产欧美在线| 欧美激情91| 亚洲精品久久久久久久久| 欧美电影免费观看网站| 久久精品国产免费观看| 黄色小说综合网站| 欧美大胆成人| 久久综合久久美利坚合众国| 国内精品视频一区| 欧美另类69精品久久久久9999| 在线观看亚洲a| 欧美成人免费在线| 久久天天躁夜夜躁狠狠躁2022| 国产一区在线播放| 免费一区视频| 欧美大片网址| 国产精品99久久久久久宅男| 国产精品99久久久久久久久| 国产精品豆花视频| 亚洲午夜久久久久久久久电影院 | 久久人人爽人人爽| 狠狠爱www人成狠狠爱综合网| 老司机精品视频网站| 欧美一区二区三区精品电影| 久久精品中文字幕免费mv| 国产亚洲欧美日韩一区二区| av不卡免费看| 亚洲午夜视频在线| 久久人人爽国产| 日韩午夜在线电影| 亚洲性图久久| 亚洲三级影院| 欧美亚州韩日在线看免费版国语版| 99精品久久免费看蜜臀剧情介绍| 在线视频一区二区| 国产精品视频导航| 欧美a一区二区| 蜜臀久久久99精品久久久久久| 亚洲一区中文字幕在线观看| 午夜国产不卡在线观看视频| 亚洲精品五月天| 亚洲国产精品成人va在线观看| 欧美日韩一区在线观看视频| 欧美一级艳片视频免费观看| 久久国产一区二区| 尤物在线精品| 小辣椒精品导航| 亚洲国产美女久久久久| 一区二区欧美在线观看| 欧美三区在线观看| 午夜精品视频在线观看一区二区 | 亚洲欧美日韩天堂| 麻豆91精品| 亚洲午夜久久久久久久久电影网| 欧美一区二粉嫩精品国产一线天| 有码中文亚洲精品| 亚洲视频免费看| 国产一区二区三区高清播放| 午夜精品久久久久久久99黑人 | 欧美日韩国产影片| 欧美一区二区高清| 久久久噜噜噜久久中文字免| 亚洲第一网站免费视频| 国产精品电影网站| 欧美成人精品在线播放| 国产精品video| 六十路精品视频| 国产精品分类| 欧美一乱一性一交一视频| 欧美日韩高清区| 久久久午夜视频| 欧美日韩国产美女| 国产精品视频精品视频| 亚洲激情电影在线| 国产伦精品一区二区三区免费迷| 亚洲国产综合在线| 国产亚洲免费的视频看| 亚洲乱码日产精品bd| 在线观看日韩专区| 亚洲麻豆国产自偷在线| 亚洲韩国青草视频| 久久精品最新地址| 亚洲男人av电影| 欧美激情麻豆| 亚洲男女自偷自拍| 在线亚洲欧美专区二区| 午夜激情久久久| 一区二区三区**美女毛片| 久久综合成人精品亚洲另类欧美| 国产亚洲午夜| 午夜精品久久久久久久99樱桃| 久久亚洲欧美国产精品乐播| 国产精品va在线播放| 亚洲人成亚洲人成在线观看| 亚洲福利电影| 久久精品国产成人| 亚洲欧美成人一区二区在线电影| 欧美第一黄色网| 美女视频黄a大片欧美| 国产精品日韩电影| 欧美寡妇偷汉性猛交| 亚洲精品中文字| 欧美国产日韩二区| 久久久精品一区| 黄色日韩网站视频| 亚洲一区精品电影| 欧美性淫爽ww久久久久无| 亚洲精品一区二区网址 | 亚洲一区二区高清| 久久久久久电影| 国内一区二区在线视频观看| 亚洲另类一区二区| 亚洲欧美日韩成人高清在线一区| 欧美日韩在线精品| 亚洲视频欧美在线| 亚洲国产婷婷香蕉久久久久久99 | 久久久国产成人精品| 国产日韩精品一区二区三区| 亚洲欧美日韩国产一区| 久久精品国产清高在天天线| 国产亚洲精品久久久久久| 欧美在线视频观看免费网站| 麻豆九一精品爱看视频在线观看免费| 亚洲国产色一区| 欧美成人一品| 亚洲影院色无极综合| 亚洲第一页中文字幕| 亚洲一区三区在线观看| 国产亚洲精品aa| 欧美jizz19性欧美| 一区二区三区欧美| av不卡在线观看| 亚洲国内自拍| 国产精品久久久久久久免费软件| 午夜精品一区二区三区在线| 亚洲第一页在线| 亚洲影院色在线观看免费| 亚洲欧洲一区二区在线播放| 国产精品毛片a∨一区二区三区|国 | 免费在线国产精品|