Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: perl client for Socket.io

by zentara (Archbishop)
on Nov 30, 2013 at 17:03 UTC ( #1065066=note: print w/replies, xml ) Need Help??


in reply to perl client for Socket.io

The old way to do a bi-directional socket, and usually the most foolproof, is to use a forking model. One fork watches for input fron STDIN to send, and the other fork receives from the socket. Try it.
#!/usr/bin/perl -w use strict; use IO::Socket; my ( $host, $port, $kidpid, $handle, $line ); #unless ( @ARGV == 2 ) { die "usage: $0 host port" } ( $host, $port ) = @ARGV || ('localhost',4444); # 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 $line; } }

Replies are listed 'Best First'.
Re^2: perl client for Socket.io
by educated_foo (Vicar) on Nov 30, 2013 at 20:35 UTC
    The OP's question is unfortunately not about sockets, but instead about a poorly-named Javascript library.
      Hi, I'm just getting into the javascript and node.js end of things. My preliminary understanding is that node.js is a server ( basically a socket) that communicates with javascript running in the browser. So theoretically, shouldn't you be able to communicate directly with node.js thru a socket program, assuming you send the right data to node.js. Just as though you can communicate with a standard web server with a socket program, as arduous as it may be.

      I'm not really a human, but I play one on earth.
      Old Perl Programmer Haiku ................... flash japh
Re^2: perl client for Socket.io
by TorontoJim (Sexton) on Oct 20, 2017 at 23:19 UTC

    I'd like to try this for a project I'm working on. However, I'm not clear about using it.

    That is to say, I'm not sure how to invoke it. How do I invoke the script and have it stay running to exchange form updates between the script on the server, and the web page on the client?

    Once invoked, how do you shut it down once that page is done with it?

    I'm not new to Perl, but I am new to Sockets of any kind. I have a project with a page that holds scheduling information and it needs to be accessible/updateable by more than one user at a time.

    Even if you can just point me to a good resource that explains the above, I'm happy to do the legwork. I just haven't read anything yet that answers those questions. Even the popular socket.io doesn't do much to explain that part of it. It only gives examples of a pseudo-server running from the client, not from a web server.

    Thank you for your time.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1065066]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2018-07-23 06:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (459 votes). Check out past polls.

    Notices?