Here is your code in working form:

Client:

#!/usr/bin/perl use strict; use Socket; my $port = 2000; my $hostname = 'localhost'; my $protocol = getprotobyname ("tcp"); my $host = gethostbyname ($hostname); my $iaddr = sockaddr_in($port,$host); socket (SOCKET, PF_INET, SOCK_STREAM, $protocol) || die ("Error establ +ishing socket to [$hostname:$port]: $!"); connect (SOCKET, $iaddr); while (defined(my $line = <SOCKET>)) { chomp $line; print "Received from [$hostname:$port]: $line\n"; } close (SOCKET); __END__ Output will look like: [starky@freak bin]$ ./client-test-inet.pl Received from [localhost:2000]: Hello, world! Received from [localhost:2000]: [starky@freak bin]$

Server:

#!/usr/bin/perl use strict; use Socket; my $line = "Hello, world!\n"; my $port = 2000; my $protocol = getprotobyname ("tcp"); my $addr = INADDR_ANY; my $clientaddr; my $serveraddr = sockaddr_in($port,$addr); socket (SERVER, PF_INET, SOCK_STREAM, $protocol) || die ("Error establ +ishing socket to port [$port]: $!"); print "Binding to port [$port] ...\n"; bind (SERVER, $serveraddr) || die ("Can't bind: $!"); print "Listening on port [$port] ...\n"; listen (SERVER, SOMAXCONN) || die ("Can't listen: $!"); print "Accepting client connections on port [$port] ...\n"; ($clientaddr = accept (CLIENT, SERVER)) || die ("Can't accept: $!"); my ($clientport,$clientiaddr) = sockaddr_in($clientaddr); my $client = inet_ntoa($clientiaddr); print "Received connection from [$client:$clientport]. Saying hello . +..\n"; print CLIENT "$line\n"; close (CLIENT); close (SERVER); print "Done.\n"; __END__ Output will look like: [starky@freak bin]$ ./server-test.pl Binding to port [2000] ... Listening on port [2000] ... Accepting client connections on port [2000] ... Received connection from [127.0.0.1:55080]. Saying hello ... Done. [starky@freak bin]$

Some comments:

In regards to IO::Socket::INET, here is a simplified version of your client (that works just the same):

#!/usr/bin/perl use strict; use IO::Socket::INET; my ($port,$hostname,$protocol) = (2000,'localhost','tcp'); my $handle = IO::Socket::INET->new( Proto => $protocol, PeerAddr => $hostname, PeerPort => $port ) || die "Cannot connect to host [$hostname] on port [$port]: $!"; while (defined(my $line = <$handle>)) { chomp $line; print "Received from [$hostname:$port]: $line\n"; } $handle->close();
Hope this helps!


In reply to Re: problem with SOCKET by Starky
in thread problem with SOCKET by Anonymous Monk

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.