Domanda

On iOS, I'm using bonjour to find other devices so I can stream data between the two. I was planning to use NSNetService for bonjour, and CocoaAsyncSocket for streaming.

In this example, they create a GCDAsyncSocket and a NSNetService on the same port:

socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[socket acceptOnPort:0 error:NULL];

netService = [[NSNetService alloc] initWithDomain:@"local." type:@"_YourServiceName._tcp." name:@"" port:socket.localPort];

Could someone explain the difference between NSNetService and GCDAsyncSocket?

To me it seems like I'm creating two sockets on the same port. Especially since you can create input & output streams from NSNetService.

[service getInputStream:&input outputStream:&output];
È stato utile?

Soluzione

GCDAsyncSocket is used to create a listening server socket:

socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
[socket acceptOnPort:0 error:NULL];

The port number is set to 0 which means that the OS picks an available port.

socket.localPort

is the port number chosen by the OS that the socket is then listening on.

NSNetService is used to publish the service via Bonjour:

netService = [[NSNetService alloc] initWithDomain:@"local." type:@"_YourServiceName._tcp." name:@"" port:socket.localPort];
[netService setDelegate:self];
[netService publish];

This does not create another socket, but uses the created port number together with the host name and service name, and publishes this information in the local network (using the Bonjour/mDNS protocol).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top