javascript - C++ server require ajax call to connect twice -


i have created simple c++ http server accept post request , parses data.

#pragma comment (lib, "ws2_32.lib") // #pragma comment (lib, "mswsock.lib")  #define default_buflen 4096 #define default_port "8080"    class chttp { public:     chttp() {         recvbuflen = default_buflen;         iresult = wsastartup(makeword(2, 2), &wsadata);         if (iresult != 0) {             printf("wsastartup failed error: %d\n", iresult);               }         zeromemory(&hints, sizeof(hints));         hints.ai_family = af_inet;         hints.ai_socktype = sock_stream;         hints.ai_protocol = ipproto_tcp;         hints.ai_flags = ai_passive;         iresult = getaddrinfo(null, default_port, &hints, &result);         if (iresult != 0) {             printf("getaddrinfo failed error: %d\n", iresult);             wsacleanup();         }     }      int init() {         listensocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);         if (listensocket == invalid_socket) {             printf("socket failed error: %ld\n", wsagetlasterror());             freeaddrinfo(result);             wsacleanup();             return 1;         }          iresult = bind(listensocket, result->ai_addr, (int)result->ai_addrlen);          if (iresult == socket_error) {             printf("bind failed error: %d\n", wsagetlasterror());             freeaddrinfo(result);             closesocket(listensocket);             wsacleanup();             return 1;         }         freeaddrinfo(result);          iresult = listen(listensocket, somaxconn);         if (iresult == socket_error) {             printf("listen failed error: %d\n", wsagetlasterror());             closesocket(listensocket);             wsacleanup();             return 1;         }          return 0;     }      void acceptconnection() {         cout << "starting listen " << endl;         clientsocket = accept(listensocket, null, null);         if (clientsocket == invalid_socket) {             printf("accept failed error: %d\n", wsagetlasterror());             closesocket(listensocket);             wsacleanup();             return;         }         cout << "listening " << endl;         datahandler();     }      void datahandler() {         bool isdoneloadingdata = false; // flag break outer loop of loading data         char recvbuf[default_buflen]; // data loaded here         map<string, string > headers;         int superposition = 0;         cout << "begin" << endl;         {             iresult = recv(clientsocket, recvbuf + superposition , default_buflen - superposition, 0);             cout << "iresult " << iresult << endl;             string tmp_buff(recvbuf,iresult+superposition);             size_t index;             size_t bytescount = 0;             while( (index = tmp_buff.find("\r\n")) != string::npos ) {                 if (tmp_buff[0] == '\r' && tmp_buff [1] == '\n') {                      size_t content_length = atoi(headers["content-length:"].c_str());                     int sparedata = tmp_buff.size() - 2; // check spare data in buffer;                     int dataload = content_length - sparedata;                     if (!dataload) {                         headers.insert(make_pair("data", tmp_buff.substr(2)));                     }                     else {                         char *otherdata = new char[content_length];                         memcpy(otherdata, tmp_buff.substr(2).c_str(), sparedata);                         int t = recv(clientsocket, otherdata + sparedata , content_length - sparedata, 0);                         headers.insert(make_pair("data", string(otherdata, content_length)));                     }                     isdoneloadingdata = true;                     break;                           }                  string tmp_header_line = tmp_buff.substr(0, index);                 int pos = tmp_header_line.find_first_of(' ');                 headers.insert(std::make_pair( tmp_header_line.substr(0,pos),tmp_header_line.substr(pos)));                 bytescount += tmp_header_line.size() + 2;                 tmp_buff = tmp_buff.substr(index + 2);             }             if (isdoneloadingdata) break;             memmove(recvbuf, recvbuf + bytescount, ( iresult + superposition) - bytescount );             superposition = ( iresult + superposition ) - bytescount;          } while (iresult > 0);         (auto &a : headers)             cout << a.first << a.second << endl;         cout << "\\\\\\\\\\\\\\\\" << endl;          return;     }   private:     socket listensocket = invalid_socket;     socket clientsocket = invalid_socket;     struct addrinfo *result = null;     struct addrinfo hints;     int recvbuflen;     int iresult;     wsadata wsadata; };  int main() {     chttp server;     if (server.init()) {         cout << "error occured " << endl;     }          server.acceptconnection();      return 0; } 

i using postman test it. far have found no problem using postman ( support file posting yet ). tried create simple chrome extension posts data server:

var url = document.getelementbyid("url");

var butt = document.getelementbyid("button"); chrome.tabs.getselected(null,function(tab) {     url.value = tab.url; });       butt.addeventlistener('click', function(){         loaddoc();       });  function loaddoc() {   var xhttp = new xmlhttprequest();   xhttp.onreadystatechange = function() {     if (this.readystate == 4 && this.status == 200) {       document.getelementbyid("demo").innerhtml = this.responsetext;     }   };   xhttp.open("post", "http://localhost:8080", true);   xhttp.setrequestheader("content-type", "application/x-www-form-urlencoded");   xhttp.send("fname=henry&lname=ford"); } 

invoking ajax call, server starts "listen" , no data sent. need invoke ajax call second time in order data sent not way ajax call should behave.

i have found nothing indicate cause this. has met same problem this? why happening , how fix this?

thanks help.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -