Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Example of using sockets/http

Reply
Thread Tools

Example of using sockets/http

 
 
Mike Curry
Guest
Posts: n/a
 
      12-31-2005
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.

 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      12-31-2005
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!!!


 
Reply With Quote
 
 
 
 
Mike Curry
Guest
Posts: n/a
 
      12-31-2005
How different is Ruby from C++, and is it as fast as C++? It also has
to be cross platform, windows/linux.

 
Reply With Quote
 
James Juno
Guest
Posts: n/a
 
      12-31-2005
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/
 
Reply With Quote
 
Moonlit
Guest
Posts: n/a
 
      01-01-2006
Hi,



Here is how to connect to Hostort 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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>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.
>



 
Reply With Quote
 
Moonlit
Guest
Posts: n/a
 
      01-01-2006
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$(E-Mail Removed)4all.nl...
> Hi,
>
>
>
> Here is how to connect to Hostort 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>>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.
>>

>
>



 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      01-01-2006
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!!!


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      01-01-2006
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!!!


 
Reply With Quote
 
Moonlit
Guest
Posts: n/a
 
      01-01-2006
Hi,

--


Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Phlip" <(E-Mail Removed)> wrote in message
news:KkGtf.558$(E-Mail Removed). ..
> 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!!!
>



 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      01-01-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help-Need Source code or example,control LCD using vhdl cehon VHDL 1 09-15-2005 07:27 AM
'example.com' == 'example.com.' => false... is this intended? Sam Roberts Ruby 15 02-07-2005 04:36 PM
Anybody has an example of a remote access VPN config using an IOS router? Eric Berthiaume Cisco 3 04-30-2004 09:15 AM
Example of using a stored procedure that returns a cursor George ASP .Net 2 01-14-2004 02:50 PM
Help example update sql db using XML dave ASP .Net 0 12-23-2003 07:31 PM



Advertisments