iosobjective-csocketssocketrocket

No iOS socket frameworks can connect to server socket


I'm trying to use a socket framework to connect to a socket I have. I am able to connect just fine with raw socket code like:

uint portNo = 9900;
CFReadStreamRef readStream;
CFWriteStreamRef writeStream;
CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"123.456.789.159", portNo, &readStream, &writeStream);
inputStream = (__bridge NSInputStream *)readStream;
outputStream = (__bridge NSOutputStream *)writeStream;

[inputStream setDelegate:self];
[outputStream setDelegate:self];

[inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[inputStream open];
[outputStream open];

but when I try with any framework I get an error and I'm completely stumped as to why. I've tried SocketRocket and Jetfire. Here's the code for connecting in SocketRocket that I've tried:

_webSocket.delegate = nil;
[_webSocket close];

_webSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:@"wss://123.456.789.159"]];
_webSocket.delegate = self;

NSLog(@"Opening Connection...");
[_webSocket open];

I've also tried with the port number added.

_webSocket = [[SRWebSocket alloc] initWithURL:[NSURL       URLWithString:@"wss://123.456.789.159:9900"]];

Also tried with http, https, ws. Still nothing.

Anybody have an idea as to why?


Solution

  • WebSocket is a protocol for bringing raw socket like functionality on top of HTTP. The socket that your server is exposing likely does not support the WebSocket protocol.

    If you want a higher-level API for raw socket programming on iOS, CocoaAsyncSocket might be a good option for you.