{
    "content": [
        {
            "type": "text",
            "text": "# POE::Component::Client::TCP (perldoc)\n\n## NAME\n\nPOE::Component::Client::TCP - a simplified TCP client\n\n## SYNOPSIS\n\n#!perl\nuse warnings;\nuse strict;\nuse POE qw(Component::Client::TCP);\nPOE::Component::Client::TCP->new(\nRemoteAddress => \"yahoo.com\",\nRemotePort    => 80,\nConnected     => sub {\n$[HEAP]{server}->put(\"HEAD /\");\n},\nServerInput   => sub {\nmy $input = $[ARG0];\nprint \"from server: $input\\n\";\n},\n);\nPOE::Kernel->run();\nexit;\n\n## DESCRIPTION\n\nPOE::Component::Client::TCP implements a generic single-Session client. Internally it uses\nPOE::Wheel::SocketFactory to establish the connection and POE::Wheel::ReadWrite to interact with\nthe server.\n\n## Sections\n\n- **NAME**\n- **SYNOPSIS**\n- **DESCRIPTION** (1 subsections)\n- **PUBLIC METHODS** (3 subsections)\n- **Public Events**\n- **Reserved Heap Members**\n- **SEE ALSO**\n- **CAVEATS**\n\nUse structuredContent.sections for detailed options, examples, and full documentation.\n"
        }
    ],
    "structuredContent": {
        "command": "POE::Component::Client::TCP",
        "section": "",
        "mode": "perldoc",
        "summary": "POE::Component::Client::TCP - a simplified TCP client",
        "synopsis": "#!perl\nuse warnings;\nuse strict;\nuse POE qw(Component::Client::TCP);\nPOE::Component::Client::TCP->new(\nRemoteAddress => \"yahoo.com\",\nRemotePort    => 80,\nConnected     => sub {\n$[HEAP]{server}->put(\"HEAD /\");\n},\nServerInput   => sub {\nmy $input = $[ARG0];\nprint \"from server: $input\\n\";\n},\n);\nPOE::Kernel->run();\nexit;",
        "tldr_summary": null,
        "tldr_examples": [],
        "tldr_source": null,
        "flags": [],
        "examples": [],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 22,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 8,
                "subsections": [
                    {
                        "name": "Performance Considerations",
                        "lines": 7
                    }
                ]
            },
            {
                "name": "PUBLIC METHODS",
                "lines": 1,
                "subsections": [
                    {
                        "name": "new",
                        "lines": 2
                    },
                    {
                        "name": "new",
                        "lines": 144
                    },
                    {
                        "name": "getpeername",
                        "lines": 99
                    }
                ]
            },
            {
                "name": "Public Events",
                "lines": 25,
                "subsections": []
            },
            {
                "name": "Reserved Heap Members",
                "lines": 39,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 10,
                "subsections": []
            },
            {
                "name": "CAVEATS",
                "lines": 17,
                "subsections": []
            }
        ],
        "sections": {
            "NAME": {
                "content": "POE::Component::Client::TCP - a simplified TCP client\n",
                "subsections": []
            },
            "SYNOPSIS": {
                "content": "#!perl\n\nuse warnings;\nuse strict;\n\nuse POE qw(Component::Client::TCP);\n\nPOE::Component::Client::TCP->new(\nRemoteAddress => \"yahoo.com\",\nRemotePort    => 80,\nConnected     => sub {\n$[HEAP]{server}->put(\"HEAD /\");\n},\nServerInput   => sub {\nmy $input = $[ARG0];\nprint \"from server: $input\\n\";\n},\n);\n\nPOE::Kernel->run();\nexit;\n",
                "subsections": []
            },
            "DESCRIPTION": {
                "content": "POE::Component::Client::TCP implements a generic single-Session client. Internally it uses\nPOE::Wheel::SocketFactory to establish the connection and POE::Wheel::ReadWrite to interact with\nthe server.\n\nPOE::Component::Client::TCP is customized by providing callbacks for common operations. Most\noperations have sensible default callbacks, so clients may be created with as little work as\npossible.\n",
                "subsections": [
                    {
                        "name": "Performance Considerations",
                        "content": "POE::Component::Client::TCP's ease of use comes at a price. The component is generic, so it's\nnot tuned to perform well for any particular application.\n\nIf performance is your primary goal, POE::Kernel's selectread() and selectwrite() perform\nabout the same as IO::Select, but your code will be portable across every event loop POE\nsupports.\n"
                    }
                ]
            },
            "PUBLIC METHODS": {
                "content": "new",
                "subsections": [
                    {
                        "name": "new",
                        "content": "that will handle server interaction.\n"
                    },
                    {
                        "name": "new",
                        "content": "always reliable to wait for the \"Connected\" callback to fire before transmitting data to the\nserver.\n\nThe client's constructor may seem to take a daunting number of parameters. As with most POE\nmodules, POE::Component::Client::TCP tries to do as much work in its constructor so that the\nrun-time code path is relatively light.\n\nConstructor Parameters Affecting the Session\nThe parameters in this section affect how the client's POE::Session object will be created.\n\nAlias\n\"Alias\" is an optional symbolic name for the client's Session. It allows other sessions to post\nevents to the client, such as \"shutdown\" and \"reconnect\". The client itself may yield() these\nevents, so an alias isn't usually needed.\n\nAlias => \"client\",\n\nArgs\n\"Args\" is optional. When specified, it holds an ARRAYREF that will be passed to the \"Started\"\ncallback via @[ARG0..$#]. This allows a program to pass extra information into the client\nsession.\n\nInlineStates\n\"InlineStates\" is optional. If specified, it must hold a hashref of named callbacks. Its syntax\nis that of POE:Session->create()'s inlinestates parameter.\n\nObjectStates\nIf \"ObjectStates\" is specified, it must hold an arrayref of objects and the events they will\nhandle. The arrayref must follow the syntax for POE::Session->create()'s objectstates\nparameter.\n\nPackageStates\nWhen the optional \"PackageStates\" is set, it must hold an arrayref of package names and the\nevents they will handle The arrayref must follow the syntax for POE::Session->create()'s\npackagestates parameter.\n\nPreConnect\n\"PreConnect\" is called before \"Connected\", and it has different parameters: $[ARG0] contains a\ncopy of the socket before it's given to POE::Wheel::ReadWrite for management. Most HEAP members\nare set, except of course $[HEAP]{server}, because the POE::Wheel::ReadWrite object has not\nbeen created yet. \"PreConnect\" may enable SSL on the socket using POE::Component::SSLify.\n\"PreConnect\" must return a valid socket to complete the connection; the client will disconnect\nif anything else is returned.\n\nPreConnect => {\n# Convert the socket into an SSL socket.\nmy $socket = eval { ClientSSLify($[ARG0]) };\n\n# Disconnect if SSL failed.\nreturn if $@;\n\n# Return the SSL-ified socket.\nreturn $socket;\n}\n\nSessionType\nEach client is created within its own Session. \"SessionType\" names the class that will be used\nto create the session.\n\nSessionType => \"POE::Session::MultiDispatch\",\n\n\"SessionType\" is optional. The component will use \"POE::Session\" by default.\n\nSessionParams\n\"SessionParams\" specifies additional parameters that will be passed to the \"SessionType\"\nconstructor at creation time. It must be an array reference.\n\nSessionParams => [ options => { debug => 1, trace => 1 } ],\n\nNote: POE::Component::Client::TCP supplies its own POE::Session constructor parameters.\nConflicts between them and \"SessionParams\" may cause the component to behave erratically. To\navoid such problems, please limit SessionParams to the \"options\" hash. See POE::Session for an\nknown options.\n\nWe may enable other options later. Please let us know if you need something.\n\nStarted\n\"Started\" sets an optional callback that will be invoked within the client session has been\nstarted. The callback's parameters are the usual for the session's start handler.\n\n\"Args\" may be used to pass additional parameters to \"Started\". This can be used to bypass issues\nintroduced by closures. The values from \"Args\" will be included in the @[ARG0..$#] parameters.\n\nsub handlestarted {\nmy @args = @[ARG0..$#];\n# ...\n}\n\nPOE::Wheel::SocketFactory Constructor Parameters\nThe constructor parameters in this section affect how the client's POE::Wheel::SocketFactory\nobject will be created.\n\nBindAddress\n\"BindAddress\" specifies the local interface address to bind to before starting to connect. This\nallows the client to connect from a specific address when multiple interfaces are available.\n\n\"BindAddress\" is optional. If specified, its value will be passed directly to\nPOE::Wheel::SocketFactory's BindAddress constructor parameter.\n\nBindPort\n\"BindPort\" sets the local socket port that the client will be bound to before starting to\nconnect. This allows the client to connect from a specific port.\n\nIt's not usually necessary to bind to a particular port, so \"BindPort\" is optional and disabled\nby default.\n\nIf specified, the value in \"BindPort\" is passed directly to POE::Wheel::SocketFactory's own\nBindPort constructor parameter.\n\nConnectError\n\"ConnectError\" is an optional callback to handle errors from POE::Wheel::SocketFactory. These\nerrors happen when a socket can't be created or has trouble connecting to the remote host.\n\nThe following parameters will be passed to the callback along with the usual POE event\nparameters: $[ARG0] will describe what was happening at the time of failure. $[ARG1] and\n$[ARG2] will contain the numeric and string versions of $!, respectively.\n\nDepending on the nature of the error and the type of client, it may be useful to reconnect from\nthe ConnectError callback.\n\nConnectError => sub {\nmy ($operation, $errornumber, $errorstring) = @[ARG0..ARG2];\nwarn \"$operation error $errornumber occurred: $errorstring\";\nif (errorisrecoverable($errornumber)) {\n$[KERNEL]->delay( reconnect => 60 );\n}\nelse {\n$[KERNEL]->yield(\"shutdown\");\n}\n},\n\nPOE::Component::Client::TCP will shut down after ConnectError if a reconnect isn't requested.\n\nConnected\nConnections are asynchronously set up and may take some time to complete. \"Connected\" is an\noptional callback that notifies a program when the connection has finally been made.\n\nThis is an advisory callback that occurs after a POE::Wheel::ReadWrite object has already been\ncreated. Programs should not need to create their own.\n\n\"Connected\" is called in response to POE::Wheel::SocketFactory's SuccessEvent. In addition to\nthe usual POE event parameters, it includes a copy of the established socket handle in $[ARG0].\nPOE::Component::Client::TCP will manage the socket, so an application should rarely need to save\na copy of it. $[ARG1] and $[ARG2] contain the remote address and port as returned from"
                    },
                    {
                        "name": "getpeername",
                        "content": "Connected => {\nmy ($socket, $peeraddr, $peerport) = @[ARG0, ARG1, ARG2];\n# ...\n}\n\nSee \"PreConnect\" to modify the socket before it's given to POE::Wheel::ReadWrite.\n\nConnectTimeout\n\"ConnectTimeout\" is the maximum number of seconds to wait for a connection to be established. If\nit is omitted, Client::TCP relies on the operating system to abort stalled connect() calls.\n\nThe application will be notified of a timeout via the ConnectError callback. In the case of a\ntimeout, $[ARG0] will contain \"connect\", and $[ARG1] and $[ARG2] will contain the numeric and\nstring representations of the ETIMEDOUT error.\n\nDomain\n\"Domain\" sets the address or protocol family within which to operate. The \"Domain\" may be any\nvalue that POE::Wheel::SocketFactory supports. AFINET (Internet address space) is used by\ndefault.\n\nUse AFINET6 for IPv6 support. This constant is exported by Socket. Also be sure to have\nSocket::GetAddrInfo installed, which is required by POE::Wheel::SocketFactory for IPv6 support.\n\nRemoteAddress\n\"RemoteAddress\" contains the address of the server to connect to. It is required and may contain\na host name (\"poe.perl.org\"), a dot- or colon-separated numeric address (depending on the\nDomain), or a packed socket address. Pretty much anything POE::Wheel::SocketFactory's\nRemoteAddress parameter does.\n\nRemotePort\n\"RemotePort\" contains the port of the server to connect to. It is required and may be a service\nname (\"echo\") or number (7).\n\nPOE::Wheel::ReadWrite Constructor Parameters\nParameters in this section control configuration of the client's POE::Wheel::ReadWrite object.\n\nDisconnected\n\"Disconnected\" is an optional callback to notify a program that an established socket has been\ndisconnected. It includes no special parameters.\n\nIt may be useful to reconnect from the Disconnected callback, in the case of MUD bots or\nlong-running services. For example:\n\nDisconnected => sub {\n$[KERNEL]->delay( reconnect => 60 );\n},\n\nThe component will shut down if the connection ceases without being reconnected.\n\nFilter\n\"Filter\" specifies the type of POE::Filter object that will parse input from and serialize\noutput to a server. It may either be a scalar, an array reference, or a POE::Filter object.\n\nIf \"Filter\" is a scalar, it will be expected to contain a POE::Filter class name:\n\nFilter => \"POE::Filter::Line\",\n\n\"Filter\" is optional. In most cases, the default \"POE::Filter::Line\" is fine.\n\nIf \"Filter\" is an array reference, the first item in the array will be treated as a POE::Filter\nclass name. The remaining items will be passed to the filter's constructor. In this example, the\nvertical bar will be used as POE::Filter::Line's record terminator:\n\nFilter => [ \"POE::Filter::Line\", Literal => \"|\" ],\n\nIf it is an object, it will be cloned every time the client connects:\n\nFilter => POE::Filter::Line->new(Literal => \"|\"),\n\nBe sure to \"use\" the appropriate POE::Filter subclass when specifying a \"Filter\" other than the\ndefault.\n\nServerError\n\"ServerError\" is an optional callback that will be invoked when an established server connection\nhas encountered some kind of error. It is triggered by POE::Wheel::ReadWrite's ErrorEvent. By\ndefault, the component will log any errors to STDERR. This may be suppressed by defining a\nquieter ServerError callback.\n\nAs with \"ConnectError\", it is invoked with the customary error parameters: $[ARG0] will contain\nthe name of the operation that failed. $[ARG1] and $[ARG2] will hold the numeric and string\nforms of $!, respectively.\n\nComponents usually disconnect on error. POE::Component::Client::TCP will shut down if the socket\ndisconnects without being reconnected.\n\nServerFlushed\n\"ServerFlushed\" is an optional callback to notify a program that ReadWrite's output buffers have\ncompletely flushed. It has no special parameters.\n\nThe component will shut down after a server flush if $heap->{shutdown} is set.\n\nServerInput\n\"ServerInput\" is a required callback. It is called for each fully parsed input record received\nby POE::Wheel::ReadWrite. $[ARG0] contains the input record, the format of which is determined\nby the \"Filter\" constructor parameter.\n\n\"SeverInput\" will stop being called when $[HEAP]{shutdown} is true. The most reliable way to\nset the \"shutdown\" member is to call $[KERNEL]->yield(\"shutdown\").\n"
                    }
                ]
            },
            "Public Events": {
                "content": "POE::Component::Client::TCP handles a small number of public \"command\" messages. These may be\nposted into the client from an external session, or yielded from within the client.\n\nconnect\nThe \"connect\" event causes POE::Component::Client::TCP to begin connecting to a server. It\noptionally includes a new RemoteHost and RemotePort, both of which will be used for subsequent\nreconnections.\n\n$[KERNEL]->post(alias => connect => \"127.0.0.1\", 80);\n\nIf the client is already connected to a server, it will disconnect immediately before beginning\nthe new connection procedure. Buffered input and output will be lost.\n\nreconnect\nThe \"reconnect\" command causes POE::Component::Client::TCP to immediately disconnect its current\nconnection and begin reconnecting to its most recently set RemoteHost and RemotePort. Any\nbuffered input and output will be lost.\n\nshutdown\nThe \"shutdown\" command tells POE::Component::Client::TCP to flush its buffers, disconnect, and\nbegin DESTROY procedures.\n\nAll input will be discarded after receipt of \"shutdown\". All pending output will be written to\nthe server socket before disconnecting and destructing.\n",
                "subsections": []
            },
            "Reserved Heap Members": {
                "content": "POE::Component::Client::TCP requires some heap space for its own bookkeeping. The following\nmembers are used and should be used as directed, or with care.\n\nThis sample input handler is an example of most reserved heap members:\n\nsub handleinput {\n# Pending input from when we were connected.\nreturn unless $[HEAP]{connected};\n\n# We've been shut down.\nreturn if $[HEAP]{shutdown};\n\nmy $input = $[ARG0];\n$[HEAP]{server}->put(\"you sent: $input\");\n}\n\nserver\nThe read-only \"server\" heap member contains the POE::Wheel object used to connect to or talk\nwith the server. While the component is connecting, \"server\" will be a POE::Wheel::SocketFactory\nobject. After the connection has been made, \"server\" is replaced with a POE::Wheel::ReadWrite\nobject.\n\nThe most reliable way to avoid prematurely using \"server\" is to first check the \"connected\"\nreserved heap member. See the example above.\n\nshutdown\n\"shutdown\" is a read-only flag that tells the component it's shutting down. It should only be by\nthe \"shutdown\" event, which does other cleanup.\n\n\"shutdown\" may be checked to avoid starting new work during a client's shutting-down procedure.\nSee the example above.\n\nconnected\n\"connected\" is a read-only flag that indicates whether the component is currently connected.\n\nshutdownonerror\n\"shutdownonerror\" is a read-only flag that governs the component's shutdown-on-error behavior.\nWhen true, POE::Component::Client::TCP will automatically shutdown when it encounters an error.\n",
                "subsections": []
            },
            "SEE ALSO": {
                "content": "The SEE ALSO section in POE contains a table of contents covering the entire POE distribution.\n\nPOE::Component::Server::TCP is the server-side counterpart to this module.\n\nThis component uses and exposes features from POE::Filter, POE::Wheel::SocketFactory, and\nPOE::Wheel::ReadWrite.\n\nSee \"SYNOPSIS\" in POE::Wheel::SocketFactory for a more efficient but lower-level way to create\nclients and servers.\n",
                "subsections": []
            },
            "CAVEATS": {
                "content": "This looks nothing like what Ann envisioned.\n\nPOE::Component::Client::TCP is a generic client. As such, it's not tuned for any particular\ntask. While it handles the common cases well and with a minimum of code, it may not be suitable\nfor everything.\n\nAUTHORS & COPYRIGHTS\nPOE::Component::Client::TCP is Copyright 2001-2013 by Rocco Caputo. All rights are reserved.\nPOE::Component::Client::TCP is free software, and it may be redistributed and/or modified under\nthe same terms as Perl itself.\n\nPOE::Component::Client::TCP is based on code, used with permission, from Ann Barcomb\n<kudra@domaintje.com>.\n\nPOE::Component::Client::TCP is based on code, used with permission, from Jos Boumans\n<kane@cpan.org>.\n",
                "subsections": []
            }
        }
    }
}