UNIX域套接字和可可

UNIX域套接字和可可

本文介绍了UNIX域套接字和可可的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用UNIX域套接字的Cocoa应用程序中实现IPC,我没有经验。我发现苹果的示例项目,但它是用C写的,看起来很好,相当复杂(是的,我已经阅读了大部分内容)。

I want to implement IPC in a Cocoa application using UNIX domain sockets, with which I have no experience. I found Apple's CFLocalServer example project, but it's written in C and looks, well, fairly complicated (and yes, I have read most of it).

CFLocalServer中演示的技术仍然是最先进的,在Objective-C和Cocoa / Foundation中实现UNIX域套接字?

Are the techniques demonstrated in CFLocalServer still state-of-the-art, or is there a way to implement UNIX domain sockets in Objective-C with Cocoa/Foundation?

我正在玩 NSSocketPort s和 NSFileHandle s(这提供了一个健康的抽象,这是伟大的这个项目),并在,但还没有能够让它一切正常。

I'm playing with NSSocketPorts and NSFileHandles (which provide a healthy amount of abstraction, which is great for this project) and found some very-related code in Mike Bean's Networking in Cocoa, but haven't been able to Make It All Work yet.

推荐答案

最后,我使用UNIX域套接字,他们工作得很好。我已经为我的服务器设置了套接字(但是没有写代码,成功地创建它自己),并让客户端连接到它。

In the end, I did use UNIX domain sockets, and they work pretty well. I have launchd set up the socket for my server (but did write code, successfully, to create it myself), and have the client connect to it.


  • 您可以在NSFileHandle中封装连接的两端

  • You can wrap both ends of the connection in NSFileHandle

您需要在客户端使用 connect()创建与套接字的连接

You do need to use connect() on the client side to create a connection to the socket

您应该使客户端和服务器都忽略SIGPIPE

You should make both the client and the server ignore SIGPIPE

如果您使用零长度数据回调,套接字的另一端已断开连接(即服务器/客户端退出)。在这种情况下,请确保关闭并释放套接字的结尾 - 不要尝试再次读取数据,或者只是获取另一个零长度的回调(永远)。

If you get called back with zero-length data, it means that the thing on the other end of the socket has disconnected (i.e. the server/client exited). Make sure to close and release your end of the socket in this case — don't try to read data from it again, or just get another zero-length read callback (for ever and ever)

您负责对通过套接字发送的消息进行分隔和组合(一端发送的单个消息可能在另一端发送多个消息,或者可能会合并多个消息)

You’re responsible for delimiting and assembling the messages you send across the socket (a single message sent on one end may be spit into more than one on the other end, or multiple messages might be merged)

我很乐意分享或发布代码,只是没有时间清理公共消费。如果有人有兴趣,请让我知道。

I would be happy to share or publish code, just haven’t had the time to clean it up for public consumption. If anyone is interested, let me know.

这篇关于UNIX域套接字和可可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 20:23