Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-18-2005, 05:57 AM   #1
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 31
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
$| = 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:
IO::Socket -> create the socket
$| = 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?
Old 08-18-2005, 11:04 AM   #2
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Original Poster
Rep: Reputation: 31
Anyone help?
At least show me a perl forum where I could ask
Old 08-19-2005, 12:10 AM   #3
Registered: Jan 2005
Location: Atlanta, GA
Distribution: Gentoo, Slackware
Posts: 217

Rep: Reputation: 30
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.
Old 08-19-2005, 02:14 AM   #4
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Here's an example:

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.");

in a separate sub, do this:

    # Activate socket
    $read_handles = IO::Select->new();
    # 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();
                # Msg ready ?
                # read data
                $msg = '';
                $msg = <$client>;

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

                    # Add to conn recs queue
                    # client has finished/closed
                    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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem w/ sockets, fdopen, and blocking. 95se Programming 3 11-17-2005 12:38 PM
PERL sockets bahadur Programming 4 05-08-2005 05:01 AM
Set/Unset Non-Blocking sockets? zer0python Programming 2 01-04-2004 01:31 AM
perl(Cwd) perl(File::Basename) perl(File::Copy) perl(strict)....What are those? Baldorg Linux - Software 1 11-09-2003 08:09 PM
chrooting apache v2 (php, ssl, perl support) ; perl configuration markus1982 Linux - Security 3 01-26-2003 06:15 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:51 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration