473,396 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Example of using sockets/http

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.

Dec 31 '05 #1
9 6621
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!!!
Dec 31 '05 #2
How different is Ruby from C++, and is it as fast as C++? It also has
to be cross platform, windows/linux.

Dec 31 '05 #3
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/
Dec 31 '05 #4
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" <mm*****@rogers.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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.

Jan 1 '06 #5
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:43***********************@news.xs4all.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" <mm*****@rogers.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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.


Jan 1 '06 #6
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!!!
Jan 1 '06 #7
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!!!
Jan 1 '06 #8
Hi,

--
Regards, Ron AF Greve

http://moonlit.xs4all.nl

"Phlip" <ph******@yahoo.com> wrote in message
news:Kk***************@newssvr30.news.prodigy.com. ..
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!!!

Jan 1 '06 #9

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

Jan 1 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Snyke | last post by:
I'm currently looking for a good C++ Wrapper for sockets. I've already tried http://www.libsockets.net/ but I have some problems compiling it on Windows. Does anybody know a smaller (this library...
10
by: Cory Nelson | last post by:
I've created a new C++ sockets library - small, cross-platform, IPv4 and IPv6. If anyone would like to critique it, I'd appreciate it. Doesn't support the more exotic protocols - only TCP and UDP....
4
by: prashantkumar1982 | last post by:
I am using the select call to read from many sockets. I don't want to call read on every socket to check if it is closed, as it defeats the purpose of using the select call. Is there any way to...
3
by: Mr Mind - Lion | last post by:
Please send me some sample for using Raw sockets. And also clearly specify that how can I set the ip header of the protocol. Please help me. Thanx.
2
by: 1388-2/HB | last post by:
I've got a small sockets application where I communicate with a web server via async sockets method. Using the ASCIIEncoding Class, I convert strings to byte arrays (and vice versa) in...
4
by: Marco Meoni | last post by:
Hi. I read the Gordon McMillan's "Socket Programming Howto". I tried to use the example in this howto but this doesn't work. The code is class mysocket: '''classe solamente dimostrativa -...
2
by: dariophoenix | last post by:
Hi, I am trying to encapsulate Linux sockets and POSIX threads in C++ classes (I work in Knoppix, using KDevelop). Since sockets and threads are new to me, I searched for example code and found...
4
by: billie | last post by:
Hi all. I'm (re)writing an FTP server application by using asyncore/asynchat modules. FTP tipically got two different channels: command and data. I'm succesfully managing command channel through...
6
by: David | last post by:
http://msdn2.microsoft.com/en-us/library/bew39x2a(VS.80).aspx I was looking at above link and I just don't see the advantage of this. The main thread is just stopping and waiting for each of the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.