|
||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||
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 |
|---|
void bind()
The underlying socket is not created until you call bind, but bind will be called automatically when you try to send or receive packets.
void bind(int localPort)
localPort - the local port to receive from
void send(java.lang.String remoteAddress,
int remotePort,
java.lang.String data)
remoteAddress - the remote address to send toremotePort - the remote port to send todata - the date to send
void send(java.lang.String remoteAddress,
int remotePort,
Bytes data)
remoteAddress - the remote address to send toremotePort - the remote port to send todata - the date to send
void send(java.lang.String remoteAddress,
int remotePort,
byte[] data)
remoteAddress - the remote address to send toremotePort - the remote port to send todata - the date to sendvoid setTimeout(int timeout)
The timeout is set to 5 seconds by default.
timeout - the timeoutUdpSocket.Packet readPacket()
This is a blocking call.
Adjust the timeout using setTimeout().
UdpSocket.Packet readPacket(int length)
If there is a packet waiting it will be returned immediately.
This is a blocking call. Adjust the timeout using setTimeout().
length - the max length of the packet to receive
void close()
|
© 2023 Vercara, LLC. All Rights Reserved. | |||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||