send and recive

This is a discussion on send and recive within the C++ Programming forums, part of the General Programming Boards category; Got a question in regards to reciving files from a remote site. Code: // connect to remote site - ok! ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    send and recive

    Got a question in regards to reciving files from a remote site.

    Code:
       // connect to remote site - ok!
    
       char buf[1024];
    
       char *msg = "GET / HTTP/1.0 \n\n";
    
       int count = send(socketfd, msg, strlen(msg), 0);
    	
       count = recv(socketfd, buf, sizeof(buf),0);
    The above code connects to a specific web server and ask for index.html file on the remote site - this works. The data is placed in the char array which is fine, but is this the best way of going about this ? 1024 Bytes is hardly enough to store a html file and I assume making a char buf[100240]; also is overkill. Is there a better method of storing the data ?

    Thanks for any input

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How about a temporary file?

    Also, I presume you know one call to recv() isn't guaranteed to get you everything the remote server sends you?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by Hammer
    How about a temporary file?

    Can I place the data directly into a file in the recv call or must I fist store it in a char buf[] and then copy it into a temp file ?


    Also, I presume you know one call to recv() isn't guaranteed to get you everything the remote server sends you?


    Yes, Im aware of that.
    Thanks for your response.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You:
    >must first store it in a char buf[] and then copy it into a temp file.
    So after a successful recv(), call an appropriate write function, then go back and recv() some more.

    At some point, you'll want to display the data (on the screen, I presume), you might want to wait until the transmission is complete, or you can display a partial file. It's up to you
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Thanks for your response Hammer.

    Tryed adding a fstream object to my code (Making a mfc app in visual c++).

    Code:
    #include <iostream.h>
    #include <fstream.h>
    
    // code
    
    std::fstream file;
    
    //code
    I then get the following errors :
    'std' : is not a class or namespace name
    'fstream' : undeclared identifier

    The code works fine apart from the std::fstream file; statment.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    6
    For the headers, use this:
    #include <iostream>
    #include <fstream>

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Thanks, fixed the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Send and Recive Information through the serial Port RS232
    By amigoloko in forum C# Programming
    Replies: 4
    Last Post: 04-14-2006, 01:34 PM
  2. Send information to this IP.
    By Queatrix in forum Networking/Device Communication
    Replies: 6
    Last Post: 02-03-2006, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21