#!/usr/bin/perl # # Fetch an image from a Hawking Tech HCN230G # # Paco Hope # Copyright (c) 2007 Paco Hope. # # This is free software; you can redistribute it and/or modify it under # the same terms as Perl itself. use IO::Socket::INET; sub fetchImage( $$$ ) { my $ip = shift; my $port = shift; my $filename = shift; my $imageBuf = ""; my $imageLen = 0; open( OUTFILE, ">$filename" ) or die "can't open $filename for writing"; my $socket = IO::Socket::INET->new( PeerAddr => $ip, PeerPort => $port, Proto => 'tcp', Timeout => 10, Type => SOCK_STREAM ) or die "Couldn't connect! $!"; print $socket "0110\n"; # first 2 bytes are 16-bit int with the size of the image recv $socket, $imageLen, 2, 0; $imageLen = unpack( "%n", $imageLen ); # next 2 bytes probably have meaning, but if the image is # less than 64k, I think we can ignore them. This code ignores # them. (We load them into the imageBuf, but overwrite them # later). recv $socket, $imageBuf, 2, 0; $imageBuf=""; my $got = 0; my $tempbuf=""; while( $got < $imageLen ) { # Try to get everything remaining in one request recv $socket, $tempbuf, ($imageLen - $got), 0 ; # add the number of bytes we received $got += (length $tempbuf); # append them to our overall image buffer $imageBuf .= $tempbuf; $tempbuf=""; } close($socket); print OUTFILE $imageBuf; close( OUTFILE ); } ## ## MAIN ## $cam1file="/home/users/paco/cam1.jpg"; $cam2file="/home/users/paco/cam2.jpg"; fetchImage( "10.1.101.249", 4321, $cam1file ); fetchImage( "10.1.101.248", 4321, $cam2file );