LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 08-18-2005, 05:57 AM   #1
ivanatora
Member
 
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
$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?
 
Old 08-18-2005, 11:04 AM   #2
ivanatora
Member
 
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
puffinman
Member
 
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
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,289

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
  


Reply


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


All times are GMT -5. The time now is 04:18 PM.

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