SocketRocket not throwing error on connecting invalid host - ios

I am developing an iPhone App (using iOS 9 beta). I am using Socket connections for which I am using SocketRocket client library. But when I try to establish a wss connection with some invalid host name, I don't get any error on opening socket, connection or even on sending data, so whenever I try to run program it seems like information about host is correct and data is being sent.
I am using current version of SocketRocket library, I have added SRWebSocket.h, SRWebSocket.m and SocketRocket-Prefix.pch files in my project. Following is the part of code I have:
NSString* url = [NSString stringWithFormat:#"wss://%#/myproject/stream?data=%d", webSocketHost, dummyData];
SRWebSocket *webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];
webSocket.delegate = self;
// open websocket
[webSocket open];
// send message to websocket
[webSocket send:[self getJSONString:parameters]];
// close websocket
[webSocket close];
webSocket = nil;
If I pass some random host name like "abc.def" for the variable webSocketHost, it will still run smoothly (I have try-catch blocks surrounding above code, and I also tried to put break points in between and debugged it line by line).
And even when I don't have any internet connection to my phone, there aren't any errors thrown.
Does anyone know what could be the problem?
Thanks!

Are you implementing this delegate method? The library won't throw an error when you call [webSocket open], it will call this method if it can't connect to the endpoint sometime in the future since establishing a connection is an asynchronous operation.
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;

Related

Stop & Start SignalR-Objc SRHubConnection multiple times makes connection unstable

In one of our iOS app using SignalR-ObjC, the connection is working fine across the app.
As in iOS app in background is not responsive and can't receive signalr messages. So we have following implementation:
SRHubConnection *connection = [[SRHubConnection alloc] initWithURLString:url useDefault:NO]
App goes to Background: [connection stop]; Stop the connection.
App comes to Foreground: [connection start]; Create a new connection object and start it. .
But if we switch between background and foreground 3 to 4 times continuously, makes the connection unstable and it do not connects again and throws following error.
[HTTP_BASED_TRANSPORT]
Cleandisconnectfailed.ErrorDomain=NSURLErrorDomainCode=-1001"The request timed out."UserInfo={
NSUnderlyingError=0x159283ae0{
ErrorDomain=kCFErrorDomainCFNetworkCode=-1001"The request timed out."UserInfo={
NSErrorFailingURLStringKey=http: //websiteurl.net/signalr/abort?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22hotlinechathub%22%7D%5D&connectionToken=wxhidka/adfaf/MDf57llaaBz22i55k7%2BUVNX4DHCixM1hX1k%2BhVy/Yi1FwnwMdwhzIrgpaeD5E4oC%2B702UnosdkQL1z2FrgebWw2uY0qCh9LG0Zfj9ZdY2pRG0&transport=longPolling,
NSErrorFailingURLKey=http: //website.net/signalr/abort?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22hotlinechathub%22%7D%5D&connectionToken=gcukXaFIN/wm4ilNrj/MDf57llaaBz22i55k7%2BUVNX4DHCixM1hX1k%2BhVy/Yi1FwnwMdwhzIrgpaeD5E4oC%2B702UnosdkQL1z2FrgebWw2uY0qCh9LG0Zfj9ZdY2pRG0&transport=longPolling,
_kCFStreamErrorCodeKey=-2102,
_kCFStreamErrorDomainKey=4,
NSLocalizedDescription=Therequesttimedout.
}
},
NSErrorFailingURLStringKey=http: //website.net/signalr/abort?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22hotlinechathub%22%7D%5D&connectionToken=gcukXaFIN/wm4ilNrj/MDf57llaaBz22i55k7%2BUVNX4DHCixM1hX1k%2BhVy/Yi1FwnwMdwhzIrgpaeD5E4oC%2B702UnosdkQL1z2FrgebWw2uY0qCh9LG0Zfj9ZdY2pRG0&transport=longPolling,
NSErrorFailingURLKey=http: //website.net/signalr/abort?clientProtocol=1.3.0.0&connectionData=%5B%7B%22Name%22%3A%22hotlinechathub%22%7D%5D&connectionToken=gcukXaFIN/wm4ilNrj/MDf57llaaBz22i55k7%2BUVNX4DHCixM1hX1k%2BhVy/Yi1FwnwMdwhzIrgpaeD5E4oC%2B702UnosdkQL1z2FrgebWw2uY0qCh9LG0Zfj9ZdY2pRG0&transport=longPolling,
_kCFStreamErrorDomainKey=4,
_kCFStreamErrorCodeKey=-2102,
NSLocalizedDescription=Therequesttimedout.
}
And error:
[CONNECTION]
negotiationfailedErrorDomain=NSURLErrorDomainCode=-1001
"The request timed out."UserInfo={Same as displayed above}
Question:
Is there a way to stop SRHubConnection from going to such unstable state and start the new connection without error?
Make sure that you are not calling start or stop multiple times, on connection object.
Calling them multiple times can lead connection to be in unstable state.

AFNetworking -1017 error

I am working on iOS application and facing one strange issue. I am using AFNetworking framework to communicate with server (HTTPS communication). I am retrieving student data from server by using "getStudentData" web service API. It is post request. It works for all user ids except one. It fails when we have a data for more than 450 students. Below are the error details,
Error Domain=NSURLErrorDomain Code=-1017 "cannot parse response" UserInfo=0x7bf9c7d0 {NSErrorFailingURLStringKey=https://www.fdmobileservices.com/mAccountsWeb/services/speedpass/rpc, _kCFStreamErrorCodeKey=-1, NSErrorFailingURLKey=https://www.fdmobileservices.com/mAccountsWeb/services/speedpass/rpc, NSLocalizedDescription=cannot parse response, _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x7bf9a380 "cannot parse response"}
As per error description, It tells "Could Not parse", so I think it may be due to server is returning "nil" or some data other than JSON format.
But I am not able to trace it since from application side it directly goes into below method,
- (void)connection:(NSURLConnection __unused *)connection
didFailWithError:(NSError *)error
Is there any way from application side to trace root cause? This method works for other user login except one.
I tried using web client to access this service by same user login, it works well and return data of 450 students. I think due to some reason iOS network layer rejects this. I am trying to find out.
Thanks in advance.
As you can see here, kCFURLErrorCannotParseResponse = -1017. So probably this is saying that there is something wrong in the parameters.
If You're using AFNetworking 2.0, I suggest You to deep debug your response flow with severals breakpoints.
Specifically try to set a breakpoint on AFURLResponseSerialization.m at the start of the method:
- (id)responseObjectForResponse:(NSURLResponse *)response
data:(NSData *)data
error:(NSError *__autoreleasing *)error;
Inside the method, if the below condition is true, probably there is something wrong (but you can step into validateResponse:data:error: to better understand what's wrong):
if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error])
If the above condition is false you can then check about the generated responseString:
NSString *responseString = [[NSString alloc] initWithData:data encoding:stringEncoding];
If you're expecting json data, check that responseString is not nil and data data is valid, maybe also the stringEncoding.
Also ensure that the contentType that you're receiving is the one you're expecting.

TUSKit iOS 8: ERROR: connection requested new body stream, which is currently not supported

Using TUSKit for iOS and tus.io for our API. After setting up tus.io and configuring the example project for TUSKit
<TUSResumableUpload.m:(115)> Finished upload to `PATH/videos/uploads/15/` for fingerprint assets-library://asset/asset.mp4?id=30C691FA-5866-4498-82CB-E95FB600BD74&ext=mp4
2015-04-07 01:46:36.397 TUSKit[207:8879] url: PATH/videos/uploads/15/
2015-04-07 01:46:36.410 TUSKit[207:8879] <TUSResumableUpload.m:(157)> ERROR: connection requested new body stream, which is currently not supported
the snippet
- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)request {
TUSLog(#"ERROR: connection requested new body stream, which is currently not supported");
return nil;
}
in TUSResumableUpload.m
is where we are getting the error, I'm just not sure why
the uploads on the server are started, but empty.
Any suggestions on how to fix this?
Xcode 6.1.1
Solved!
There was a bug in the API with the redirects which caused the TUSKit to fail to finish the upload.
And then we had to change the way binary data was received in order to start getting data in our files.

Creating sockaddr_in for CocoaAsyncSocket Getting Invalid IPv4 address

I'm having trouble connecting to a Java socket server. I know the server is working because I connect via a Java socket. I'm using the CocoaAsyncSocket library to make a client connection from an iOS device. I've tried the following,
[socket connectToHost:#"XXX.XXX.X.XXX" onPort:9090 error:&err]
method but the server never sees the client connect and the client (CocoaAsyncSocket) thinks its connected. So thats no good, then I realized there was another connection method available.
So I'm thinking I should be using the connectToAddress method instead. I've used this post as a reference for my current code but I'm still getting an error and I'm not sure why. The only difference from my version and the suggested version is for the length they use sa_len and I was getting a error and xCode wanted to switch it to sin_len, so I did. I'm really new to direct socket connections so bear with me.
GCDAsyncSocket *socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()];
struct sockaddr_in ip4addr;
ip4addr.sin_family = AF_INET;
ip4addr.sin_port = htons(9090);
inet_pton(AF_INET, "XXX.XXX.X.XXX", &ip4addr.sin_addr);
NSData *discoveryHost = [NSData dataWithBytes:&ip4addr length:ip4addr.sin_len];
NSError *err = nil;
if (![socket connectToAddress:discoveryHost error:&err])
{
NSLog(#"I CANNOT CONNECT!");
}
else
{
NSLog(#"IM CONNECTED!");
}
The connection fails and the error is,
Error Domain=GCDAsyncSocketErrorDomain Code=2 "A valid IPv4 or IPv6 address was not given" UserInfo=0x8bac880 {NSLocalizedDescription=A valid IPv4 or IPv6 address was not given}
I changed
NSData *discoveryHost = [NSData dataWithBytes:&ip4addr length:ip4addr.sin_len];
to
NSData *discoveryHost = [NSData dataWithBytes:&ip4addr length:sizeof(ip4addr)];
and it fixed that error I was getting. However, the reason I wasn't able to connect via the connectToHost method was due to my server socket code. I have two server sockets accepting connections. I commented out the second and it worked just fine. I'm guessing it was due to the thread being locked by the second socket or something.

How to sent different request using the same connection?

My app behaves like this:
Client: sent a request (using a NSURLConnection) to server asking server to generate a file and
download that file.
Server: generate the file and send the file's data
back to client (using chunked encoding)
After generating the file, server will update the file's header, so after downloading
file, client needs to make second request to get the updated header. During
file generating and updating, if server detects that connection from client is
closed, it will delete the generated file, so the connection need to
be kept alive.
Client: sent a second request (another NSURLConnection) to get updated header
The problem is after first request (i use NSURLConnection) server detects connection closed and delete the generated file, so the second request fails.
I suspect the reason is that i use two different instance of NSURLConnection, so I look into the document for something like NSMutableURLConnection. It doesn't exist!.
My question: Is there a way to keep connection alive between to request ( by reusing NSURLConnection, or using NSMutableRequest and re-request with the same NSURLConnection)?
You can use :
NSURLConnection *currentConnection = [NSURLConnection alloc];
[currentConnection initWithRequest:request1 delegate:self];
[currentConnection initWithRequest:request2 delegate:self];
You can verify the Original and Current NSURLRequest using :
- (NSURLRequest *)originalRequest NS_AVAILABLE_IOS(5_0);
- (NSURLRequest *)currentRequest NS_AVAILABLE_IOS(5_0);

Resources