Connecting Tech Pros Worldwide Help | Site Map

Example of using sockets/http

Mike Curry
Guest
 
Posts: n/a
#1: Dec 31 '05
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.

Phlip
Guest
 
Posts: n/a
#2: Dec 31 '05

re: Example of using sockets/http


Mike Curry wrote:
[color=blue]
>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.[/color]

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!!!


Mike Curry
Guest
 
Posts: n/a
#3: Dec 31 '05

re: Example of using sockets/http


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

James Juno
Guest
 
Posts: n/a
#4: Dec 31 '05

re: Example of using sockets/http


Mike Curry wrote:[color=blue]
> How different is Ruby from C++, and is it as fast as C++? It also has
> to be cross platform, windows/linux.
>[/color]
http://www.ruby-lang.org/en/
Moonlit
Guest
 
Posts: n/a
#5: Jan 1 '06

re: Example of using sockets/http


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" <mmcurry@rogers.com> wrote in message
news:1136059932.685132.102570@g43g2000cwa.googlegr oups.com...[color=blue]
>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.
>[/color]


Moonlit
Guest
 
Posts: n/a
#6: Jan 1 '06

re: Example of using sockets/http


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$e4fe514c@news.xs4all.nl...[color=blue]
> 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" <mmcurry@rogers.com> wrote in message
> news:1136059932.685132.102570@g43g2000cwa.googlegr oups.com...[color=green]
>>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.
>>[/color]
>
>[/color]


Phlip
Guest
 
Posts: n/a
#7: Jan 1 '06

re: Example of using sockets/http


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:
[color=blue]
> How different is Ruby from C++,[/color]

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.)
[color=blue]
> and is it as fast as C++?[/color]

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.
[color=blue]
> It also has
> to be cross platform, windows/linux.[/color]

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
Guest
 
Posts: n/a
#8: Jan 1 '06

re: Example of using sockets/http


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. ;-)
[color=blue]
> Looking at the code I forgot:
>
> #define INVALID_SOCKET -1[/color]

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!!!


Moonlit
Guest
 
Posts: n/a
#9: Jan 1 '06

re: Example of using sockets/http


Hi,

--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Phlip" <phlipcpp@yahoo.com> wrote in message
news:KkGtf.558$801.188@newssvr30.news.prodigy.com. ..[color=blue]
> 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. ;-)
>[color=green]
>> Looking at the code I forgot:
>>
>> #define INVALID_SOCKET -1[/color]
>
> Hmm. Sometimes we claim that all such #defines should be 'static const
> int' instead.[/color]

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.[color=blue]
>
> Can anyone think of a linguistic reason that this must be a #define?
>
> --
> Phlip
> http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
>[/color]


Maxim Yegorushkin
Guest
 
Posts: n/a
#10: Jan 1 '06

re: Example of using sockets/http



Mike Curry wrote:[color=blue]
> 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.[/color]

Sockets might be to complicated for a starter. Try libcurl.
http://curl.planetmirror.com/libcurl/c/example.html

Closed Thread