{
    "mode": "perldoc",
    "parameter": "Net::Server::Multiplex",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AServer%3A%3AMultiplex/json",
    "generated": "2026-06-09T21:39:11Z",
    "synopsis": "package MyPlexer;\nuse base qw(Net::Server::Multiplex);\nsub muxinput {\n#...code...\n}\nPACKAGE->run();",
    "sections": {
        "NAME": {
            "content": "Net::Server::Multiplex - Multiplex several connections within one process\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "package MyPlexer;\n\nuse base qw(Net::Server::Multiplex);\n\nsub muxinput {\n#...code...\n}\n\nPACKAGE->run();\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "This personality is designed to handle multiple connections all within one process. It should\nonly be used with protocols that are guaranteed to be able to respond quickly on a packet by\npacket basis. If determining a response could take a while or an unknown period of time, all\nother connections established will block until the response completes. If this condition might\never occur, this personality should probably not be used.\n\nThis takes some nice features of Net::Server (like the server listen socket setup, configuration\nfile processing, safe signal handling, convenient inet style STDIN/STDOUT handling, logging\nfeatures, deamonization and pid tracking, and restartability -SIGHUP) and some nice features of\nIO::Multiplex (automatic buffered IO and per-file-handle objects) and combines them for an\neasy-to-use interface.\n\nSee examples/samplechat.pl distributed with Net::Server for a simple chat server that uses\nseveral of these features.\n",
            "subsections": []
        },
        "PROCESS FLOW": {
            "content": "The process flow is written in an open, easy to override, easy to hook, fashion. The basic flow\nis shown below.\n\n$self->configurehook;\n\n$self->configure(@);\n\n$self->postconfigure;\n\n$self->postconfigurehook;\n\n$self->prebind;\n\n$self->bind;\n\nif (Restarting server) {\n$self->restartopenhook();\n}\n\n$self->postbindhook;\n\n$self->postbind;\n\n$self->preloophook;\n\n$self->loop; # This basically just runs IO::Multiplex::loop\n# For routines inside a $self->loop\n# See CLIENT PROCESSING below\n\n$self->preserverclosehook;\n\n$self->postchildcleanuphook;\n\n$self->serverclose;\n\nif (Restarting server) {\n$self->restartclosehook();\n$self->hupserver;\n# Redo process again starting with configurehook\n}\n\nThe server then exits.\n",
            "subsections": []
        },
        "CLIENT PROCESSING": {
            "content": "The following represents the client processing program flow:\n\n$self->{server}->{client} = Net::Server::Proto::TCP->accept();  # NOTE: Multiplexed with muxinput() below\n\nif (checkfordequeue seconds have passed) {\n$self->rundequeue();\n}\n\n$self->getclientinfo;\n\n$self->postaccepthook; # Net::Server style\n\nif ($self->allowdeny\n&& $self->allowdenyhook) {\n\n# (Net::Server style $self->processrequest() is never called.)\n\n# A unique client specific object is created\n# for all mux* methods from this point on.\n$self = PACKAGE->new($self, client);\n\n$self->muxconnection; # IO::Multiplex style\n\nfor (every packet received) {\n$self->muxinput;  # NOTE: Multiplexed with accept() above\n}\n\n} else {\n\n$self->requestdeniedhook;\n\n# Notice that if either allowdeny or allowdenyhook fails, then\n# new(), muxconnection(), and muxinput() will never be called.\n# muxeof() and muxclose() will still be called, but using a\n# common listen socket callback object instead of a unique client\n# specific object.\n\n}\n\n$self->muxeof;\n\n$self->postprocessrequesthook;\n\n$self->muxclose;\n\nThis process then loops multiplexing between the accept() for the next connection and",
            "subsections": [
                {
                    "name": "mux_input",
                    "content": ""
                }
            ]
        },
        "HOOKS": {
            "content": "The *hook methods mentioned above are meant to be overridden with your own subroutines if you\ndesire to provide additional functionality.\n\nThe loop() method of Net::Server has been overridden to run the loop routine of IO::Multiplex\ninstead. The Net::Server methods may access the IO::Multiplex object at \"$self->{mux}\" if\ndesired. The IO::Multiplex methods may access the Net::Server object at \"$self->{netserver}\" if\ndesired.\n\nThe processrequest() method is never used with this personality.\n\nThe other Net::Server hooks and methods should work the same.\n\n\"$self->rundequeue()\"\nThis hook only gets called in conjunction with the checkfordequeue setting. It will run\nevery checkfordequeue seconds. Since no forking is done, this hook should run fast in\norder to prevent blocking the rest of the processing.\n",
            "subsections": []
        },
        "TIMEOUTS": {
            "content": "settimeout\nTo utilize the optional timeout feature of IO::Multiplex, you need to specify a timeout by using\nthe settimeout method.\n\n$self->{netserver}->{mux}->settimeout($fh, $secondsfromnow);\n\n$fh may be either a client socket or a listen socket file descriptor within the mux.\n$secondsfromnow may be fractional to achieve more precise timeouts. This is used in\nconjunction with muxtimeout, which you should define yourself.\n\nmuxtimeout\nThe main loop() routine will call $obj->muxtimeout($mux, $fh) when the timeout specified in\nsettimeout is reached where $fh is the same as the one specified in settimeout() and $obj is\nits corresponding object (either the unique client specific object or the main listen callback\nobject) and $mux is the main IO::Multiplex object itself.\n",
            "subsections": []
        },
        "CALLBACK INTERFACE": {
            "content": "Callback objects should support the following interface. You do not have to provide all of these\nmethods, just provide the ones you are interested in. These are just like the IO::Multiplex\nhooks except that STDOUT is tied to the corresponding client socket handle for your convenience\nand to more closely emulate the Net::Server model. However, unlike some other Net::Server\npersonalities, you should never read directly from STDIN yourself. You should define one or more\nof the following methods:\n\nmuxconnection ($mux,$fh)\n(OPTIONAL) Run once when the client first connects if the allowdeny passes. Note that the\n\"$self->{netserver}->{server}\" property hash may be modified by future connections through\nNet::Server. Any values within it that this object may need to use later should be copied within\nits own object at this point.\n\nExample:\n$self->{peerport} = $self->{netserver}->{server}->{peerport};\n\nmuxinput ($mux,$fh,\\$data)\n(REQUIRED) Run each time a packet is read. It should consume $data starting at the left and\nleave unconsumed data in the scalar for future calls to muxinput.\n\nmuxeof ($mux,$fh,\\$data)\n(OPTIONAL) Run once when the client is done writing. It should consume the rest of $data since",
            "subsections": [
                {
                    "name": "mux_input",
                    "content": "muxclose ($mux,$fh)\n(OPTIONAL) Run after the entire client socket has been closed. No more attempts should be made\nto read or write to the client or to STDOUT.\n\nmuxtimeout ($mux,$fh)\n(OPTIONAL) Run once when the settimeout setting expires as explained above.\n"
                }
            ]
        },
        "BUGS": {
            "content": "This is only known to work with TCP servers.\n\nIf you need to use the IO::Multiplex style settimeout / muxtimeout interface, you cannot use\nthe Net::Server style checkfordequeue / rundequeue interface. It will not work if the\ncheckfordequeue option is specified. The rundequeue method is just a compatibility interface\nto comply with the Net::Server::Fork style rundequeue but is implemented in terms of the\nIO::Multiplex style settimeout and muxtimeout methods.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Rob Brown <bbb@cpan.org>\n",
            "subsections": []
        },
        "MAINTAINER": {
            "content": "Paul Seamons <paul@seamons.com>\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This package may be distributed under the terms of either the\nGNU General Public License\nor the\nPerl Artistic License\n\nAll rights reserved.\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "Net::Server by Paul Seamons <paul@seamons.com>,\n\nIO::Multiplex by Bruce Keeler <bruce@gridpoint.com>.\n",
            "subsections": []
        }
    },
    "summary": "Net::Server::Multiplex - Multiplex several connections within one process",
    "flags": [],
    "examples": [],
    "see_also": []
}