前言


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。

CocoaAsyncSocket 文档1:Socket简单介绍的更多相关文章

  1. Winform开发框架中的内容及文档管理模块功能介绍

    在开发项目的时候,我们有一些场景需要编辑一些HTML文档,作为内容发布系统的一部分,有时候也需要对一些文档如WORD文档进行编辑管理,这样需要我们对这些内容及文档进行合适的管理.本文主要介绍在WInf ...

  2. java基于feemarker 生成word文档(超级简单)

    问题由来: 开发个新需求,需要按规定导出word文档,文档截图如下 因为之前没做过这个,一脸懵B啊,导出excel和txt倒是经常接触到,对于这个word这种格式不严谨的文件怎么处理呢? 技术选型:可 ...

  3. 第三十章、containers容器类部件QMdiArea多文档界面部件功能介绍及开发应用

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 一.引言 老猿在前期学习PyQt相关知识时,对每个组件的属性及方法都研 ...

  4. Asp.net操作Word文档,原来这么简单啊!

    引用Word对象库文件  具体做法是打开菜单栏中的项目>添加引用>浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,vs.net将会自动将库文件转化为 ...

  5. #爬虫必备,解析html文档----beautifulsoup的简单用法

    #出处:http://mp.weixin.qq.com/s?__biz=MjM5NzU0MzU0Nw==&mid=201820961&idx=2&sn=b729466f334d ...

  6. 有效的XML: DTD(文档类型定义)介绍(转)

    文档类型定义和命名空间 有效(Valid)的XML文档: 首先,XML文档是个格式正规的(Well-formed)XML文档:(见格式正规的XML:语法 属性 实体 处理指令 样式单 CDATA节). ...

  7. 阅读MDN文档之CSS选择器介绍(一)

    本文为阅读MDN文档笔记 目录 Different types of Selectors Attribute Selectors Presence and value attribute select ...

  8. MongoDB系列一:MongoDB文档型数据库特点介绍

    一.MongoDB数据库的存储特点: 1.MongoDB是文档型数据库,存储的是文档(将json二进制化为Bson存储): 2.存储的每篇文档都可以有自己独特的存储结构,颠覆了传统数据库必须按照表和列 ...

  9. 第三篇——第二部分——第一文 SQL Server镜像简单介绍

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/DBA_Huangzj/article/details/26951563 原文出处:http://bl ...

随机推荐

  1. 觉醒力量 (hidpower)

    觉醒力量 (hidpower) 题目描述 [题目背景] 从前有一款非常火的游戏被人们称为pokemon,从最初的红绿蓝黄版直到现在的XY版,都受到世界各地小朋友和大朋友们的喜爱. [题意描述] 作为一 ...

  2. windows 批处理删除指定目录下 指定类型 指定天数之前文件

    删除D:\test下5天前所有文件,如下: @echo offset SrcDir=D:\testset DaysAgo=5forfiles /p %SrcDir% /s /m *.* /d -%Da ...

  3. Databus架构分析与初步实践

    简介 Databus是一个低延迟.可靠的.支持事务的.保持一致性的数据变更抓取系统.由LinkedIn于2013年开源.Databus通过挖掘数据库日志的方式,将数据库变更实时.可靠的从数据库拉取出来 ...

  4. JS允许找不到对象 不必判断是否存在某个div

    原文发布时间为:2009-11-10 -- 来源于本人的百度文章 [由搬家工具导入] 加一个try catch 即可。。。。 例子: for(var i=0;i<10;i++)     {    ...

  5. c#中类与结构的区别 struct与class的区别

    原文发布时间为:2008-11-23 -- 来源于本人的百度文章 [由搬家工具导入] 类与结构的实例比较   类与结构的差别   如何选择结构还是类   一.类与结构的示例比较:   结构示例:    ...

  6. RecyclerView的Item和Item内的控件点击处理

    需求场景:RecyclerView的Item需要点击,或者Item中的某个控件需要点击,或者两者同时需要点击处理. 一.adapter代码如下: package com.ldw.adapter; im ...

  7. HDU 1060 Leftmost Digit【log10/求N^N的最高位数字是多少】

    Leftmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. Axure基础操作

    一  简介 Axure RP就是一个快速原型(Rapid Prototyping)设计工具. 快速原型(快速模型demo)在真正的开发之前,构造一个原型.把需求模块进行落实.实现部分交互. 二  Ax ...

  9. 10.1综合强化刷题 Day3

    括号序列(bracket) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一个括号序列,但这个序列不一定合法. 一个合法的括号序列如下: ()是合法的 ...

  10. centos7下使用wget命令安装mysql

    1.首先安装wget命令: yum -y install  wget 2.下载mysql wget http://repo.mysql.com/mysql-community-release-el7- ...