Device are not able to exchange data using NSStream

I am developing a 3D Wi-Fi multiplayer game for iPhone and iPad without using any game engine. I have taken reference of the “WiTap” sample for device connectivity (Link to the sample code: WiTap sample)


I found that the devices are not able to receive the data sent by other device. I debugged my code and I found that the “NSStreamEventHasBytesAvailable” event is not triggered even if the other device has successfully written data to the stream. Also I found that when I close the streams from one device, the other device is not receiving “NSStreamEventEndEncountered” event on NSStreamDelegate.


The above issue only happens when I set “includesPeerToPeer” property to YES. I am setting this property to YES to allow faster discovery of devices (to avoid bug described here: link). I am testing my game on iPhone 5 (running on iOS 8.4) and iPad 3rd generation (running on iOS 7.1).


I have three questions:

  1. Why the above issue only happens when I set “includesPeerToPeer” property to YES?
  2. I think the above issue can happen if two devices are referring to different streams. Is there any method to confirm my assumption?
  3. Are there any other coding errors which can cause this issue?


I’d appreciate any suggestions on this issue. Thank you in advance.


Code snippets:


1. To search for nsnetservice

-(BOOL)searchForServicesOfType:(NSString *)type inDomain:(NSString *)domain {

self.netServiceBrowser = [[NSNetServiceBrowser alloc] init];

self.netServiceBrowser.includesPeerToPeer = YES;

[self.netServiceBrowser setDelegate:self];

[self.netServiceBrowser searchForServicesOfType:type inDomain:domain];

[self.tableView reloadData];

return YES;

}


2. To publish nsnetservice

self.netService = [[NSNetService alloc] initWithDomain:domain type:protocol name:name port:0];

if(self.netService == nil)

return NO;

self.netService.includesPeerToPeer=YES;


[self.netService setDelegate:self];

[self.netService publishWithOptions:NSNetServiceListenForConnections];


3. To receive request on our net service


- (void)netService:(NSNetService *)sender didAcceptConnectionWithInputStream:(NSInputStream *)istr1 outputStream:(NSOutputStream *)ostr1 {


[[NSOperationQueue mainQueue] addOperationWithBlock:^{

[self.netService stop];

[self.delegate didAcceptConnectionForServer:self inputStream:istr1 outputStream:ostr1];


}];

}


4. To send request to specific net service

BOOL success;

NSInputStream * inStream;

NSOutputStream * outStream;


success = [netService getInputStream:&inStream outputStream:&outStream];


if ( ! success ) {

[self setup_wifi];

} else {

_inStream = inStream;

_outStream = outStream;

isHost = true;

[self openStreams];

}

Given that you started with WiTap, and WiTap sets

includesPeerToPeer
, please test with an unmodified version of WiTap in your environment to see whether it works or not. That’ll tell us whether the problem is with your code (WiTap works but your code doesn’t) or in your environment (WiTap doesn’t work).

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thank you for the feedback. I tested the unmodified version of “WiTap” sample and I found that the sample works properly. Hence, there must be issue with my code. Can you suggest code regions which can possibly cause this issue?

I can’t see anything wrong with the code you posted but it’s easy to miss stuff in this sort of situation.

What stream events do you see? You can work that out by putting log statements in

-stream:handleEvent:
on both the client and server.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi,

Did you ever solve this?

I'm running into a similar issue.

Device are not able to exchange data using NSStream
 
 
Q