Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello

How can I send more data than 1MB to a TCP socket on the other end?
I can send 1MB, but I want to send 10, 100 or 500MB which doesnt work.
If I send more than 1MB I only get the 3-way TCP connection.
I want to send a large file so I can see with windump/tcptrace if I get alot of data that are being resent.
Or is there a better way way to do it?.

THE CLIENT SOCKET:
use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr '192.168.1.1', PeerPort => '7070', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; $file = "1MB.txt"; open (LOG, $file); @1MB = <LOG>; print $sock "@1MB"; close($sock);

THE SERVER SOCKET:

use IO::Socket; my $sock = new IO::Socket::INET ( LocalHost > '192.168.1.1', LocalPort => '7070', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $sock; my $new_sock = $sock->accept(); while(defined(<$new_sock>)) { print $_; } close($sock);

//Anders Andersson

Edit, BazB: added code tags.

Replies are listed 'Best First'.
Re: How can I send more than 1MB to a TCP socket?
by hawtin (Prior) on Oct 26, 2003 at 15:17 UTC

    Have you tried chunking up the input side?

    open (LOG, $file); binmode LOG; $linelen = 1024*1024; while(!eof(LOG)) { my($buf); $len = sysread LOG,$buf,$linelen; syswrite OUT,$buf,$len if($len); } close(LOG);

    The above is a chunk of code that worked for me, I assume that using file handles won't mess it up.

    Update:Fixed the confusion between LOG (from the question) and IN (from my previous experience) (thanks bart)

Re: How can I send more than 1MB to a TCP socket?
by sauoq (Abbot) on Oct 26, 2003 at 17:40 UTC
    I can send 1MB, but I want to send 10, 100 or 500MB which doesnt work.

    What do you mean by "it doesn't work"? There is no arbitrary limit on the amount of data you can send over a tcp connection. Could you be running into a RAM issue on your machine? Reading the whole file into an array in memory interpolating that array into a string isn't exactly memory efficient. You might want to write your client-side while loop as

    print while <LOG>;

    Or is there a better way way to do it?.

    Probably... netcat may be just what you are looking for. You might also find iperf helpful.

    Good luck.

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: How can I send more than 1MB to a TCP socket?
by vek (Prior) on Oct 26, 2003 at 17:42 UTC

    I'd be very careful about slurping the entire file into memory ( @1MB = <LOG>;) especially with the 100 & 500MB files. I'd probably use a while loop to iterate through the file and send smaller chunks of data one at a time to the server.

    -- vek --
Re: How can I send more than 1MB to a TCP socket?
by pg (Canon) on Oct 26, 2003 at 17:36 UTC

    FTP protocol fits in this situation better, and you can use some module like Net::FTP. You don't even need to worry about the chunks, as that module would handle this for you.

    Checked FTP spec, and theorbtwo is right about the restart (REST But FTP does not resolve one thing... If your connection is unstable and slow, and you lost your connection half way all the time, with the nature of FTP protocol, you have to restart from the very begining. If this is a real problem in your environment, then chunk it yourself, and establish your own little protocol to restart from where it is broken. (I am not talking about the resending done by TCP, that's a totally different level.)

      Er, as to FTP not supporting restarting, that isn't true. The client mearly has to give a proper REST command before the GET. OTOH, as another poster noted, TCP should deal just fine with arbitrarly long packets.


      Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

Re: How can I send more than 1MB to a TCP socket?
by pg (Canon) on Oct 26, 2003 at 18:12 UTC

    FTP protocol fits in this situation better, and you can use some module like Net::FTP. You don't even need to worry about the chunks, as that module would handle this for you.

    Checked FTP spec, therbtwo is rightBut FTP does not resolve one thing... If your connection is unstable, you may lost connection half way, with the nature of FTP protocol, you have to restart from the very begining. If this is a real problem in your environment, then chunk it yourself, and establish your own little protocol to restart from where it is broken. (I am not talking about the resending done by TCP, that's a totally different level.)