From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of Greg Aiken Sent: 22 April 2009 00:36 To: perl-win32-users@listserv.activestate.com Subject: IO::Socket question (client receive - when # of bytes to be receivedis NOT known in advance)
> method 1 (below) does work to receive a response from a server. but requires I know in advance the number of > bytes to receive. > > I am wondering if something like method 2 may be used in the case of where one does NOT know in advance how > many bytes the server will be sending. ive attempted to try this using the code presented in method 2, but > this fails miserably. was hoping someone in the group had a working alternative method to share. > > > use IO::Socket; > > $http_get_request = << "HTTP_GET"; > GET /index.htm HTTP/1.1 > Host: google.com > Keep-Alive: timeout=15, max=60 > Connection: keep-alive > > HTTP_GET > > #setup client socket > $main::socket = new IO::Socket::INET > ( Proto => "tcp", PeerAddr => "google.com", PeerPort => "80", ); > $main::socket->autoflush(1); > > #send http1.1 (persistent connection) request for data > $main::socket->send($http_get_request); > > > #receive the http1.1 response... > > #method 1 > #this method works > #but requires i know how many bytes to receive > #which I dont always know - so i dont like this method > $main::socket->recv($response, 8192); > print $response; > > #method 2 > #cant something like this be done instead? > #i would prefer to use this pseudo-code method > #as there is no hard-coded number of bytes to receive > #is there a way to achieve this? > # while ( $main::socket->recv($block, 8192) ) { > # $response .= $block; > # } > # print $response; I hope you are not trying to reinvent that particular wheel. If so, see 'perldoc lwptut' and 'perldoc lwpcook'. At its simplest, it is little more than a one-liner. If you are just using that as an example, thenyour method 2 is a perfectly reasonable way to read an unknown amount of data from a socket. The while loop will read until the socket is closed. For anything more fine grained than that, or it is your responsibility to close the socket, then you will need to know how to recognise when to start and stop reading from the socket, which depends on the protocol that is being used. It can also depend on what else you are trying to do, if anything. HTH -- Brian Raven This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy. Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden. _______________________________________________ Perl-Win32-Users mailing list Perl-Win32-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs