Reputation: 290
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, here, here and here. 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:
-(id) init {
self = [super init];
if (self) {
memset(&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", &(sin.sin_addr)) < 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 *)&sin, sizeof(sin)) < 0) {
NSLog(@"Error while binding");
}
len = sizeof(sin);
if (getsockname(self.serverSocket, (struct sockaddr *)&sin, &len) < 0) {
// Handle error here
}
int socket_port = ntohs(sin.sin_port);
if (listen(self.serverSocket, 5) < 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 *)&sin, &len);
NSLog(@"new socket response: %d", self.clientSocket);
}
@end
to establish the connection i'm calling setNetworkSocketStream:
- (void) setNetworkSocketStream:(CFStringRef) host withPortNumber:(UInt32) port; {
CFStreamCreatePairWithSocketToHost(NULL, host, port, &readStream, &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];
}
and i have implemented handleEvent method from NSStreamDelegate.
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
so i run it on the simulator and when i press the button which calls setNetworkSocketStream
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,
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
i thought that if there was a connection between the two i'll get some events from handleEvent, but nothing...
all this networking programming is very new to me so i probably made a lot of mistakes along the way.
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.
much thanks to anyone who read through the whole question and a thank you very much in advance to anyone who can help...
Upvotes: 2
Views: 549
Reputation: 290
so i finally got the simulator and my phone to exchange messages
first, i stopped doing this:
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
second, i was using :
NSString *response = self.sendMessage.text;
NSLog(@"%@", response);
NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
[outputStream write:[data bytes] maxLength:[data length]];
to send messages between the two, which for some reason didn't work so i switched the message sending method to the C send() function and that did it.
Upvotes: 1