Logo

Programming-Idioms

  • Rust
  • Perl
  • Ruby
require 'socket'
require 'socket'

p = 4913
u1 = UDPSocket.new
u1.bind("127.0.0.1", p)
u1.send "message-to-self", 0, "127.0.0.1", p

b = u1.recvfrom(1024).first
use std::net::UdpSocket;
let mut b = [0 as u8; 1024];
let sock = UdpSocket::bind(("localhost", p)).unwrap();
sock.recv_from(&mut b).unwrap();
use IO::Socket::IP qw(SOCK_DGRAM);
my $p = 55555;
my $socket = IO::Socket::IP->new(
    LocalHost => 'localhost',
    LocalService => $p,
    Type => SOCK_DGRAM,
);
while () {
    $socket->read(my $B, 1024);
}

To verify the socket is operating, run `ss -anptu`.

To feed UDP traffic, use <https://nmap.org/ncat/>:
`ncat -u localhost 55555 < /dev/urandom`
import std.socket;
ubyte[1024] b;

auto sock = new UdpSocket();
scope(exit) sock.close;

sock.bind(new InternetAddress(p));
sock.receive(b);

New implementation...
< >
programming-idioms.org