Fellow Monks, I am new to sockets, in fact, so new I am still bright and shiny :-).

I have found a simple script using IO::Socket to connect across two machines.
The article says that if I only want to use it on one machine then just change the PeerAddr attribute to make it so (as Jean Luc would say).
Anyway here's the code for the receiver;

use IO::Socket; my $sock = new IO::Socket::INET ( LocalAddr => 'localhost', LocalPort => 10000, 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);
This works fine and starts OK, waiting for input. Here's the caller
use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => 'localhost', PeerPort => 10000, Proto => 'tcp', ); die "could not create socket\n" unless $sock; print $sock "Hello there\n"; close($sock);

So as soon as I start the caller script, the receiver script prints an error stating that $_ in the print statement was not initialised.

Or to be more precise;

Use of uninitialised value at ./receiver.pl line xx, <GEN1>, chnuk 1

Can someone please point me in the right direction to get this working? There must be something rather fundamental that both the author of the code and myself are not aware of.

Many thanks.
readey


In reply to Using IO::Socket by readey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.