objective-csocketscore-foundation

I am using Unix Domain sockets in a macOS application, but my CFSocketRef at some point becomes stale, then crashes on any call. Any solution?


I am using Unix Domain Sockets in an Objective-C macOS Project, using this very good answer. The answer provides a complete class to build a socket client and server. For the moment I am only interested in the client functionality. The communication works well, but after a short time the CFSocketRef becomes stale, and does not fire the callback function any more when there is available data. Any subsequent call to any function on the CFSocketRef results in a crash, for example:

return (BOOL)CFSocketIsValid( self.sockRef );

The crashlog says:

Expected typeID 61 (CFSocket) does not match actual typeID 0 (<unknown>)

I have tried to update the code in this way:

CFTypeID typeID = CFGetTypeID(self.sockRef);

if (typeID != 61) {
    NSLog(@"Here we are.");
}

Normally, the type is 61 and it works, but after becoming stale, it crashes on the CFGetTypeID call. I am not very experienced in Core Foundation, does anybody with more experience can suggest to me a debugging strategy or solution ? Many thanks


Solution

  • I could not find an explanation for the crash described in the question, but I changed approach, and I discovered that CocoaAsyncSocket can be used as a Wrapper for Unix Domain Sockets connections too, and it also comes with a complete example called DomainTest. I would highly recommend the use of it against calling the CoreFoundation calls directly, as it is an outstanding library, with a well-established stability. I have used it now in my project, and I do not get any more crashes. Thanks for your attention.

    EDIT:

    I came across another good solution from Chilkat software, which is a paid library. They have very good examples like this, that perfectly fits my purpose. You can find more information about their CkoSocket class here. Thanks again for your attention.