zl程序教程

您现在的位置是:首页 >  其他

当前栏目

CocoaAsyncSocket 文档1:Socket简单介绍

文档 简单 介绍 socket
2023-09-11 14:14:42 时间

前言


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。