The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

UV::TCP - TCP socket handles in libuv

SYNOPSIS

  #!/usr/bin/env perl
  use strict;
  use warnings;

  use Socket;

  # A new stream handle will be initialised against the default loop
  my $tcp = UV::TCP->new;

  $tcp->connect(pack_sockaddr_in(1234, inet_aton("127.0.0.1")), sub {
    say "Connected!";

    $tcp->write("Hello, server!\n");
  });

  # set up the data read callback
  $tcp->on(read => sub {
    my ($self, $err, $buf) = @_;
    say "More data: $buf";
  });
  $tcp->read_start();

DESCRIPTION

This module provides an interface to libuv's TCP stream handle.

EVENTS

UV::TCP inherits all events from UV::Stream and UV::Handle.

METHODS

UV::TCP inherits all methods from UV::Stream and UV::Handle and also makes the following extra methods available.

open

    $tcp->open($fh);

The open method associates the TCP handle with an existing filehandle already opened by the process.

Note that this method is not currently supported on Windows, because libuv would want overlapped IO (created by WSA_FLAG_OVERLAPPED), but Perl does not create such sockets.

See also https://github.com/p5-UV/p5-UV/issues/38.

nodelay

    $tcp->nodelay($enable);

The nodelay method controls the TCP_NODELAY socket option.

keepalive

    $tcp->keepalive($enable, $delay);

The keepalive method controls keepalive behaviour on the TCP socket. If the $enable argument is true then $delay must be supplied; if not it is ignored and may be absent.

simultaneous_accepts

    $tcp->simultaneous_accepts($enable);

The simultaneous_accepts method controls whether asynchronous accept requests are queued by the operating system.

bind

    $tcp->bind($addr);

The bind method associates the TCP socket with the given local address.

connect

    $tcp->connect($addr, sub {
        my ($err) = @_;
        die "Cannot connect TCP socket - $err\n" if $err;

        say "The TCP socket is now connected";
    });

The connect method requests that the TCP socket be connected a server found at the given address.

On completion the callback is invoked. It is passed undef on success, or an error value on failure. This error value can be compared numerically to one of the UV_E* constants, or printed as a string to give a message.

getpeername

    my $addr = $tcp->getpeername;

The getpeername method returns a packed sockaddr string containing the address to which this TCP handle is connected.

getsockname

    my $addr = $tcp->getsockname;

The getsockname method returns a packed sockaddr string containing the address on which this TCP handle is listening for incoming connections.

close_reset

    $tcp->close_reset();
    $tcp->close_reset(sub {say "we've closed"});

The close_reset method requests that the socket be closed with a RST packet. It otherwise behaves similarly to "close" in UV::Handle.

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.