char recvbuffer[1024];
int rsult=1;
while(rsult!=0)
{
rsult = recv(theSocket,recvbuffer,1024,0);
if(rsult!=0)
send(theClient,recvbuffer,1024,0);
}
Thanks
Receiving all data with winsock
I''m writing an http proxy server and here''s the situation. I want to be able to send my request header to the server requested by the client. After that, I need to recieve all the data from that server and relay it back to the client. If I''ve only got a fixed size buffer (1024 bytes), how can I receive all of what''s being sent to me by the server using recv?
Here''s the code that I''m using, but it just isn''t working (order of data received is all wrong, hangs when there''s no data to be recieved because it''s blocking sockets)
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
You can''t know how much data there is unless you parse the HTTP request yourself. And you don''t know how long the HTTP header field is, the length is only for the incoming content.
Sockets NEVER know how much data there is to receive, you design around it.
Sockets NEVER know how much data there is to receive, you design around it.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
ok, so I''ll parse the content-length field in the response header. Does the content-length field account for the size of the response header as well as the rest of the data?
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
quote:
And you don''t know how long the HTTP header field is, the length is only for the incoming content.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
er.. heh. Didn''t notice that :-\
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
Even if you did know how much was supposed to be coming, you''d be in trouble if (for some reason) not all of it arrived. You just have to keep listening until it''s all done, buffering as you go.
Ok, so here''s what I''ve done. It loops through recieve now, over and over. Doesn''t determine how much to recieve based on content-length or anything. The problem that I''ve ran into is that in the loop, it receives the last packet over and over. How do I tell when I''m done receiving the data? I suppose I could check to see if the received data is the same as the last received data, but I sorta don''t want to do this because Its possible that an HTML file could have a string of same characters larger than my buffer size * 2.
pixelwrench.com | [email="matt[nospam]@pixelwrench[nospam]com"]email[/email] lethalhamster: gamedev keeps taking my money, but im too lazy to not let them
recv should return something different when the connection''s closed. I believe it''s 0 for a graceful disconnect and something less than 0 for an ungraceful one, but you''ll want to check that in the docs. I once heard that some web servers/browsers keep the socket open rather than close it, but I know nothing about how that works; consult the relevant RFC I guess.
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement