File: 10-basics.t

package info (click to toggle)
libanyevent-handle-udp-perl 0.050-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 160 kB
  • sloc: perl: 360; makefile: 6; sh: 4
file content (31 lines) | stat: -rw-r--r-- 919 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#! perl

use strict;
use warnings FATAL => 'all';
use Test::More tests => 3;
use AnyEvent::Handle::UDP;
use Socket qw/unpack_sockaddr_in/;
use IO::Socket::INET;

alarm 3;

{
	my $cb = AE::cv;
	my $server = AnyEvent::Handle::UDP->new(bind => [ localhost => 0 ], on_recv => $cb);
	my $port = (unpack_sockaddr_in($server->sockname))[0];
	my $client = IO::Socket::INET->new(PeerHost => 'localhost', PeerPort => $port, Proto => 'udp');
	send $client, "Hello", 0;
	is($cb->recv, "Hello", 'received "Hello"');
}

{
	my $cb = AE::cv;
	my $server = AnyEvent::Handle::UDP->new(bind => [ localhost => 0 ], on_recv => sub {
		my ($message, $handle, $client_addr) = @_;
		is($message, "Hello", "received \"Hello\"");
		$handle->push_send("World", $client_addr);
	});
	my $client = AnyEvent::Handle::UDP->new(connect => $server->sockname, on_recv => $cb);
	$client->push_send("Hello");
	is($cb->recv, "World", 'received "World"');
}