#include #include #include #include #include #include #include #include #include #include #include #include #define BUF_SZ 1024 struct hostent *hp; struct sockaddr_in addr; int on = 1; bool comm_is_open = false; int comm_sock = -1; char *server_host = "127.0.0.1"; int server_port = 8005; char session_id[100]; // session identifier as a string char success[] = "success"; void open_server_comm(){ addr.sin_addr.s_addr = inet_addr(server_host); addr.sin_port = htons(server_port); addr.sin_family = AF_INET; comm_sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); if( comm_sock == -1 ){ printf("open_server_comm error: create socket for server comm\n"); return; } setsockopt(comm_sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int)); if(connect(comm_sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0){ printf("open_server_comm error: connect\n");fflush(stdout); return; } comm_is_open = true; printf("Server communication open\n");fflush(stdout); } void close_server_comm(){ if( comm_is_open == false ) return; shutdown(comm_sock, SHUT_RDWR); close(comm_sock); comm_is_open = false; comm_sock = -1; printf("Server communication closed\n");fflush(stdout); } // functions called by main to interact with server int session_start(){ char req_buf[] = "GET /api/session_start\r\n\r\n"; static char resp_buf[BUF_SZ]; printf("Session Start\n"); open_server_comm(); write(comm_sock, req_buf, sizeof(req_buf)-1); bzero( resp_buf, BUF_SZ ); if( read(comm_sock, resp_buf, BUF_SZ-1) > 0 ){ //printf("Response:\n%s",resp_buf); fflush(stdout); } close_server_comm(); char *sp = strstr(resp_buf,success); if( sp == NULL ){ return(EXIT_FAILURE); }else{ // extract and store the session id from the response sp += sizeof(success); size_t n = strspn(sp,"0123456789"); memcpy(session_id,sp,n); session_id[n] = '\0'; printf("Session Start success %s\n",session_id);fflush(stdout); return(EXIT_SUCCESS); } } void ping(){ //char req_buf[] = "GET /api/ping?session_id=sid\r\n\r\n"; char req_buf[] = "GET /api/ping\r\n\r\n"; static char resp_buf[BUF_SZ]; printf("ping()\n"); open_server_comm(); write(comm_sock, req_buf, sizeof(req_buf)-1); bzero( resp_buf, BUF_SZ ); if( read(comm_sock, resp_buf, BUF_SZ-1) > 0 ){ //printf("Response:\n%s",resp_buf); fflush(stdout); } close_server_comm(); if( strstr(resp_buf,success) == NULL ){ printf("ping failure\n");fflush(stdout); }else{ printf("ping success\n");fflush(stdout); } } void session_query(char *command){ char req_buf[BUF_SZ] = "GET /api/session_query?session_id="; strcat(req_buf,session_id); strcat(req_buf,"&command="); strcat(req_buf,command); strcat(req_buf,"\r\n\r\n"); size_t req_len = strlen(req_buf); static char resp_buf[BUF_SZ]; printf("Session query\n"); open_server_comm(); write(comm_sock, req_buf, req_len); bzero( resp_buf, BUF_SZ ); if( read(comm_sock, resp_buf, BUF_SZ-1) > 0 ){ //printf("Response:\n%s",resp_buf); fflush(stdout); } close_server_comm(); char *sp = strstr(resp_buf,success); if( sp == NULL ){ printf("error from query\n"); fflush(stdout); }else{ // extract and store the session response sp += sizeof(success); size_t n = strspn(sp,"0123456789"); sp[n] = '\0'; printf("query success %s\n",sp);fflush(stdout); } } void session_stop(){ char req_buf[BUF_SZ] = "GET /api/session_stop?session_id="; strcat(req_buf,session_id); strcat(req_buf,"\r\n\r\n"); size_t req_len = strlen(req_buf); static char resp_buf[BUF_SZ]; printf("Session Stop\n"); open_server_comm(); write(comm_sock, req_buf, req_len); bzero( resp_buf, BUF_SZ ); if( read(comm_sock, resp_buf, BUF_SZ-1) > 0 ){ //printf("Response:\n%s",resp_buf); fflush(stdout); } close_server_comm(); if( strstr(resp_buf,success) == NULL ){ printf("error from Session Stop\n"); fflush(stdout); }else{ printf("Session Stop success\n");fflush(stdout); } } // end of functions called by main int main(int argc, char *argv[]){ printf("Client test\n"); session_start(); // establishes a session with backend instance ping(); // does not require the backend session_query("sid"); // does require the backend session_stop(); // terminates session and backend instance printf("end Client test\n"); }