View Full Version: Stock Quote

C++ Learning Community > C++ Creations > Stock Quote


Title: Stock Quote
Description: Get a live Stock quote (nearly)


myork - March 8, 2004 07:43 PM (GMT)
As everybody was donating code here, I though I should add somthing new.

Here is a quick command line tool I nocked up.
If run from the command line (you can do that in windows too)

It will take the first argument as a STOCK symbol and retrive the current price.

CODE

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

#include <iostream>
#include <string>

int main(int argc,char* argv[])
{

   if (argc < 2)
   {
       std::cout << "Please specify a stock symbol\n";
       exit(1);
   }

   struct sockaddr_in    address;
   address.sin_family    = AF_INET;
   address.sin_port      = htons(80);
   inet_aton("66.94.228.229",&address.sin_addr);

   int sockNo    = socket(PF_INET,SOCK_STREAM,0);
   int result    = connect(sockNo,(sockaddr*)&address,sizeof(struct sockaddr_in));

   if (result == 0)
   {
       /* We have a connection */
       std::string    getRequest("GET /d/quotes.csv?f=l1&s=");

       getRequest    += argv[1];
       getRequest    += "\r\n";
       write(sockNo,getRequest.c_str(),getRequest.length());

       char    buffer[2000];
       read(sockNo,buffer,2000);

       std::cout << "Stock Price of " << argv[1] << " is " << buffer;
   }
   else
   {
       std::cout << "Failed to Connect\n";
   }
   return(0);
}

FrozenKnight - March 8, 2004 07:56 PM (GMT)
Ok i see only one probelm you incuded <sys/socket.h> 3 times.
and can you give me a link to where you learned how to use sockets like that. every other example i've seen used uses winsock yours doesn't appear to.

myork - March 8, 2004 08:21 PM (GMT)
QUOTE
Ok i see only one probelm you incuded <sys/socket.h> 3 times.

That's from cutting and pasting from the man pages without looking.

Everything in the code is from reading the man pages on socket,connect.

Just for reference '66.94.228.229' is finance.yahoo.com and port 80 is the HTTP port where a web server us usually listening.

You get the same result by pointing your web browser at:

CODE
http://finance.yahoo.com/d/quotes.csv?f=l1&s=<PUT 4 LETTER SYMBO HERE>


or doing a:

CODE
telnet finance.yahoo.com 80
GET /d/quotes.csv?f=l1&s=<PUT 4 LETTER SYMBO HERE>


ih8censorship - March 8, 2004 11:29 PM (GMT)
yeah i would also like to know about why it doesnt use any WSA startup or anything--- i think its code for unix or something.that would make sense cause i dont have a couple of those headers unless there ones you made :blink:

in case anyone here uses vc++ and wants a real easyway to download a file off the net (i got reminded of this cause i used it to download stock quotes from yahoo too :lol: ) heres what you can do

CODE

#include <URLMON.h>
#pragma comment (lib,"URLMON.lib")
void main()
{
URLDownloadToFile(NULL,"http://finance.yahoo.com/d/quotes.csv?s=HDI+SGDE+BBI+MSFT+CPI&f=sl1","downloaded.TXT",0,NULL);
//i first learned this function after i dissasembled some spyware that got onto my computer....
}
and if anyone needs to know how to use the yahoo stock information this site will be very helpfull

myork - March 9, 2004 01:00 PM (GMT)
QUOTE
yahoo stock information this site will be very helpfull


Now that is a usfull site.
I had reverse engineered all the <x>, <x>1 and <x>2 codes but gave up after that.

QUOTE
i think its code for unix or something.that would make sense cause i dont have a couple of those headers unless there ones you made


Actually I wrote it on a windows box, but I had cygwin installed and used the g++ compiler. Which I recommend as a great place to get a free C compiler that is extremely close to standards compliance (I believe it is a lot closer than the MS compiler, but I don;t know that for a fact).




* Hosted for free by InvisionFree