Example of using sockets/http

Discussion in 'C++' started by Mike Curry, Dec 31, 2005.

  1. Mike Curry

    Mike Curry Guest

    I am looking for a simple sample of using sockets to download a page
    into a string. If anyone knows where I could get a sample, or post one
    here, that would be great.
     
    Mike Curry, Dec 31, 2005
    #1
    1. Advertising

  2. Mike Curry

    Phlip Guest

    Mike Curry wrote:

    >I am looking for a simple sample of using sockets to download a page
    > into a string. If anyone knows where I could get a sample, or post one
    > here, that would be great.


    Why are you using C++?

    Scripting languages like Ruby come with HTTP libraries to do that in a
    couple function calls.

    You could also shell to wget or lynx like this:

    system("lynx -source http://myserver/mypage > aFile.html");

    now read aFile.html

    If you write that from scratch, with sockets, you will have to debug it for
    various protocols and network conditions. Just re-use what's out there...

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
     
    Phlip, Dec 31, 2005
    #2
    1. Advertising

  3. Mike Curry

    Mike Curry Guest

    How different is Ruby from C++, and is it as fast as C++? It also has
    to be cross platform, windows/linux.
     
    Mike Curry, Dec 31, 2005
    #3
  4. Mike Curry

    James Juno Guest

    Mike Curry wrote:
    > How different is Ruby from C++, and is it as fast as C++? It also has
    > to be cross platform, windows/linux.
    >

    http://www.ruby-lang.org/en/
     
    James Juno, Dec 31, 2005
    #4
  5. Mike Curry

    Moonlit Guest

    Hi,



    Here is how to connect to Host:port i.e.string Host = "www.host.com"; int
    Port = 80;

    Note:

    Socket is an int on unix type of systems but SOCKET on windows.

    #define closesocket as close on unix type systems.

    Do Connect. The code is pretty much cross platform (I have used it on linux
    and lot's of unices as well as windows) You have to check your OS for the
    headers to include though. On MS-Windows platforms you have to initialize
    the stack (from the top of my head WSAStartup or somethig like that) and
    during linktime you have include the appropriate library (I believe inet.lib
    but just search for select on MSDN and scroll down). For Sun-os you also
    have to add some libraries (just do a man -k select or something like that
    to get the correct manual pages).


    bool CBaseSocket::Init()
    {
    struct protoent *PE = getprotobyname( "tcp" );
    if( PE ) ProtoNumber = PE->p_proto;
    else ProtoNumber = 6; // Otherwise select proto anyway

    struct hostent *pHE;

    //memset( &sin, 0, sizeof sin );
    sin.sin_family = AF_INET;
    sin.sin_port = htons( Port );

    // Try as dotted notation e.g. 192.168.1.1
    sin.sin_addr.s_addr = inet_addr( Host.c_str() );
    if( sin.sin_addr.s_addr != -1 )return true;

    // Try as labels e.g. moonlit.xs4all.nl
    if( ( pHE = gethostbyname( Host.c_str() ) ) == NULL )
    {
    LastError = string( "Couldn't get name" );
    return false;
    }

    sin.sin_addr.s_addr = *reinterpret_cast<unsigned long*>(
    *pHE->h_addr_list );

    return true;
    }


    bool CBaseSocket::Connect()
    {
    bool RetVal = true;

    if( !Init() )
    {
    LastError = string( "Couldn't convert address" );
    Log << Level2 << LastError << End;
    return false;
    }

    if( ( Socket = static_cast<SOCKET>( socket( AF_INET, SOCK_STREAM, 0 ) ) )
    == INVALID_SOCKET )
    {
    LastError = string( "Error creating client Socket" );
    Log << Level2 << LastError << End;
    return false;
    }

    if( connect( Socket, (struct sockaddr*)&sin, sizeof( sin ) ) ==
    SOCKET_ERROR )
    {
    LastError = string( "Couldn't connect to" );
    Log << Level2 << LastError << End;
    closesocket( Socket );
    return false;
    }

    unsigned long True = 1;
    ioctlsocket( Socket, FIONBIO, &True ); // Hope this works (undocumented)?
    setsockopt( Socket, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<char*>(
    &True ), sizeof( True ) );

    return true;
    }






















    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "Mike Curry" <> wrote in message
    news:...
    >I am looking for a simple sample of using sockets to download a page
    > into a string. If anyone knows where I could get a sample, or post one
    > here, that would be great.
    >
     
    Moonlit, Jan 1, 2006
    #5
  6. Mike Curry

    Moonlit Guest

    Looking at the code I forgot:

    #define INVALID_SOCKET -1

    on unix systems

    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "Moonlit" <news moonlit xs4all nl> wrote in message
    news:43b71d8a$0$11064$4all.nl...
    > Hi,
    >
    >
    >
    > Here is how to connect to Host:port i.e.string Host = "www.host.com";
    > int Port = 80;
    >
    > Note:
    >
    > Socket is an int on unix type of systems but SOCKET on windows.
    >
    > #define closesocket as close on unix type systems.
    >
    > Do Connect. The code is pretty much cross platform (I have used it on
    > linux and lot's of unices as well as windows) You have to check your OS
    > for the headers to include though. On MS-Windows platforms you have to
    > initialize the stack (from the top of my head WSAStartup or somethig like
    > that) and during linktime you have include the appropriate library (I
    > believe inet.lib but just search for select on MSDN and scroll down). For
    > Sun-os you also have to add some libraries (just do a man -k select or
    > something like that to get the correct manual pages).
    >
    >
    > bool CBaseSocket::Init()
    > {
    > struct protoent *PE = getprotobyname( "tcp" );
    > if( PE ) ProtoNumber = PE->p_proto;
    > else ProtoNumber = 6; // Otherwise select proto anyway
    >
    > struct hostent *pHE;
    >
    > //memset( &sin, 0, sizeof sin );
    > sin.sin_family = AF_INET;
    > sin.sin_port = htons( Port );
    >
    > // Try as dotted notation e.g. 192.168.1.1
    > sin.sin_addr.s_addr = inet_addr( Host.c_str() );
    > if( sin.sin_addr.s_addr != -1 )return true;
    >
    > // Try as labels e.g. moonlit.xs4all.nl
    > if( ( pHE = gethostbyname( Host.c_str() ) ) == NULL )
    > {
    > LastError = string( "Couldn't get name" );
    > return false;
    > }
    >
    > sin.sin_addr.s_addr = *reinterpret_cast<unsigned long*>(
    > *pHE->h_addr_list );
    >
    > return true;
    > }
    >
    >
    > bool CBaseSocket::Connect()
    > {
    > bool RetVal = true;
    >
    > if( !Init() )
    > {
    > LastError = string( "Couldn't convert address" );
    > Log << Level2 << LastError << End;
    > return false;
    > }
    >
    > if( ( Socket = static_cast<SOCKET>( socket( AF_INET, SOCK_STREAM, 0 ) ) )
    > == INVALID_SOCKET )
    > {
    > LastError = string( "Error creating client Socket" );
    > Log << Level2 << LastError << End;
    > return false;
    > }
    >
    > if( connect( Socket, (struct sockaddr*)&sin, sizeof( sin ) ) ==
    > SOCKET_ERROR )
    > {
    > LastError = string( "Couldn't connect to" );
    > Log << Level2 << LastError << End;
    > closesocket( Socket );
    > return false;
    > }
    >
    > unsigned long True = 1;
    > ioctlsocket( Socket, FIONBIO, &True ); // Hope this works (undocumented)?
    > setsockopt( Socket, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<char*>(
    > &True ), sizeof( True ) );
    >
    > return true;
    > }
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    >
    > --
    >
    >
    > Regards, Ron AF Greve
    >
    > http://moonlit.xs4all.nl
    >
    > "Mike Curry" <> wrote in message
    > news:...
    >>I am looking for a simple sample of using sockets to download a page
    >> into a string. If anyone knows where I could get a sample, or post one
    >> here, that would be great.
    >>

    >
    >
     
    Moonlit, Jan 1, 2006
    #6
  7. Mike Curry

    Phlip Guest

    The major topic here is simply "horses for courses". C++ is a good student
    language and good system language. Many simpler and higher-level languages
    are better for the high-level glue code we write to bind systems together.

    Mike Curry wrote:

    > How different is Ruby from C++,


    Ruby (similar to Python, Perl, etc.) comes from only one distribution, so
    its maintainers can bundle any library works. (Also, such libraries are
    suspiciously easy to write. Ruby bundles with Webrick, for example, which is
    a small and full-featured web server.)

    > and is it as fast as C++?


    You can write complex programs in Ruby much faster than you can in C++.

    And a program that uses HTTP will be IO-bound by nature, so the mild
    performance hit using an interpreted language is negligible.

    > It also has
    > to be cross platform, windows/linux.


    Because Ruby comes from one distribution, the maintainers can add libraries
    to it that are as portable as it is.

    Ruby can work on any platform that supports Standard C, and it has
    installers for all the major Linuces and for Windows.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
     
    Phlip, Jan 1, 2006
    #7
  8. Mike Curry

    Phlip Guest

    Moonlit wrote:

    Thank you! That's the example which would be 2-4 lines of code in a higher
    level language, but it's always nice to know I can Google for this post when
    I need C++.

    Anecdote: Just last fall I put in similar code to a C++ script I wrote
    (against my advice here), and have since simplified everything else enough
    to take it out. ;-)

    > Looking at the code I forgot:
    >
    > #define INVALID_SOCKET -1


    Hmm. Sometimes we claim that all such #defines should be 'static const int'
    instead.

    Can anyone think of a linguistic reason that this must be a #define?

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
     
    Phlip, Jan 1, 2006
    #8
  9. Mike Curry

    Moonlit Guest

    Hi,

    --


    Regards, Ron AF Greve

    http://moonlit.xs4all.nl

    "Phlip" <> wrote in message
    news:KkGtf.558$...
    > Moonlit wrote:
    >
    > Thank you! That's the example which would be 2-4 lines of code in a higher
    > level language, but it's always nice to know I can Google for this post
    > when I need C++.
    >
    > Anecdote: Just last fall I put in similar code to a C++ script I wrote
    > (against my advice here), and have since simplified everything else enough
    > to take it out. ;-)
    >
    >> Looking at the code I forgot:
    >>
    >> #define INVALID_SOCKET -1

    >
    > Hmm. Sometimes we claim that all such #defines should be 'static const
    > int' instead.


    Oops, yes you are absolutely right. I just checked the complete class and
    saw that I actually had them both and used both :-(. Ok, time to clean up
    some stuff I guess, thanks for triggering me.
    >
    > Can anyone think of a linguistic reason that this must be a #define?
    >
    > --
    > Phlip
    > http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
    >
     
    Moonlit, Jan 1, 2006
    #9
  10. Mike Curry wrote:
    > I am looking for a simple sample of using sockets to download a page
    > into a string. If anyone knows where I could get a sample, or post one
    > here, that would be great.


    Sockets might be to complicated for a starter. Try libcurl.
    http://curl.planetmirror.com/libcurl/c/example.html
     
    Maxim Yegorushkin, Jan 1, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    4
    Views:
    484
    Roedy Green
    Jul 28, 2003
  2. dp
    Replies:
    0
    Views:
    515
  3. BobSmith
    Replies:
    2
    Views:
    656
    BobSmith
    Aug 8, 2005
  4. Ishwar Rattan

    timeout and sockets example request??

    Ishwar Rattan, Jul 22, 2004, in forum: Python
    Replies:
    3
    Views:
    3,593
    Dave Brueck
    Jul 23, 2004
  5. Replies:
    0
    Views:
    236
Loading...

Share This Page