Create socket

From Botnix

create_socket

r = main::create_socket(id,module,host,port)

Create a socket, which is inserted into the bot's socket engine. You must specify a port and host to connect to. The module and callback parameters indicate the module name to call back to when there is data waiting to be read from the socket, the module being the perl object '$self' parameter. The id can be any arbitary text which your module can use to identify the socket. The function socket_callback will always be the called function within the module. Once you are finished with a socket you MUST delete it with main::delete_socket. this will transparently close the file descriptor for you. This function returns the file descriptor allocated, or 0 if it fails. You should avoid direct access to the file descriptor wherever possible.

An example of the socket_callback function and how it is used is shown below (taken from the modules/irc/telnet.pm module). It shows how to detect lost connections, and how to read the data (and when) and also how to tidy up data lines to remove newlines etc.

sub socket_callback {
        my ($self,$id) = @_;
        my $result = main::read_socket($id);
        $telnets{$id}{in} += length($result);
        if ($result eq "") {
                main::lprint("telnet Connection '$id' failed") if $main::debug;
                main::delete_socket($id);
                main::lprint("Deleted socket") if $main::debug;
                delete $telnets{$id};
                return;
        } else {
                chomp($result);
                $result =~ s/(\r|\n)//g;
                if ($result eq "") { return };
                main::lprint("telnet Data on '$id': '$result'") if $main::debug;  

        ...

        }
}

Further examples can be found on the documentation page for the create_listen_socket call.