CocoaAsyncSocket 文檔1:Socket簡介

前言


CocoaAsyncSocket是 IOS下廣泛應用的Socket三方庫,網上相關例子數不勝數,這裏我就不直接上代碼,本文由B9班的真高興發表於CSDN博客。另闢一條思路:翻譯SocketAsyncSocket的文檔
原文地址:https://github.com/robbiehanson/CocoaAsyncSocket/wiki/Intro

Introduction to Sockets


If you’re a beginner to networking, this is the place to start. Working with a socket can be very different from working with a file, even though the APIs may be similar. A little bit of investment in your knowledge and understanding of networking fundamentals can go a long way. And it can save you a lot of time and frustration in the long run.

We will keep it brief, and will maintain a focus on developers: just what developers need to accomplish their goal, while not skipping important fundamentals that could later cause problems.

介紹套接字


如果你是一個網絡方面的新手,這裏可以給你做一個入門。使用socket和文件操作有很大的不同,儘管他們的API可能非常相近。對網絡原理的知識和理解的一點投入可以在長久的時間內起作用。同時呢在一個很長的時間內可以節省你的時間避免你的挫折。

我們會保證它(介紹)儘量簡短,並且焦點主要集中在開發者身上:僅僅是開發者完成他們目標需要的東西,同時不跳過可能導致問題的重要步驟。

Sockets, Ports, and DNS - Oh My!


In networking parlance, a computer is a host for a number of sockets. A socket is one end of a communication channel called a network connection; the other end is another socket. From its own point of view, any socket is the local socket, and the socket at the other end of the connection is the remote socket.

To establish the connection, one of the two sockets must contact the other socket. To make contact the socket must know the other socket’s address. Every socket has an address. The address consists of two parts: the host address and the port number. The host address is the IP address of the computer, and the port number uniquely identifies each socket hosted on the computer.

A computer can have multiple host addresses because it can have multiple networking interfaces. For example, a computer might be equipped with an ethernet card, a modem, a WiFi card, a VPN connection, Bluetooth, etc. And in addition to all this, there is a special interface for connecting to itself (called “loopback” or sometimes “localhost”).

An address such as “google.com” corresponds to a host address, but it is not a host address itself. It is a DNS entry or DNS name, which is converted to a host address by a DNS look-up operation. One can think of DNS like a phone book. If you wanted to call someone, but didn’t know their number, you could lookup their number in the phone book. Their name is matched to a phone number. Similarly, DNS matches a name (such as “google.com”) to an IP address.

套接字,端口,DNS


在網絡術語中,計算機可以作爲多個套接字的主機。一個Socket是網絡連接的通信通道的一端;另一端是另一個Socket。從自己的角度看,任何一個Socket,都是本地Socket,鏈接另一端的Socket就可以看做遠程Socket

要建立連接,兩個Socket中必須有一個Socket主動去連接另一個Socket。爲了建立連接,Socket必須知道另一個Socket的地址。每個Socket都有一個地址。地址包括兩部分:主機地址和端口號。主機地址是計算機的IP地址,端口號唯一的標識了主機上的每個套接字。

一臺計算機可以有多個主機地址,因爲計算機可以有多個網絡接口。例如,計算機可能會配備一個以太網卡,調制解調器,一個WiFi卡,一個VPN連接,藍牙,等,除此之外,還有連接本身的特殊接口(稱爲“迴環”或“localhost”)。

一個地址像“Google.com”聯繫到一個主機地址,但是它本身不是一個主機地址。這是一個DNS條目或DNS名稱,它是由一個DNS查找操作轉換爲主機地址。人們可以把DNS想象成一個電話簿。如果你想給某人打電話,又不知道他的電話號碼,那麼你可以查閱電話薄來找到電話號碼。(電話簿中)他們的名字和電話號碼相對應。同樣,DNS匹配名稱(比如:Google.com)和IP地址。

Networking Huh?


The crux of the problem is that the network you’ll be communicating over is unreliable. Perhaps you’re sending data out over the Internet. Maybe it’s going to be sent via WiFi, or some cellular connection. Or maybe it’s going to be sent into space via a satellite. You might not even know.

But let’s assume for a moment that you did know. Let’s assume you knew that all communication was going to take place over regular ethernet, within a closed business network. The communication would be 100% reliable right? Wrong. And I’m not referring to cut wires or power outages either.

All data that gets sent or received gets broken into little packets. These packets then get pumped onto the network, and arrive at routers which have to decide where they go. But during bursts of traffic, a router might get overloaded with packets. Packets are coming in faster than the router can figure out where to route them. What happens? The same thing that happens millions of times a day all over the world: the router starts dropping packets.

In addition to lost packets on the network, the receiving computer might be forced to drop packets too. Perhaps the computer is overloaded, or the receiving application isn’t reading the data from the OS fast enough. There’s also the potential that the packet was corrupted during transmission, perhaps from electrical interference. And all of this is without getting into other issues introduced by things like the WiFi or the Internet.

If you’re new to networking, you might be thinking that it’s a miracle that everything works as well as it does. The fact is, the miracle is derived from the networking protocols that have been perfected over the last several decades, and from the developers that understand them and use them effectively. (That’s you!)

網絡 哈哈


關鍵問題是,你要使用溝通的網絡是不可靠的。也許你會通過Internet發送數據。也許會通過WiFi或者蜂窩網絡,或者通過衛星發到外太空,你甚至可能不知道。

讓我們假定那一刻你知道。讓我們假定那一刻所有的通信都發生在一個封閉的以太網。現在通信100%可靠麼?當然不是。這裏我指的不是剪斷電線或着中斷電力。

所有發送的數據都被打散成爲小的數據包。這些數據包會被注入網絡,到達路由器,路由器要決定這些數據包去哪裏?在這爆炸性的流量中,數據包可能會讓路由器過載。接收數據包的速度要超過路由器分發數據包的速度。會發生什麼呢?同樣的事情在全世界每天發生數百萬次:路由器開始丟包。除了在網絡上丟失的數據包,接收計算機可能會被迫棄數據包。也許計算機是超負荷的,或者接收的應用程序,從操作系統中讀取數據的速度不夠快。在傳輸過程中,數據包在傳輸過程中可能會損壞,可能是來自於電氣干擾。而這一切都是摒除了諸如WiFi或互聯網引入的問題。

如果你是個網絡方面的菜鳥,那麼你會認爲所有事情都正常運行簡直是一個奇蹟。事實上,奇蹟是來自於過去幾十年中不斷完善的網絡協議,和了解它們(網絡協議)並有效地使用它們的開發人員。(比如說你)

Bring on the Protocols


You can probably list dozens of protocols that have something to do with computer networking:

HTTP, FTP, XMPP, POP, IMAP, SMTP, DHCP, DNS, VoIP, SIP, RTP, RTCP, …

But every single one of these protocols is layered on top of another protocol that handles the networking for it. These lower level protocols handle the majority of the networking aspect so that the application layer protocol (those listed above) can focus on the application aspect.

The “application layer protocols” listed above are layered on top of a “transport layer protocol”. And of all the protocols listed above, there are only two transport layer protocols that are used: TCP and UDP.

這裏是協議


你可能會列出一打和網絡有關的協議:HTTP, FTP, XMPP, POP, IMAP, SMTP, DHCP, DNS, VoIP, SIP, RTP, RTCP, …

但是這些每一個協議都建立在一個爲它(這個協議)控制網絡的協議之上(每個協議都是上層協議唄)。底層協議解決了大部分網絡方面的事情,應用層協議(上面列出的)纔可以專注於應用層面。

上面列出的“應用層協議”建立在“傳輸層協議”之上。以上列出的所有協議,只有兩個傳輸層使用的協議:TCP和UDP。

UDP


The User Datagram Protocol (UDP) is the simpler of the two. You can only put a small amount of data into a UDP packet, and then you send it on its way. And then… that’s it. There is no guarantee that the message will arrive. And if you send multiple packets back-to-back, there is no guarantee that they will arrive in order. Seems pretty unreliable, no? But it’s weakness is also its strength. If you are sending time-sensitive data, such as audio in a VoIP call, then you don’t want your transport protocol wasting time retransmitting lost packets since the lost audio would arrive too late to be played anyway. In fact, streaming audio and video are some of the biggest uses for UDP.

UDP also has an advantage that it doesn’t require a “connection handshake”. Think about it like this: If you were sitting on a train, and you wanted to have a long conversation with the stranger next to you, you would probably start with an introduction. Something like, “Where are you heading? Oh yeah, I’m heading in that direction too. My name’s Robbie, what’s yours?” But if you just wanted to know what the time was, then you could skip the introduction. You wouldn’t be expected to tell the stranger your name. You could just say, “Excuse me, do you have the time?” To which the stranger could quickly respond, and you could both go back to doing whatever you were doing. This is why a protocol like DNS uses UDP. That way your computer can say, “Excuse me, what is the IP of google.com?” And the server can quickly respond.

UDP


用戶數據報協議(UDP)是兩個協議中簡單的。你可以把少量數據放進一個UDP包中,然後把它發送出去。然後……就這樣了。你不能保證數據會到達。如果你接連發出多個數據包,你不能保證它們按序到達。這樣看起來似乎很不靠譜,不是嗎?但是它的缺點同時也是它的優勢。如果您發送時間敏感數據,如VoIP呼叫的音頻,然後你不想讓你的傳輸協議浪費時間重發丟失的音頻幀,因爲丟失的音頻數據可能遠遠落後於播放進度。事實上,音頻流和視頻流大量的使用了UDP。

UDP也有優勢,它不需要一個“連接握手”。這樣想:如果你坐在火車上,想和鄰座的陌生人展開一次長交談,你可能會以一個自我介紹作爲開場。類似於,“你要去哪兒?”哦,太棒了,我也去那個方向。我的名字叫羅賓,你叫啥?但如果你只是想知道時間是什麼,那麼你可以跳過自我介紹。你不希望陌生人知道你的名字。你可以說:“打擾了,你知道幾點了嗎?”陌生人可以很快的回答你,然後你就可以做你想做的事去了。這就是爲什麼DNS使用UDP協議。這就像你的計算機計算機就可以說:“打擾,Google.com的IP是多少?”服務器可以很快的回答你。

TCP


The Transmission Control Protocol (TCP) is probably the protocol you use the most. Whether you’re browsing the web, checking your email, or sending instant messages to friends, you’re probably using TCP.

TCP is designed for “long conversations”. So there is an initial connection handshake, and after that data can flow back and forth for as long as necessary. But the great thing about TCP is that it was designed to make communication reliable in the face of an unreliable network. So it does all kinds of really cool stuff for us. If you send some information over TCP, and part of it gets lost, the protocol will automatically figure out what got lost and resend it. And when you send information, TCP makes sure that information always arrives in the correct order. But wait, there’s more! The protocol will also detect congestion in the network, and automatically scale accordingly so everybody can share.

So there are a lot of great reasons to use TCP, and it fits in nicely with a lot of networking tasks. Plus there is no limit to the amount of data you can send via TCP. It is designed to be an open stream of data flowing in both/either direction. It is simply up to the application layer to determine what that data looks like.

TCP


傳輸控制協議(TCP)可能是你最經常使用的協議。無論你是瀏覽網頁,查看郵件,或發短信給朋友,你都有可能會使用TCP。

TCP是專爲“長對話”設計的。因此,在初始的連接握手之後,只要有必要,就可以相互傳輸數據。但關於TCP的偉大的,是它設計爲在不可靠的網絡上實現可靠的通信。所以它對我們來說真的很酷。如果你把一些信息通過TCP發送,信息的一部分丟失了,協議會自動找出丟掉的數據並重新發送。當你發送信息,TCP確保信息總是以正確的順序到達。等等,不止這些!該協議還將檢測網絡中的擁塞,並自動縮放,從而使大家可以共享帶寬。

所以有很多偉大的原因使用TCP,它適合很多網絡任務。再加上可以通過發送TCP的數據沒有數量的限制(一種面向流的協議,沒有數據邊界)。它被設計成雙方向傳輸的數據流。它支持應用層簡單的決定數據的樣子。

Where do we fit in?


So… UDP and TCP… how do we use them? Is that what the CocoaAsyncSocket libraries provide? Implementations of TCP and UDP? Nope, not quite. As you can imagine, TCP and UDP are used all over the place. So naturally they are provided by the operating system. If you open up your terminal and type “man socket” you can see the low level BSD socket API. The libraries are essentially wrappers that sits on top of low-level socket API’s and provide you, the developer, an easy to use framework in Objective-C.

So CocoaAsyncSocket provides a great API that simplifies networking for you. But networking can still be tricky, so we recommend you read the following before you get started:

General Documentation
Common Pitfalls
Another invaluable resource is the CocoaAsyncSocket mailing list.

我們在哪用呢?


所以……TCP和UDP……我們應該怎麼使用呢?他們是CocoaAsyncSocket庫提供?實現的TCP和UDP?不,確實不是這樣。就像你想象的,TCP和UDP可以在所有的地方使用。因此,它們是由操作系統提供的。如果你打開終端輸入“man Socket”你可以看到底層的BSD Socket API。庫的本質上是封裝底層的API,爲您(開發者)提供一個易於使用的框架在Objective-C。

發佈了92 篇原創文章 · 獲贊 137 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章