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: $!";

In reply to Socket Communication-duh by dant

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.