Developing a lightweight, hi-performance and dynamic JSON
api server with C
Shaikh Sonny Aman for
SQABD LT4
Background
Play Tools
PHP Java Python C
WHY!!
Requirement
http://myserver.com:8080?mod=score&action=get_score&id=123
{score:4,id:123
}
The Server
if ( (client_socket=accept(server_socket, (struct sockaddr *)&cad, &alen)) < 0) {
printf("accept failed\n");exit (1);
}pthread_create(&tid, NULL, serverthread, (void*)client_socket );
While(1){
}
accept connection from clientrun a thread to process
Processing Request
Read all available data
recv(client_socket, buffer,buffer_size, 0);
Processing Request
Host: www.http.header.free.frAccept: image/gif, image/x-xbitmap, image/jpeg, imagepjpeg,Accept-Language: FrAccept-Encoding: gzip, deflate
Get the request params
mod=score&action=get_score&id=123GET: Http1.1
Calling Dynamic Function
PIC : Position Independent Code
dlopen(PATH, mode)
dlsym(handle, fun_name)
void (*fn)(int* cs,cJSON** js);
void echo_name(int* client_socket, cJSON** request)
Demo
QuickTime™ and aH.264 decompressor
are needed to see this picture.
QnA