• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
NSStream annoying problem
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSStream annoying problem


  • Subject: NSStream annoying problem
  • From: Philippe ROBIN <email@hidden>
  • Date: Tue, 20 Apr 2004 04:09:40 -0700

Help me please guys, I am really stuck with this one...

I am using NSStream in my development, and occasionnaly I need to accept incoming connections.

For this I use an addition provided by John R Chang here...
http://homepage.mac.com/jrc/contrib/sample_code/NSStreamSample/JRCNSStreamAdditions.m

...to allow me to wait for an incoming connection on a specific port.

The problem is I cannot seam to close properly the listening socket once it has accepted the port and created the other socket. Therefore I cannot recreate a listening socket on the same port later on.

Here is the code with the callback first (where appearantly the socket is not closed properly).

Any help greatly appreciated.

Philippe

------------------------------------------the code---------------------------------

static void _SocketReadCallBack(CFSocketRef socket, CFSocketCallBackType callbackType, CFDataRef address, const void *data, void *info)
{
if (info == NULL)
return; // !!!
struct AcceptCallbackInfo * callbackInfo = (struct AcceptCallbackInfo * )info;

CFSocketInvalidate(socket);

// Remove CFRunLoopSourceRef
CFRunLoopRemoveSource(CFRunLoopGetCurrent(), callbackInfo->source, kCFRunLoopDefaultMode);
CFRelease(callbackInfo->source);

CFSocketNativeHandle s = *((CFSocketNativeHandle *)data); //CFSocketGetNative(socket);
CFReadStreamRef inputStream;
CFWriteStreamRef outputStream;

// Create input and output streams
CFStreamCreatePairWithSocket(NULL, s, &inputStream, &outputStream);

if (inputStream)
CFReadStreamSetProperty(inputStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
if (outputStream)
CFWriteStreamSetProperty(outputStream, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);

// Perform callback
[callbackInfo->target performSelector:callbackInfo->selector withObject:(id)inputStream withObject:(id)outputStream];

free(callbackInfo);
callbackInfo = NULL;
}

+ (BOOL) acceptStreamsFromTCPPort:(unsigned short)port selector:(SEL)didAcceptSelector target:(id)anObject
{
// Build signature
struct sockaddr_in sin = { .sin_family = AF_INET, .sin_port = port }; // C99-style designated initializer
CFDataRef address = CFDataCreateWithBytesNoCopy(NULL, (UInt8 *)&sin, sizeof(struct sockaddr_in), kCFAllocatorNull);
CFSocketSignature signature = {PF_INET, SOCK_STREAM, IPPROTO_TCP, address};

// Build context
struct AcceptCallbackInfo * callbackInfo = malloc(sizeof(struct AcceptCallbackInfo));
callbackInfo->target = anObject;
callbackInfo->selector = didAcceptSelector;
callbackInfo->source = NULL;
CFSocketContext context = { .info = callbackInfo };

CFSocketRef socket = CFSocketCreateWithSocketSignature(NULL, &signature, kCFSocketAcceptCallBack, (CFSocketCallBack)&_SocketReadCallBack, &context);

// A second call to this with the same port, even if the previous connection has been completed and closed will return 0 (no socket created) why???

CFRelease(address);
if (socket == NULL)
{
free(callbackInfo);
return NO;
}

callbackInfo->source = CFSocketCreateRunLoopSource(NULL, socket, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), callbackInfo->source, kCFRunLoopDefaultMode);

// The callback must cleanup socket, callbackInfo, and callbackInfo->source.

return YES;
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Prev by Date: Re: [little OT] Licensing/Implementing in Cocoa/Obj-C
  • Next by Date: Re: [little OT] Licensing/Implementing in Cocoa/Obj-C
  • Previous by thread: initWithBitmapDataPlanes + colorspace
  • Next by thread: Re: Why the warnings or errors?
  • Index(es):
    • Date
    • Thread