Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to buffer a file on a socket, anyone have a pm file

Status
Not open for further replies.

yourkeylady

Programmer
Jan 8, 2004
63
US
I'm trying to send images files and having trouble with it.
I know my trouble is in buffering, but I can't find examples of it to follow.

I've only studied sockets for a couple days, so any suggestions you have are welcome.

The trouble is it takes too long to read. And doesn't send.


Client socket
Code:
#!perl
# Client Program
use IO::Socket::INET;
print ">> Client Program <<\n";
use strict;
my $bufferlines = 10;
# Create a new socket
my $this_socket = new IO::Socket::INET->new(
    PeerPort => 1234,
    Proto    => 'udp',
    PeerAddr => 'localhost'
);

# Send messages
#          'file-C:\test\local.txt',
my @files = ( 'file-C:\test\b0.png' );
my $file = shift (@files);
$this_socket->send($file);
my $server_msg = '';
while (1) {
    $this_socket->recv( $server_msg, 128 );

    #        print $serve
    print "\n server_msg  ", $server_msg;
    if ( $server_msg eq 'next' ) {

        # the last file was saved
    print "\n calling get next file  ";

        &get_next_file;

    }       elsif ( $server_msg eq 'send' ) {
        print qq~\n next file '$file'~;
             print qq~\n calling read_file $file~;

            &read_file($file);



    }
    else {
      #$this_socket->send('quit');
      # shutdown is for forked processes, it's not needed for single connections
      #shutdown($this_socket,2);
      #  exit(1);
      print "\n  no reply?? $server_msg ";
    }
}

sub get_next_file {
print "\n # of files left ".@files;
    unless (@files) {

        # if no more files are to be sent
        print "\n this should not be reached Closing";

        # Send an empty message to server and exit
        print qq~\n This should not be reached, sending blank ~;
        $this_socket->send('quit');

    }
    else {
           $file = shift(@files);
        print qq~\n sending next file $file ~;
        $this_socket->send($file);


    }
}

sub read_file {
    my $pth = shift ();
    $pth =~ s/^file.//;
    my @content = ();
    if ( open( FIL1, $pth ) ) {
    	print "\n reading $pth ";
    	my $content = '';
       # @content = <FIL1>;
      	my $count = 0;
      	#  This is where I need help
      	while (<FIL1>) {
         	++$count;
         	#print $count;
           	$content .= $_;
         	 if($count > $bufferlines){
          		$count = 0;
          		#print $content;
            	$this_socket->send($content);
        		$content = '';
        	}
        }

      	close(FIL1);
      	print "\n handle closed";
        if(@content){$this_socket->send($content); }
    }
    else {
        print "\nCannot open $pth ";
    }

}

Server socket
Code:
#!perl
# Server Program
use strict;
use IO::Socket::INET;
print ">> Server Program <<\n";

# Create a new socket
my $MySocket = new IO::Socket::INET->new( LocalPort => 1234, Proto => 'udp' );

# Keep receiving messages from client
my $def_msg = "\nReceiving message from client.....\n";
my $file    = '';
my $reply   = '';
my $handle;
 my $ext;
while (1) {
    $MySocket->recv( $reply, 128 );
    # print "\nReceived message '", $reply, "'\n";
    if ( $reply =~ /^file-/ ) {
    	( my $junk, $file ) = split ( /\-/, $reply );
	    print "\n set file to $file";
         $ext = $file;
        $ext =~ s/.*\.//;
        chomp($ext);
        print "\n ext '" . $ext . "'";
        $file =~ s/\.$ext$//;
        if ( open( FIL, ">${file}_sent.$ext" ) ) {
            print "\n writing new file ${file}_sent.$ext\n";
            if ( $ext eq 'png' || $ext eq 'jpg' ) {
                print "\n setting binmode ";
                binmode FIL;
            }
            else {
                print "\n this is a text file ";
            }
            $handle = *FIL;
            $MySocket->send('send');
            next;
        }
        else {
            print "\n Cannot write ${file}_sent.$ext $! ";
        }
        $file = '';
        $MySocket->send('next');
    }

     elsif($reply eq 'quit'){
        #print "\n ?? reply is '" . $reply . "'\n";
        # shutdown($MySocket,2);
        # shutdown is for forked processes, it's not needed for single connections
        print "\n Client has exited!";
        exit 1;
    }
    elsif($handle){
        #if($ext eq 'txt'){
 #           print $reply;
        #} else{ print "\n.."; }
        #$reply =~ s/^.//;
        print $handle $reply;
         close(FIL);
        $handle = undef;
        print "\n wrote file content";
        $MySocket->send('next');

    }   else{
    print "\n stuck";
    $MySocket->send('next');
    }
}

Thanks
Tricia
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top