LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Non blocking sockets in Perl (https://www.linuxquestions.org/questions/programming-9/non-blocking-sockets-in-perl-354499/)

ivanatora 08-18-2005 05:57 AM

Non blocking sockets in Perl
 
How to make such a socket?:)
I understood that there are 2 ways:
using IO::Socket
using Fcntl ?
I'm sort of familiar with IO::Socket, and I write simple IRC bots/servers. I thought that
$sock->autoflush(1);
$| = 1;
makes the socket non-blocking, but a friend of mine just laughed and said that I'm wrong :) He said that I better use Fcntl, but it still could be done with IO::Socket... I'm confused.
Let's say according to me, a non-blocking socket allows other parts of the code to be executed in the same time where the socket is being read or wrote. This is how I do with IO::Socket:
Code:

IO::Socket -> create the socket
$sock->autoflush(1);
$| = 1;
while (<$sock>){
if ($sock =~ /some stuff/){
do(some other stuff);
}
}

So in that way I make simple query-response bots, but nothing more than that.
I want to make simple application: a timer bot. Let's say at every 30 seconds, it writes a line to the socked, and uses time() function. I don't know even where from to start. Somebody give me a direction please?

ivanatora 08-18-2005 11:04 AM

Anyone help?
At least show me a perl forum where I could ask :)

puffinman 08-19-2005 12:10 AM

You can use the four argument form of select() to check whether a handle (like a socket) has input waiting. However, IO::Handles (which include IO::Sockets) have the blocking function (for example, $sock->blocking(0)) to turn blocking on and off.

chrism01 08-19-2005 02:14 AM

Here's an example:

Code:

print "create_listening_socket()\n";

    # Listen to port
    $socket_errmsg = "";
    $cfg::listen_socket =
                IO::Socket::INET->new(LocalPort => $cfg::params{'RPS_PORT'},
                                      Type      => SOCK_STREAM,
                                      Proto    => 'tcp',
                                      Listen    => SOMAXCONN,
                                      Reuse    => 1,
                                      Blocking  => 0 )
                or $socket_errmsg =
                "Can't make RPS server Listen socket on port ".
                "$cfg::params{'RPS_PORT'}: $@\n";

    # Did we succeed ?
    if( $socket_errmsg )
    {
        send_email($socket_errmsg."\n$0 exiting with error.");
        exit_with_error($socket_errmsg);
    }

in a separate sub, do this:

    # Activate socket
    $read_handles = IO::Select->new();
    $read_handles->add($cfg::listen_socket);
    # Main loop: check reads/accepts, check writes, check ready to process

    # Loop forever, listening for incoming msgs
    while (1)
    {
        # check for new information on the connections we have
        ($new_read_handles) =
                        IO::Select->select($read_handles, undef, undef,
                                            $cfg::params{'LISTEN_WAIT'} );

        # anything to read or accept?
        foreach $client ( @$new_read_handles )
        {
            if( $client == $cfg::listen_socket )
            {
                # accept a new connection
                $new_client = $cfg::listen_socket->accept();
                $read_handles->add($new_client);
            }
            else
            {
                # Msg ready ?
                # read data
                $msg = '';
                $msg = <$client>;

                if( $msg )
                {
#DEBUG
#print "Listen: MSG-IN=$msg\n";

                    # Add to conn recs queue
                    $cfg::send_queue->enqueue($msg);
                }
                else
                {
                    # client has finished/closed
                    $read_handles->remove($client);
                    close $client;
                }
            }
        }

btw; non-blocking socket means socket doesn't wait for a msg if none is incoming ie code doesn't
get stuck waiting. You may or may not want this.
See Perl Cookbook chap 17


All times are GMT -5. The time now is 09:56 PM.