Is there a mechanism in Objective-C similar to Netty in Java for diverting TCP to UDT protocols in Transport Layer.

Now I want to implement HTTP request and response (runs over TCP by default) to run over UDT from my application.

  1. Is this possible?

  2. Is there any in-built mechanism in iOS for this?

2

There are 2 answers

0
johnstlr On

There isn't anything quite as convenient as Netty. However you might want to take a look at the CFNetwork programming guide, specifically the sections on Communicating with HTTP Servers. This describes the CFHTTPMessage methods which can be used to create, and serialise, HTTP requests, and decode responses. As they serialise to, and decode from, buffers, you are free to transmit the messages however you like. If you already have a UDT implementation then it should be reasonably straightforward.

Note you are responsible for encoding / decoding the HTTP body appropriately but if your web service protocol is fairly simple that might only be a case of serialising / deserialising strings.

3
AntonijoDev On

If you would like to use HTTP than I suggest NSURLConnection class. For example to use POST request with headers do something like this:

int kTimeoutInterval = 30;

NSString *post = @"Something to post";
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];


NSString *link = @"http://some_link";
link = [link stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:link] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kTimeoutInterval];

[request setURL:[NSURL URLWithString:link]];

[request setHTTPMethod:@"POST"];
// set some header entries, for example:
//[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[postLength length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];

NSError *error;
NSURLResponse* response=nil;
NSData* data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

Now be careful, this is a synchronous request, and it will block the thread on which it is run for the execution time or timeout time that is defined in kTimeoutInterval constant. This can be changed to async mode with:

[NSURLConnection connectionWithRequest...

in which case the response will come through delegate method. So to decide which approach works best for you, go through NSURLConnection documentation. Hope this helps...