Note that there are some explanatory texts on larger screens.

plurals
  1. POData Loss during write back using sokcet programming
    primarykey
    data
    text
    <p>Currently, I'm learning how to build a transparent HTTP proxy in C++. There had two issues on the proxy client side I couldn't resolve for long time. Hope someone can point out the root causes based on following scenarios. Thanks a lot. :D</p> <p>The HTTP proxy I built right now is somehow work partially only. For example, I could access google's main page through proxy while I couldn't get any search result after I typed keyword(the google instant is also not working at all). On the other hand, youtube is working perfectly includes searching, loading video and commenting. What's more, there also got some websites like yahoo even couldn't display main page after I keyed in its URL. </p> <p>The reason why I said the issues are on the proxy client side at the begining is because I traced the data flow of my program. I found out the written size returned by socket programming function write() was smaller than the data size I passed to my write back function. The most weird observation for me was the data losing issue is independent from the size of data. The socket write() function could work properly for youtube video data which is nearly 2MB while it would loss data for google search request which is just 20KB. </p> <p>Furthermore, there also had another situation that browser displayed blank when the data size I passed to my write back function and the written size returned by socket write function() are the same. I used wireshark to trace the flow of communication and compared mine with pure IP communication without proxy involved. I found out that browser didn't continuously send out HTTP requests after it received certain HTTP responses comparing with pure IP communication flow. I couldn't find out why the browser didn't send out rest of HTTP requests. </p> <p>Following is my code for write back function: </p> <pre><code>void Proxy::get_data(char* buffer, size_t length) { cout&lt;&lt;"Length:"&lt;&lt;length&lt;&lt;endl; int connfd; size_t ret; // get connfd from buffer memset(&amp;connfd, 0, sizeof(int)); memcpy(&amp;connfd, buffer, sizeof(int)); cout&lt;&lt;"Get Connection FD:"&lt;&lt;connfd&lt;&lt;endl; // get receive data size size_t rData_length = length-sizeof(int); cout&lt;&lt;"Data Size:"&lt;&lt;rData_length&lt;&lt;endl; // create receive buffer char* rBuf = new char[rData_length]; // allocate memory to receive buffer memset(rBuf, 0, rData_length); // copy data to buffer memcpy(rBuf, buffer+sizeof(int), rData_length); ret = write(connfd, rBuf, rData_length); if(ret &lt; 0) { cout&lt;&lt; "received data failed"&lt;&lt; endl; close(connfd); delete[] rBuf; exit(1); } else { printf("Write Data[%d] to Socket\n", ret); } close(connfd); delete[] rBuf; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload