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

Please forgive the heft of this post by a socket-scripting-newbie. This is a simple socket scripting 101 problem that I've been on for two days. Both the client and server scripts reside on my Mac.

I start the server script up, it prints the server host and port and then waits at the "accept" call. I run the client script, the server script prints the client host and port (which I've removed for brevity) and (apparently) waits for text input from the client.

The client gets and prints the welcome message from the server. I then type text expecting the server to accept it, reverse it, and send it back, but the server script never sees anything at the while ($m = <NEWSOCK>) statement.

The client script is invoked by ./clientScript localhost 7654

Incidentally, I want the server script to eventually run as a daemon on a Solaris server, but first things first.

Thanks very much for any responses, I really do appreciate your attention.

- Dan

#--------------------------------------------------------------------- +---- #SERVER SCRIPT #!/usr/bin/perl -Tw require 5.002; use Socket; #- ReverseEchoer.pl ((c) 1999 Dr. Herong Yang example) $domain = 2; # Internet domain $type = 1; # Sequenced, reliable, two-way connection, byte streams socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp')); $host = pack('C4', 0, 0, 0, 0); # Local wildcard host id: 0.0.0.0 $port = 7654; $address = pack('S n a4 x8', $domain, $port, $host); bind(SOCK, $address); $queueSize = 5; # Queue up to 5 connections listen(SOCK, $queueSize); $cAddress = accept(NEWSOCK, SOCK); ($cDomain, $cPort, $cHost) = unpack('S n a4 x8', $cAddress); select(NEWSOCK); $| = 1; select(STDOUT); print NEWSOCK "Welcome to Reverse Echo Server.\r\n"; ##### print this +to the client (COMMENT 1) while ($m=<NEWSOCK>) { ##### wait for input from client, but nothing e +ver comes (COMMENT 5) $m =~ s/\n|\r//g; last if ($m eq "."); $m = reverse($m); print NEWSOCK "$m\r\n"; } close(NEWSOCK); #--------------------------------------------------------------------- +---- #CLIENT SCRIPT #!/usr/bin/perl -w require 5.002; use strict; use Socket; my ($remote, $port, $iaddr, $paddr, $proto, $line, $m); $remote = shift || 'localhost'; $port = shift || 7654; if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') } die "No port" unless $port; $iaddr = inet_aton($remote) or die "no host: $remote"; $paddr = sockaddr_in($port, $iaddr); $proto = getprotobyname('tcp'); socket(SOCK, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; connect(SOCK, $paddr) or die "connect: $!"; while ($line = <SOCK>) { print $line; ##### the Welcome line appears here (COMMENT 2) $m = <STDIN>; ##### I type "abc" here... (COMMENT 3) print SOCK $m; ##### ...send it to the server socket script (COMMENT + 4) } close(SOCK) or die "close: $!";

Replies are listed 'Best First'.
Re: Socket Communication-duh
by almut (Canon) on Aug 13, 2007 at 17:30 UTC

    You need to turn on autoflushing in the client, too ($| = 1, just like you have it in the server).

      almut - Thank you very much - that was it, it works fine now!

      I would change my username "dant" to "duh" but the settings page won't let me.

      Much obliged!

Re: Socket Communication-duh
by aquarium (Curate) on Aug 14, 2007 at 00:44 UTC
    btw netcat is an excellent command line utility to test client and server TCP and UDP.
    the hardest line to type correctly is: stty erase ^H