http://www.perlmonks.org?node_id=234203


in reply to problem with SOCKET

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!