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

I have a service that listens for commands, when a command is received it echo's it back to the client and in a log. It works fine when I telnet from the terminal, but i'm having problems scripting it. It doesn't seem to write to the socket. I can read from the socket, but I can't write to it. Whatever I print to the socket from the script, it should be echoed back to me and logged on the server, but I don't see it in either place. Upon connecting to the server, I receive:
Trying to aquire lock on log Connection Established Lock aquired
Then I send a string ending in a ; FYI, in the server, $/ = ";"; I've tried just about everything I can think of... Can anyone out there spot the error? Thanks
#!/usr/bin/perl -w use strict; use IO::Socket::INET; my $text = 'Hello;'; my $host = "10.10.30.130"; my $port = "5678"; my $sock = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port, Proto => 'tcp' ); $sock or die "Can't connect to $host:$port\n"; #$sock->autoflush(1); my $input = ""; my $char = ""; until ($input =~ /Lock aquired\s*$/){ sysread($sock, $char, 1); $input .= $char; } print $input; #$sock->send($text ); #syswrite $sock,$text ; print $sock $text ; $sock->shutdown(1); close $sock; exit;

Replies are listed 'Best First'.
Re: Read and write to socket
by BrowserUk (Patriarch) on Oct 16, 2007 at 21:35 UTC
      I didn't think it was necessary since the exchange works perfectly when I do it manually using telnet. The server code part is working as expected, it's printing to a socket with the above code that I am asking about.
Re: Read and write to socket
by zentara (Cardinal) on Oct 17, 2007 at 11:52 UTC
    Try this bi-directional client, it acts like telnet.
    #!/usr/bin/perl -w use strict; use IO::Socket; my ( $host, $port, $kidpid, $handle, $line ); ( $host, $port ) = ('192.168.0.1',1200); my $name = shift || ''; if($name eq ''){print "What's your name?\n"} chomp ($name = <>); # create a tcp connection to the specified host and port $handle = IO::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => $port ) or die "can't connect to port $port on $host: $!"; $handle->autoflush(1); # so output gets there right away print STDERR "[Connected to $host:$port]\n"; # split the program into two processes, identical twins die "can't fork: $!" unless defined( $kidpid = fork() ); # the if{} block runs only in the parent process if ($kidpid) { # copy the socket to standard output while ( defined( $line = <$handle> ) ) { print STDOUT $line; } kill( "TERM", $kidpid ); # send SIGTERM to child } # the else{} block runs only in the child process else { # copy standard input to the socket while ( defined( $line = <STDIN> ) ) { print $handle "$name->$line"; } }

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum
Re: Read and write to socket
by ulfwil (Initiate) on May 11, 2011 at 06:44 UTC
    Hello, Did you ever found the problem in your code regarding this problem?