Creating sockaddr_in for CocoaAsyncSocket Getting Invalid IPv4 address - ios

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.

Related

TCP connection issue by using BlueSocket framework on IOS

Getting error of "Error code: -9989(0x-2705), Connection refused" by using BlueSocket framework and connecting between Mac and IOS.
Here is the logic:
I am treating Mac as a server:
// making TCP IPV4 socket
try self.listenSocket = Socket.create(family: .inet, type: .stream, proto: .tcp)
// start lisening port of 8888
try socket.listen(on: self.port)
// accept client connection when there is
let newSocket = try socket.acceptClientConnection()
// keep opening and reading data ....
iPhone as a client:
self.socket = try Socket.create(family: .inet)
try self.socket?.connect(to: ip, port: 8888)
try self.socket?.setReadTimeout(value: readWriteTimeOut)
try self.socket?.setWriteTimeout(value: readWriteTimeOut)
self.socket?.readBufferSize = Socket.SOCKET_MAXIMUM_SSL_READ_BUFFER_SIZE
client first time connect with the server works fine.
after the server receives data, client-side automatically closes the socket.
client tries to connect the server again to send back data by using same code above.
Then error displays!
I think by default when the server-side uses socket.listen, it has SO_REUSEADDR set to true
Need suggestions on how to resolve this issue. Thanks!
Just need to make sure while loop for socket.acceptClientConnection() is opening all the time....

SocketRocket not throwing error on connecting invalid host

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;

Server Unreachable Errors with NSURLSession

I have been using NSURLSession on my app since iOS 7 first came out. Recently some users started complaining that some features weren't working, after having a deep look at their logs I noticed that several network requests failed with error:
{NSLocalizedDescription=Server is unreachable. Check your network connection and try again., NSUnderlyingError=0x174c4c300 "The operation couldnât be completed. (NSURLErrorDomain error -1001.)"}
As I have no idea what the problem might be, I added some code to resolve the hostname when the requests failed:
NSString *urlString = [url host];
const char *hostUrl = [urlString UTF8String];
struct hostent *remote_entity = gethostbyname(host);
if (!remote_entity) {
DDLogWarn(#"DNS Lookup failed! Cant resolve Hostname: %# for request: %#", [url host], [url description]);
return nil;
}
else {
// Get address info from host entry
struct in_addr *remote_in_addr = (struct in_addr *) remote_entity->h_addr_list[0];
char *sremote_in_addr = inet_ntoa(*remote_in_addr);
NSString* hostIP = [NSString stringWithUTF8String:sremote_in_addr];
DDLogWarn(#"Hostname: %# IP: %# (original request: %#)", [url host], hostIP, [url description]);
return hostIP;
}
After deploying this change, I can see in my logs that even when the request fails ( with NSURLErrorDomain error -1001) I can still resolve the hostname.
I don't understand how this could happen, I can always resolve the hostname but the requests still fail somehow. Please note that I can't reproduce this and this bug only manifests from time to time with no predictable pattern.
Any ideas of how to proceed to fix this for good ? Should I implement a packet tracking mechanism in my apps (similar to traceroute on unix)? Could this be a misconfiguration on my servers since requests to other hosts (like Amazon S3) never fail? I'm using Heroku by the way.
Thanks in advance,
Ze
I recommend looking at Apple's Networking Overview Guide, specifically the Handling Network Problems Gracefully section.
When working with networking code, you have to account for the fact that sometimes things go wrong. This is especially true on mobile devices where cellular connections can come and go. In the above mentioned section on handling network problems, Apple makes recommendations for automatically retrying requests and detecting network changes using the SCNetworkReachability API. I recommend using that strategy in your networking code to give your users the best experience.
In your specific case, it appears the -1001 error is indicating a network timeout. The host lookup is succeeding, but for whatever reason, the network request did not make it to the server. In that case, a retry would make sense.

How to get mac address using GCDAsynUdpSocket

I am using GCDAyncSocket for UDP broadcast. I receive the data in the form of NSData. How can I get the mac address from that received data? I tried with
NSString *msg = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
But it shows the msg as nil.
How can I get the MAC address from this received data?
I would appreciate your help.

ios - Restkit and SSL certificate error

I have a tomcat server that uses a self signed SSL certificate and is running a web service. I am trying to connect to the web service with Restkit. However, I am getting an error related to certificate validity. Here is my code:
NSURL *url=[NSURL URLWithString:baseURL];
RKClient *client = [RKClient clientWithBaseURL:url];
client.disableCertificateValidation=YES;
RKRequest *request = [client requestWithResourcePath:[NSString stringWithFormat:#"/addEvent?deviceID=%#&eventID=%#",deviceID,eventID]];
request.disableCertificateValidation=YES;
request.delegate=self;
RKResponse *response = [request sendSynchronously];
This request fails with the following error:
2013-01-09 15:11:53.931 Mobile_ACPL[5761:907] The certificate for this server is invalid. You might be connecting to a server that is pretending to be “notify.acpl.lib.in.us” which could put your confidential information at risk.
I get this error even though I have set disableCertificateValidation to YES. How can I get this working?
EDIT: I attempted adding the certificate as shown here: https://github.com/RestKit/RestKit/pull/131
I still get the same result.
EDIT 2: It looks like the error message is being set at this line in RKRequest.m:
payload = [NSURLConnection sendSynchronousRequest:_URLRequest returningResponse:&URLResponse error:&error];
NSURLConnection does not cater for authentication challenges in synchronous calls. You need to make asynchronous calls for this to work.
In my case, I was setting disableCertificateValidation on RKClient but I was using a RKObjectManager which used a different RKClient. The following line, placed after the RKObjectManager initialization, did the trick:
[RKObjectManager sharedManager].client.disableCertificateValidation = YES;
if you are using RestKit using
client.allowsInvalidSSLCertificate = YES;
won't work, instead do this:
if you added rest kit manually to your project, click on RestKit.xcodeproj go to project > Build Settings > Preprocessor Macros
and add _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1
thats finished.

Resources