开发者

How to run Objective-C binary on a web server?

开发者 https://www.devze.com 2023-04-02 04:26 出处:网络
Alright, I have a rather odd question here. I feel much more comfortable writing code in Objective-C over any other language.I recently had to do some server-side programming, which required me to lea

Alright, I have a rather odd question here. I feel much more comfortable writing code in Objective-C over any other language. I recently had to do some server-side programming, which required me to learn PHP. It works, yeah, but for fun I want to achieve the same thing through Objective-C. So, I created a binary using Xcode's Foundation preset. Here's most of the binary:

#import <Foundation/Foundation.h>
#import "JSONKit.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *theURL = [NSString stringWithFormat:@"http://blahblahblah.com/blah"];
    NSError *err = nil;
    NSURLResponse* response = nil;
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc] init] autorelease];
    NSURL*URL = [NSURL URLWithString:theURL];
    [request setURL:URL];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setTimeoutInterval:30];
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    NSDictionary *someData = [data objectFromJSONData];
    NSString *someString = [[someData objectForKey:@"foo"]objectForKey:@"bar"开发者_如何学Python];
    //do something

    [pool drain];
    return 0;
} 

Quite basic code. It simply downloads some stuff from my server and I parse the JSON result and get a string that I want to use. So, my question is - how can I run this on my Linux-based server? I know it's possible, maybe using GNUStep (or cocotron?), which I don't know how to use. Anyone have ideas?


Well, I suggest the same thing as the @lacqui.. Use CGI to run your program.. and here's the steps ..

(Side note: using CGI is deprecated as it starts a process each time a request is coming to the server (modern servers/web containers initiate a new thread (vs process).)

So, let's Start:

  • The input at hand is a program written in Objectiv-c
  • The output is a CGI script(program or whatever they name it) that will run inside some http server.

First, let me ask you, What's the target platform to deploy your application?

  • If target deployment platform is a Mac then you will have to get the binary out of xcode ( I think it would be in a .dmg format) and find some where how to run a .dmg as a CGI program inside a web server ( I am not sure if apache runs under Mac or not)

  • But If it is Windows or Linux:

    1. You will need to compile your application using GNUstep (I know nothing about portability from Xcode to GNUstep) You will need a GNUstep. Steps to install GNUstep either for Windows or Linux is trivial.
    2. Once Installing GNUstep, you will have to compile your application again using it, refer to the same two links above to know how to compile your application.
    3. The issue here is, AFAIK, GNUstep don't fully support Objc-2, so possibilities that the compilation will fail cause of usage of JSONKit.h is high. If your program compiles successfully, then you are almost done.
    4. Suppose your program compiles, And you now have the binary program.. You will need to deploy it in some HTTP server that have CGI enabled. You can follow my blogpost here to know how to deploy a binary program written in C into some small http server called mini-httpd on Linux (it should apply to any binary program regardless of its source language).


What you want to look at is called the Common Gateway Interface. It is a protocol that states the way a web server will interact with subordinate processes.

What will happen is that, when a user browses to the URL that is mapped to your program, the server will start your program, and put the text of the request into STDIN. Your program will do whatever processing is required, then put the results (as well as some header information) into STDOUT.


What goes wrong when you try? You should be able to compile it with the GCC's Objective-C compiler. You should be able to run it.

0

精彩评论

暂无评论...
验证码 换一张
取 消