Example of using sockets/http

M

Mike Curry

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.
 
P

Phlip

Mike said:
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...
 
M

Mike Curry

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

Moonlit

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
 
P

Phlip

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 said:
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.
 
P

Phlip

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?
 
M

Moonlit

Hi,

--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

Phlip said:
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. ;-)


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top