biz.neustar.wpm.api
Interface UdpSocket


public interface UdpSocket

Raw UDP (Datagram/Unix Domain Socket) socket API.

This is a blocking socket, preliminary used for monitoring the uptime of socket services.

Here is an example that queries a DNS server over UDP and verifies the results. NOTE: This is just for illustration purposes, we have a DNS API for doing DNS queries directly.
 var net = require('net');

 var nameserver = "8.8.8.8";
 var port = 53;
 var hostname = "www.google.com";
 var hosthex = "";
 var parts = hostname.split(".");

 function toHex(val) {
   var str = Number(val).toString(16);
   return str.length == 1 ? "0" + str : str;
 }

 for (var i = 0; i < parts.length; ++i) {
   var part = parts[i];
   hosthex += toHex(part.length);
   hosthex += utils.toHex(part);
 }

 var value = "000101000001000000000000" + hosthex + "0000010001";
 var response = "000181800001001000000000" + hosthex + "0000010001";
 var responseRegex = new RegExp(response);

 var req = utils.fromHex(value);

 test.beginTransaction();
 test.beginStep();
   var s = net.newUdpSocket();
   s.bind();
   s.send(nameserver, port, req);
   var p = s.readPacket();
   if (!p.data().toHex().match(responseRegex)) {
       throw "Invalid DNS lookup response";
   }
   s.close();
 test.endStep();
 test.endTransaction();
 


Nested Class Summary
static interface UdpSocket.Packet
          A packet read from the socket.
 
Method Summary
 void bind()
          Binds a socket to an ephemeral local port.
 void bind(int localPort)
          Binds the socket to a specific local port.
 void close()
          Close the underlying socket.
 UdpSocket.Packet readPacket()
          Wait for a packet from a remote host and return it.
 UdpSocket.Packet readPacket(int length)
          Wait for a packet from a remote host and return it.
 void send(java.lang.String remoteAddress, int remotePort, byte[] data)
          Sends the given byte array to the remoteAddress and remotePort.
 void send(java.lang.String remoteAddress, int remotePort, Bytes data)
          Sends the given bytes to the remoteAddress and remotePort.
 void send(java.lang.String remoteAddress, int remotePort, java.lang.String data)
          Sends the given UTF-8 text to the remoteAddress and remotePort.
 void setTimeout(int timeout)
          Sets the read timeout in milliseconds on the socket.
 

Method Detail

bind

void bind()
Binds a socket to an ephemeral local port.

The underlying socket is not created until you call bind, but bind will be called automatically when you try to send or receive packets.


bind

void bind(int localPort)
Binds the socket to a specific local port.

Parameters:
localPort - the local port to receive from

send

void send(java.lang.String remoteAddress,
          int remotePort,
          java.lang.String data)
Sends the given UTF-8 text to the remoteAddress and remotePort.

Parameters:
remoteAddress - the remote address to send to
remotePort - the remote port to send to
data - the date to send

send

void send(java.lang.String remoteAddress,
          int remotePort,
          Bytes data)
Sends the given bytes to the remoteAddress and remotePort.

Parameters:
remoteAddress - the remote address to send to
remotePort - the remote port to send to
data - the date to send

send

void send(java.lang.String remoteAddress,
          int remotePort,
          byte[] data)
Sends the given byte array to the remoteAddress and remotePort.

Parameters:
remoteAddress - the remote address to send to
remotePort - the remote port to send to
data - the date to send

setTimeout

void setTimeout(int timeout)
Sets the read timeout in milliseconds on the socket.

The timeout is set to 5 seconds by default.

Parameters:
timeout - the timeout

readPacket

UdpSocket.Packet readPacket()
Wait for a packet from a remote host and return it.

This is a blocking call.

Adjust the timeout using setTimeout().

Returns:
the packet that was received

readPacket

UdpSocket.Packet readPacket(int length)
Wait for a packet from a remote host and return it.

If there is a packet waiting it will be returned immediately.

This is a blocking call. Adjust the timeout using setTimeout().

Parameters:
length - the max length of the packet to receive
Returns:
the packet that was received

close

void close()
Close the underlying socket.


Copyright © 2020 Neustar, Inc. All Rights Reserved.