Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to establish a p2p connection between the iphone and the simulator
    primarykey
    data
    text
    <p>I want to stream data between two peers. so i thought i'll start with just trying to send a simple string message between the two (or even just check if i can establish a connection between the two). i'm using my iphone and the simulator. eventually the app will have to work cross platform (android/ios) so i'm using POSIX C networking APIs which will work cross platform as i read in the documentation. I compiled the code from my own code and other sources such as SO, <a href="https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/NetworkingTopics/Articles/UsingSocketsandSocketStreams.html" rel="nofollow">here</a>, <a href="http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server" rel="nofollow">here</a> and <a href="http://beej.us/guide/bgnet/output/html/multipage/syscalls.html" rel="nofollow">here</a>. From what i gathered, each peer has be able to act as a server or client, so for the server part what i have so far is this code:</p> <pre><code>-(id) init { self = [super init]; if (self) { memset(&amp;sin, 0, sizeof(sin)); sin.sin_len = sizeof(sin); sin.sin_family = AF_INET; sin.sin_port = htons(21360); if (inet_pton(AF_INET, "94.21.53.41", &amp;(sin.sin_addr)) &lt; 0) { NSLog(@"Error while converting IP"); } if ((self.serverSocket = socket(PF_INET, SOCK_STREAM, 0))) { NSLog(@"socket retrieve success"); } if (bind(self.serverSocket, (struct sockaddr *)&amp;sin, sizeof(sin)) &lt; 0) { NSLog(@"Error while binding"); } len = sizeof(sin); if (getsockname(self.serverSocket, (struct sockaddr *)&amp;sin, &amp;len) &lt; 0) { // Handle error here } int socket_port = ntohs(sin.sin_port); if (listen(self.serverSocket, 5) &lt; 0) { NSLog(@"Error while listening"); } NSThread *acceptConnectionThread = [[NSThread alloc] initWithTarget:self selector:@selector(startAcceptingConnections) object:nil]; [acceptConnectionThread start]; } return self; } -(void) startAcceptingConnections { self.clientSocket = accept(self.serverSocket, (struct sockaddr *)&amp;sin, &amp;len); NSLog(@"new socket response: %d", self.clientSocket); } @end </code></pre> <p>to establish the connection i'm calling setNetworkSocketStream:</p> <pre><code>- (void) setNetworkSocketStream:(CFStringRef) host withPortNumber:(UInt32) port; { CFStreamCreatePairWithSocketToHost(NULL, host, port, &amp;readStream, &amp;writeStream); inputStream = (__bridge_transfer NSInputStream *)readStream; [inputStream setDelegate:self]; [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [inputStream open]; outputStream = (__bridge_transfer NSOutputStream *)writeStream; [outputStream setDelegate:self]; [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [outputStream open]; } </code></pre> <p>and i have implemented handleEvent method from NSStreamDelegate.</p> <p>when my view controller loads up, i init the server object and i have a button which calls setNetworkSocketStream:(CFStringRef)@"94.21.53.41" withPortNumber:(UInt32)21360</p> <p>so i run it on the simulator and when i press the button which calls setNetworkSocketStream</p> <p>i get responses like: new socket response: 12, stream event 1, Stream opened, stream event 1, Stream opened, stream event 4, StreamEventHasSpaceAvailable, stream event 2,</p> <p>so i tried running it at the same time on my phone only without the server class init, but when i call setNetworkSocketStream from there nothing happens</p> <p>i thought that if there was a connection between the two i'll get some events from handleEvent, but nothing...</p> <p>all this networking programming is very new to me so i probably made a lot of mistakes along the way.</p> <p>after all that, my questions are if anyone can point me to why i can't establish the connection?, is my code even remotely close to establish a succeful connection?, is it even possible to test this scenario with both the simulator and the iphone?, really any suggestions, advice or pointers will be very helpful.</p> <p>much thanks to anyone who read through the whole question and a thank you very much in advance to anyone who can help...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload