int WebSession::Recv() { m_content = NULL; m_contentLength = 0; int r = socket_select_read(m_sock, SERVER_TIMEOUT); if(r <= 0) return 0==r ? ERROR_RECV_TIMEOUT : r; r = http_server_recv(m_http); if(r < 0) return r; void* url = url_parse(http_server_get_path(m_http)); for(int i=0; i<url_getparam_count(url); i++) { const char *name, *value; if(0 != url_getparam(url, i, &name, &value)) continue; m_params.insert(std::make_pair(std::string(name), std::string(value))); } m_path.assign(url_getpath(url)); url_free(url); http_server_get_content(m_http, &m_content, &m_contentLength); if(m_contentLength > 0 && m_contentLength < 2*1024) { printf("%s\n", (const char*)m_content); } return 0; }
static int STDCALL http_server_thread(void* param) { char req[1024] = {0}; bool* running = (bool*)param; socket_t socket = tcpserver_create(NULL, PORT, 32); while(*running) { int r = socket_select_read(socket, 1000); if(1 == r) { struct sockaddr_in in4; socklen_t len = sizeof(in4); socket_t client = socket_accept(socket, (sockaddr*)&in4, &len); if(client != socket_invalid) { r = socket_recv_by_time(client, req, sizeof(req), 0, 5000); r = socket_send_all_by_time(client, s_reply, strlen(s_reply), 0, 5000); r = socket_shutdown(client, SHUT_RDWR); r = socket_close(client); printf("server side close socket\n"); } } } return 0; }