commit 10950fedf9f63bf6c2f301290eeae48f869a17f8 Author: Vasily Chekalkin Date: Mon Mar 30 20:33:12 2009 +1100 Use modern socket API in examples/io/http.pir diff --git a/examples/io/http.pir b/examples/io/http.pir new file mode 100644 index 0000000..e0035df --- /dev/null +++ b/examples/io/http.pir @@ -0,0 +1,68 @@ +# Copyright (C) 2001-2008, Parrot Foundation. +# $Id: http.pir 36833 2009-02-17 20:09:26Z allison $ + +=head1 NAME + +examples/io/http.pir - HTTP client + +=head1 SYNOPSIS + + % ./parrot examples/io/http.pir + +=head1 DESCRIPTION + +HTTP client, connects to WWW port and grabs a page (L). + +You should be running the echo service on your box (port 7). Be sure to +set C to 1 in F and rebuld Parrot or +the network layer won't exist. + +=cut + +.sub example :main + .local pmc sock + .local pmc address + .local string buf + .local int ret + .local int len + + # create the socket handle + print "Creating socket.\n" + sock = new 'Socket' + sock.'socket'(2, 1, 0) + unless sock goto ERR + + # Pack a sockaddr_in structure with IP and port + address = sock.'sockaddr'("www.ibm.com", 80) + print "Connecting to http://www.ibm.com:80\n" + ret = sock.'connect'(address) + print "connect returned " + print ret + print "\n" + + ret = sock.'send'("GET /us/en/ HTTP/1.0\r\nUser-agent: Parrot\r\n\r\n") +MORE: + buf = sock.'recv'() + ret = length buf + if ret <= 0 goto END + print buf + goto MORE +ERR: + print "Socket error\n" + end +END: + close sock + end +.end + +=head1 SEE ALSO + +F. + +=cut + +# Local Variables: +# mode: pir +# fill-column: 100 +# End: +# vim: expandtab shiftwidth=4 ft=pir: