title: Sending file over UDP in Perl
body: <p>Afternoon,</p>
  
  <p>I am writing a small UDP Client/Server in Perl and am having some issues sending files. I understand I need to break the file into chunks (datagrams) and send them to the server.</p>
  
  <p>I am stuck figuring out how to break the file up into datagrams and send them. As of now, I can establish a UDP connection just fine, the server reports when it is received a UDP packet. This is what I have so far, any help would be greatly appreciated!</p>
  
  <p>Server:</p>
  
  <pre><code>#!/usr/bin/perl
  
  # Flushing to STDOUT after each write
  $| = 1;
  
  use warnings;
  use strict;
  use IO::Socket;
  
  # Server side information
  my $listen_port     = 7070;
  my $protocal        = 'udp';
  my $received_data   = undef;
  
  # Creating UDP socket for server
  my $server = IO::Socket::INET-&gt;new (
      LocalPort   =&gt; $listen_port,
      Proto       =&gt; $protocal,
      Type        =&gt; SOCK_DGRAM
  ) or die "Socket could not be created, failed with error $!\n";
  
  print "Waiting for client connection on port $listen_port\n";
  
  open(FILE, "&gt;output.UDP")
    or die "File can not be opened: $!";
  
  while($server-&gt;recv($received_data, 1024)) {
      my $peer_address = $server-&gt;peerhost();
      my $peer_port    = $server-&gt;peerport();
      print "Message was received from: $peer_address, $peer_port\n";
      print FILE "$received_data";
  }
  close FILE;
  
  print "Closing socket...\n";
  $server-&gt;close();
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>#!/usr/bin/perl
  
  # Flushing to STDOUT after each write
  $| = 1;
  
  use warnings;
  use strict;
  use IO::Socket;
  
  # Client side information
  my $host        = 'apollo.cselabs.umn.edu';
  my $port        = 7070;
  my $protocal    = 'udp';
  my $datagram    = undef;
  
  # Creating UDP socket for client
  my $client = IO::Socket::INET-&gt;new (
      PeerAddr    =&gt; $host,
      PeerPort    =&gt; $port,
      Proto       =&gt; $protocal,
      Type        =&gt; SOCK_DGRAM
  ) or die "Socket could not be created, failed with error: $!\n";
  
  # Open and specified file
  open(FILE, "10MBfile.dat")
      or die "Fine can not be opened: $!";
  $client-&gt;send("test");
  
  # Send file line by line
  while (&lt;FILE&gt;) {
      $datagram = $_;
      $client-&gt;send("$datagram");
  }
  close FILE;
  # sleep(10);
  $client-&gt;close();
  </code></pre>