iPhone Native系统例程(数据报套接字类型)

发布于 2024-11-11 02:13:32 字数 984 浏览 0 评论 0原文

套接字是全双工通信 进程之间的通道 位于同一主机本地或 其中一个进程位于远程主机上。 与数据进入的管道不同 仅限一个方向,插座允许 处理发送和接收 数据。 NSFileHandle 有助于 通过流类型套接字进行通信 通过提供运行在 接受套接字的后台线程 连接并从套接字读取。

NSFileHandle 目前仅处理 通过流式通信 插座。如果你想使用数据报 或其他类型的套接字,您必须 使用创建和管理连接 本机系统例程。

一端的进程 通信通道(服务器) 首先创建并准备一个 使用系统例程的套接字。这些 BSD 和 BSD 之间的例程略有不同 非 BSD 系统,但包含 相同的步骤顺序:

  1. 创建一个流类型套接字 特定协议。

  2. 将名称绑定到套接字。

  3. 将自己添加为观察者 NSFileHandleConnectionAcceptedNotification。

  4. 发送acceptConnectionInBackgroundAndNotify 到此文件句柄对象。

    该方法接受连接 背景,创建一个新的 NSFileHandle 来自新套接字描述符的对象, 并发布 NSFileHandleConnectionAcceptedNotification。

现在我看到迈克尔回答了。 关于“stream”之间的区别-type”套接字和“datagram”套接字类型

您有本机系统例程(datagram-socket-type)的 iPhone 实现示例吗?

Sockets are full-duplex communication
channels between processes either
local to the same host machine or
where one process is on a remote host.
Unlike pipes, in which data goes in
one direction only, sockets allow
processes both to send and receive
data. NSFileHandle facilitates
communication over stream-type sockets
by providing mechanisms run in
background threads that accept socket
connections and read from sockets.

NSFileHandle currently handles only
communication through stream-type
sockets. If you want to use datagrams
or other types of sockets, you must
create and manage the connection using
native system routines.

The process on one end of the
communication channel (the server)
starts by creating and preparing a
socket using system routines. These
routines vary slightly between BSD and
non-BSD systems, but consist of the
same sequence of steps:

  1. Create a stream-type socket of a
    certain protocol.

  2. Bind a name to the socket.

  3. Adding itself as an observer of
    NSFileHandleConnectionAcceptedNotification.

  4. Sending acceptConnectionInBackgroundAndNotify
    to this file handle object.

    This method accepts the connection in the
    background, creates a new NSFileHandle
    object from the new socket descriptor,
    and posts an NSFileHandleConnectionAcceptedNotification.

Now I saw Michael answer .
About the differences between “stream-type” socket and a “datagram” socket type

Do you have iPhone implementation example for native system routines(datagram-socket-type)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2024-11-18 02:13:32

好的,首先我找到了我需要的东西,CFSocket API 允许我实现 UDP 同步。

CFSocket API

套接字是最基本的网络通信级别。插座的作用与电话插孔类似。它允许您连接到另一个套接字(本地或通过网络)并向该套接字发送数据。

最常见的套接字抽象是 BSD 套接字。 CFSocket 是 BSD 套接字的抽象。 CFSocket 只需很少的开销就提供了 BSD 套接字的几乎所有功能,并将套接字集成到运行循环中。 CFSocket 不限于基于流的套接字(例如 TCP),它可以处理任何类型的套接字。

您可以使用 CFSocketCreate 函数从头开始创建 CFSocket 对象,或者使用 CFSocketCreateWithNative 函数从 BSD 套接字创建一个 CFSocket 对象。然后,您可以使用函数 CFSocketCreateRunLoopSource 创建一个运行循环源,并使用函数 CFRunLoopAddSource 将其添加到运行循环中。这将允许您的 CFSocket 回调函数在 CFSocket 对象收到消息时运行。

不管怎样,我找到了 AsyncSocket API。

CocoaAsyncSocket 支持 TCP 和 UDP。 AsyncSocket 类用于 TCP,AsyncUdpSocket 类用于 UDP。每个类的描述如下。

AsyncSocket 是一个封装了 CFSocket 和 CFStream 的 TCP/IP 套接字网络库。它提供异步操作和带有委托支持的本机可可类。以下是主要功能:

排队的非阻塞读取和写入,具有可选的超时。您告诉它要读或写什么,完成后它会打电话给您。
自动套接字接受。如果你告诉它接受连接,它会为每个连接调用你自己的新实例。当然,您可以立即断开它们。
代表支持。错误、连接、接受、读取完成、写入完成、进度和断开连接都会导致对委托方法的调用。
基于运行循环,而不是基于线程。尽管您可以在主线程或工作线程上使用它,但您不必这样做。它使用 NSRunLoop 异步调用委托方法。委托方法包含一个套接字参数,允许您区分多个实例。
独立于一类。您无需浪费时间处理流或套接字。类处理所有这些。
支持 IPv4 和 IPv6 上的 TCP 流。
该库是公共领域的,最初由达斯汀·沃斯 (Dustin Voss) 编写。现在可在公共场合使用,以允许并鼓励其继续支持。

AsyncUdpSocket 是一个包装 CFSocket 的 UDP/IP 套接字网络库。它的工作原理几乎与 TCP 版本完全相同,但专为 UDP 设计。这包括排队非阻塞发送/接收操作、完整的委托支持、基于运行循环的自包含类以及对 IPv4 和 IPv6 的支持。

CocoaAsyncSocket

这里是 CFSocket 参考

CFSocket 参考

Ok first I found what I needed, with CFSocket API which will allow me to implement UDP Synchronization.

CFSocket API

Sockets are the most basic level of network communications. A socket acts in a similar manner to a telephone jack. It allows you to connect to another socket (either locally or over a network) and send data to that socket.

The most common socket abstraction is BSD sockets. CFSocket is an abstraction for BSD sockets. With very little overhead, CFSocket provides almost all the functionality of BSD sockets, and it integrates the socket into a run loop. CFSocket is not limited to stream-based sockets (for example, TCP), it can handle any type of socket.

You could create a CFSocket object from scratch using the CFSocketCreate function, or from a BSD socket using the CFSocketCreateWithNative function. Then, you could create a run-loop source using the function CFSocketCreateRunLoopSource and add it to a run loop with the function CFRunLoopAddSource. This would allow your CFSocket callback function to be run whenever the CFSocket object receives a message.

Regardless I found AsyncSocket API.

CocoaAsyncSocket supports TCP and UDP. The AsyncSocket class is for TCP, and the AsyncUdpSocket class is for UDP. Each class is described below.

AsyncSocket is a TCP/IP socket networking library that wraps CFSocket and CFStream. It offers asynchronous operation, and a native cocoa class complete with delegate support. Here are the key features:

Queued non-blocking reads and writes, with optional timeouts. You tell it what to read or write, and it will call you when it's done.
Automatic socket acceptance. If you tell it to accept connections, it will call you with new instances of itself for each connection. You can, of course, disconnect them immediately.
Delegate support. Errors, connections, accepts, read completions, write completions, progress, and disconnections all result in a call to your delegate method.
Run-loop based, not thread based. Although you can use it on main or worker threads, you don't have to. It calls the delegate methods asynchronously using NSRunLoop. The delegate methods include a socket parameter, allowing you to distinguish between many instances.
Self-contained in one class. You don't need to muck around with streams or sockets. The class handles all of that.
Support for TCP streams over IPv4 and IPv6.
The library is public domain, originally written by Dustin Voss. Now available in a public setting to allow and encourage its continued support.

AsyncUdpSocket is a UDP/IP socket networking library that wraps CFSocket. It works almost exactly like the TCP version, but is designed specifically for UDP. This includes queued non-blocking send/receive operations, full delegate support, run-loop based, self-contained class, and support for IPv4 and IPv6.

CocoaAsyncSocket

And here is the CFSocket Reference

CFSocket Reference

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文