4

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?

RXGangam
  • 199
  • 12
  • Have a look at GCAsyncSocket class, it is widely used class for TCP, UDP ect. and is very powerful, maybe you'll find it useful - I don't know if it supports exactly what you need but if I were you I would start from there: https://github.com/robbiehanson/CocoaAsyncSocket – AntonijoDev Dec 31 '14 at 11:04
  • Thank you for the suggestion.In above example I am able to connect the server with the specified host and port numbers using **UDP**. Now I want to make a Get/Post web service call and handle the response using **UDP**. Can you please help me. – RXGangam Jan 02 '15 at 08:27
  • possible duplicate of [Analog Java's Netty in Obj-C](http://stackoverflow.com/questions/9323035/analog-javas-netty-in-obj-c) – bummi Feb 06 '15 at 07:21
  • Jump looks like Netty, But there is no **Http implementation with UDT** available in JUMP. Anyone can help me to implement it. – RXGangam Feb 10 '15 at 08:53

2 Answers2

0

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...

AntonijoDev
  • 1,317
  • 14
  • 29
  • Thanks for your help. This is I know, But I need to make web service call using **UDP**. Is it possible? If possible, Can you please provide the sample code or link If you know. – RXGangam Jan 02 '15 at 09:25
  • **This is my requirement:** I have to convert TCP(Get/Post) protocol to UDP/UDT (Get/Post) protocol over Transport layer. – RXGangam Jan 02 '15 at 09:26
  • I am not aware of some existing component that does that for you on iOS. But you could implement it yourself. I guess that with UDP it relies on you how the protocol will look like. It wont be easy thou :). – AntonijoDev Jan 02 '15 at 09:34
0

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.

johnstlr
  • 1,431
  • 7
  • 6