Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 03-21-2006, 11:36 AM   #1 (permalink)
joincamp
Registered User
 
Join Date: Feb 2006
Posts: 2
joincamp is on a distinguished road
socket recv() data out of order

i just started messing around with sockets and i'm having some problems with some HTTP recv()s. please excuse if my GET request is messy/wrong. I just basically stole firefox's GET request and added the proxy info to it.

as far as style and error correction and stuff goes, well it's not there. i was just trying to strip it down to it's bare bones and figure out what was wrong with it.

what i'm getting back is weird. i don't get the whole page, and it doesnt come in order. if a page was in sections like 0 1 2 3 4 5, i may get the message as 5 2 4 2 3. obviously something is fundamentally wrong with the way i'm recv()ing.

I was under the impression that the recv() function had to be looped until it wasn't receiving any more data. I tried it without the loop too, and I only get the first piece of the page(but at least it was the first piece and not a random piece).

My previous research has taught me that HTTP 1.1 usually uses chunked transfer encoding. how do I handle that? i thought it was by looping the recv() function, but what i'm doing is obviously not working.

If you use HTTP 1.0, is that a way around chunked encoding? it still seems like I only get the first X bytes.

i've messed with this for a while, and I either get only the first piece, or a disorderd page. any pointers?



attempt with HTTP 1.1 and a loop
Code:
void inet::GetHTTP()
{
    char msg[1280] = "GET http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2935 HTTP/1.1
\r\nHost: www.maxim-ic.com\r\nProxy-Authorization: Basic Y2F**********rZA==\r\nUser-Agent: 
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1
\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8
,image/png,*/*;q=0.5\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: 
gzip,deflate\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\nKeep-Alive: 300\r\nConnection: 
Close\r\n\r\n";    
    int msglen = strlen(msg);
    int bytes_sent = send(s, msg, msglen, 0);                     
    cout << "Bytes sent: " << bytes_sent << endl;
    cout << "Request sent: " << msg << endl;

    char buf[262144] = {0};
    int i = 1;
    while(i) i=recv(s, buf, 262144, 0);
    cout << "\nData Recieved\n\n" << buf << endl;
}

Last edited by Valmont; 03-22-2006 at 06:43 AM.
joincamp is offline   Reply With Quote
Old 04-18-2006, 10:24 PM   #2 (permalink)
AssKoala
Anti-Zealot
 
AssKoala's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 72
AssKoala is on a distinguished road
Send a message via AIM to AssKoala Send a message via MSN to AssKoala Send a message via Yahoo to AssKoala
Are you somehow establishing a UDP connection?

You should not be receiving out of order. My webserver (had to write it for an Operating System class -- the web part was not a class aspect..) simply receives with a:

Code:
while (recv(clntSocket, buffer, RECV_SIZE, 0) > 0);
You should not be receiving out of order, or any duplicates, with TCP. However, from the sounds of it, it sounds exactly like a UDP type issue.

As for your msg, you don't need all that extra fluff if you're just testing simple stuff:

Code:
char *msg = "GET http://www.maxim-ic.com/quick_view2.cfm/qv_pk/2935 HTTP/1.1\r\nHost: www.maxim-ic.com\r\n\r\n"
Should work just fine (You only need the HTTP version, the GET, the item, and the Host).
__________________
If you always think like an expert, you'll always be a beginner. | "A handful of knowledgeable people is more effective than an army of fools" -Writing Secure Code, 2nd Ed.
AssKoala is offline   Reply With Quote
Old 04-19-2006, 07:41 AM   #3 (permalink)
kyoryu
Registered User
 
Join Date: Apr 2003
Posts: 34
kyoryu is on a distinguished road
Unless I'm mistaken (and I may well be), it appears that you're overwriting your data with each receive call. You either need to memcpy the data received into the final output buffer, or use an offset

Code:
int result;
result = recv(clntSocket, buffer+bytes_read_so_far, RECV_SIZE - bytes_read_so_far, 0);
bytes_read_so_far += result;
to put the new data at the end of the data received.

Note: the example code doesn't check the returned result.
kyoryu is offline   Reply With Quote
Old 04-19-2006, 10:29 AM   #4 (permalink)
AssKoala
Anti-Zealot
 
AssKoala's Avatar
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 72
AssKoala is on a distinguished road
Send a message via AIM to AssKoala Send a message via MSN to AssKoala Send a message via Yahoo to AssKoala
I didn't notice that, you (the op) shouldn't be using that in recv buffer to store anything, it's temporary space that you should be moving to a different buffer (generally dynamic).
__________________
If you always think like an expert, you'll always be a beginner. | "A handful of knowledgeable people is more effective than an army of fools" -Writing Secure Code, 2nd Ed.
AssKoala is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Order Multiple Items BUFFY PHP 12 10-20-2005 08:14 AM
Reading chunks of data in a servlet??? j.gohel Java 2 09-10-2005 01:40 PM
? About data types... slashdot Standard C, C++ 2 04-15-2005 09:57 AM
someone please help kickerman97 Java 3 10-19-2004 03:19 PM


All times are GMT -8. The time now is 04:02 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting