1
2
3
4
5
6
# Broadcast:
14:40:09.490409 IP 192.168.0.14.5001 > 255.255.255.255.5000: UDP, length 16
14:40:09.491281 IP 192.168.0.15.5000 > 192.168.0.14.5001: UDP, length 16
# Unicast:
14:41:44.950375 IP 192.168.0.14.5001 > 192.168.0.15.5000: UDP, length 16
14:41:44.951275 IP 192.168.0.15.5000 > 192.168.0.14.5001: UDP, length 16
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
#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; use IO::Select; $| = 1; my ($socket,$select,$data,$peer_address,$peer_port); $socket = new IO::Socket::INET ( PeerAddr => inet_ntoa(INADDR_BROADCAST), # PeerAddr => '192.168.0.15', PeerPort => '5000', LocalPort => '5001', Proto => 'udp', Broadcast => '1', ) or die "ERROR in Socket Creation : $!\n"; $select = new IO::Select or die "no select: $!\n"; $select->add( $socket ) or die "no add socket: $!"; print "Start Client.\n"; $socket->send('data from client') or die "send faild: $!"; while ($select->can_read(2)){ $socket->recv($data,128); $peer_address = $socket->peerhost(); $peer_port = $socket->peerport(); print "($peer_address:$peer_port) said: \"$data\"\n"; } $socket->close(); print "Ende\n";
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; $| = 1; my ($socket,$received_data,$peer_address,$peer_port); $socket = new IO::Socket::INET ( LocalPort => '5000', Proto => 'udp', Broadcast => '1', ) or die "ERROR in Socket Creation : $!\n"; print "Start Server.\n"; while(1) { $socket->recv($received_data,1024); $peer_address = $socket->peerhost(); $peer_port = $socket->peerport(); print "($peer_address:$peer_port) said: \"$received_data\"\n"; $socket->send('data from Server') or print "Can't send data! $!\n"; } $socket->close();