Detailed explanation of cocoahttpserver use (1)

cocoahttpserver is an open source project by deusty designs, it supports asynchronous socket, ipv4 and ipv6, http Authentication and TLS encryption, small and exquisite. In addition to supporting the GET method, other things can be said to have all that should be, and all that should not be.

The above paragraph was seen at http://blog.csdn.net/chengyingzhilian/article/details/7968065 , and it felt very powerful, so I downloaded the demo from github and studied it in detail

The first is to import the project into your own project

1 This is very simple, just drag and drop the required files to our project.

The above is my file structure, so that we can use httpserver to create our http server



 httpServer = [[HTTPServer alloc] init];
    [httpServer setType:@"_http._tcp."];
   // [httpServer setPort:12345];
    NSString * webLocalPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Web"];
    [httpServer setDocumentRoot:webLocalPath];
    
    NSLog(@"Setting document root: %@", webLocalPath);
    
    NSError * error;
    if([httpServer start:&error])
    {
        NSLog(@"start server success in port %d %@",[httpServer listeningPort],[httpServer publishedName]);
        
    }
    else
    {
        NSLog(@"启动失败");
    }

With the above code, we can create our http server, isn't it very simple,

have to be aware of is

[httpServer setPort:12345];

This code, we can use a fixed port when testing. When it's official, remove it, you know why

2 Such a simple http server is set up on your mobile phone, but there is one thing to note, when dragging and dropping web folders, you must use the real directory, not the virtual directory of xcode, which is the blue one in the picture above color folder

You can drag and hold like the picture below



Your server is now accessible from the web.

In the next chapter, I will describe how to build a web client with an interface, this aspect is set to html development, and how to connect to the file transfer service of httpserver

Guess you like

Origin blog.csdn.net/wjsxiaoweige/article/details/25902293