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

我的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 閱讀(356) 評論(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>
            亚洲激情电影在线| 亚洲日本在线视频观看| 欧美一级淫片播放口| 国产毛片精品视频| 久久久在线视频| 免播放器亚洲| 一区二区三区欧美在线| 一级成人国产| 国产亚洲在线观看| 欧美国产激情| 国产精品毛片大码女人| 久久av一区二区三区亚洲| 久久九九热re6这里有精品| 亚洲国产精品一区二区第四页av| 亚洲第一黄色| 国产精品家庭影院| 快播亚洲色图| 欧美精品自拍| 久久av一区| 欧美福利视频一区| 午夜国产一区| 欧美刺激午夜性久久久久久久| 亚洲一区二区久久| 久久免费高清| 午夜精品视频在线观看| 久久综合久久综合久久综合| 亚洲视频专区在线| 久久在线播放| 性久久久久久久久久久久| 老色鬼精品视频在线观看播放| 宅男噜噜噜66国产日韩在线观看| 欧美亚洲专区| 亚洲欧美国产日韩中文字幕| 久久露脸国产精品| 午夜伦欧美伦电影理论片| 欧美国产日韩一区| 免费成人高清视频| 国产精品日本一区二区| 亚洲国产精品久久久久婷婷老年 | 亚洲第一视频| 国产精品一区二区三区成人| 亚洲激情在线| 精品动漫3d一区二区三区| 亚洲午夜成aⅴ人片| 亚洲美女视频在线观看| 久久在线免费| 久久夜色精品国产亚洲aⅴ| 国产精品国产三级国产专区53| 亚洲缚视频在线观看| 精品999成人| 久久av最新网址| 久久精品一本| 国产女主播视频一区二区| 99视频精品全部免费在线| 亚洲欧洲在线一区| 欧美大片一区二区| 欧美黄在线观看| 亚洲国产成人午夜在线一区 | 亚洲国产日韩欧美一区二区三区| 国产一区二区三区四区hd| 午夜久久久久| 久久国产精品黑丝| 国产欧美日韩一区二区三区| 亚洲一区免费在线观看| 亚洲天堂成人| 国产精品久久久久久久电影| 夜久久久久久| 亚洲欧美日韩国产中文| 国产精品人人做人人爽人人添 | 乱人伦精品视频在线观看| 国产午夜亚洲精品羞羞网站| 亚洲欧美一区在线| 久久久久久久一区二区| 好吊视频一区二区三区四区| 久久久久久91香蕉国产| 欧美成人一区二免费视频软件| 91久久久久久| 欧美日韩在线播放三区四区| 亚洲性感激情| 久久久综合网| 亚洲激情一区二区| 欧美日韩午夜剧场| 亚洲欧美综合精品久久成人| 久久综合给合| 一本久久综合亚洲鲁鲁五月天 | 久久精品99国产精品| 噜噜噜噜噜久久久久久91| 亚洲人成人一区二区在线观看| 欧美精品在线观看| 亚洲深夜福利视频| 农村妇女精品| 亚洲男人av电影| 伊人成综合网伊人222| 欧美乱在线观看| 性感少妇一区| 91久久香蕉国产日韩欧美9色| 午夜精品视频在线观看一区二区| 国产综合久久久久久| 欧美伦理在线观看| 欧美一区网站| 亚洲精选一区二区| 久久偷看各类wc女厕嘘嘘偷窃| 日韩网站在线| 狠狠色狠狠色综合系列| 欧美三级小说| 久久婷婷一区| 午夜天堂精品久久久久| 最新成人av在线| 麻豆国产精品一区二区三区| 亚洲一区二区三区午夜| 在线精品国产成人综合| 国产精品久久综合| 欧美a级片网站| 欧美一区二区三区久久精品| 国产午夜精品久久久久久久| 久久精品麻豆| 亚洲欧美日韩精品在线| 99国产精品久久久久久久成人热| 久久久免费精品| 亚洲欧美久久| 一本久久青青| 亚洲久久在线| 亚洲高清免费在线| 国产在线不卡视频| 国产精品久久久久影院亚瑟| 欧美大色视频| 老司机精品视频网站| 久久大逼视频| 欧美一区二区三区免费在线看| 亚洲另类一区二区| 亚洲国产视频一区| 欧美成人69av| 免费亚洲电影| 欧美国产丝袜视频| 欧美成人免费网站| 欧美freesex交免费视频| 久久久一区二区| 久久久五月婷婷| 久久婷婷影院| 牛人盗摄一区二区三区视频| 蜜臀va亚洲va欧美va天堂| 久久久之久亚州精品露出| 久久久久国产精品一区| 久久激情一区| 久久久蜜臀国产一区二区| 久久久久www| 蜜桃av噜噜一区| 欧美黑人国产人伦爽爽爽| 欧美福利小视频| 亚洲激情自拍| 一区二区三区视频观看| 亚洲资源av| 久久成人精品电影| 美女视频黄 久久| 欧美日韩国产区一| 国产精品日日摸夜夜添夜夜av| 国产精品自拍一区| 精品动漫3d一区二区三区| 亚洲国产成人精品久久| 日韩视频免费观看高清在线视频| 国产精品99久久久久久久久久久久| 亚洲一区二区伦理| 久久国产精品久久精品国产| 久久在线免费观看视频| 亚洲大片av| 一区二区三区视频观看| 欧美在线关看| 欧美激情综合五月色丁香| 国产精品久久久久99| 极品日韩av| 亚洲图片在线| 久久人人爽爽爽人久久久| 欧美国产日韩亚洲一区| 一区二区三区黄色| 久久久国产精品一区二区三区| 你懂的一区二区| 国产精品久久夜| 亚洲激情自拍| 欧美中文字幕不卡| 亚洲精品麻豆| 久久久久久久网| 欧美特黄视频| 亚洲精品国精品久久99热一| 亚洲欧美区自拍先锋| 欧美h视频在线| 亚洲欧美卡通另类91av| 欧美.日韩.国产.一区.二区| 国产欧美二区| 夜夜嗨av一区二区三区中文字幕| 久久久久亚洲综合| 亚洲一区二区三区免费视频 | 日韩视频免费在线| 久久久国产精彩视频美女艺术照福利| 欧美日韩亚洲精品内裤| 亚洲福利视频一区| 久久欧美肥婆一二区| 亚洲天堂成人| 欧美视频在线一区| 99re66热这里只有精品3直播| 另类激情亚洲|