The UDP packet class facilitates handling packet data through a set of properties and methods designed for reading, writing, and managing byte arrays. It provides access to metadata such as the originating host, port, and packet length. The index property tracks the current position in the byte array, which is automatically updated after each read or write operation.
Methods include the ability to read and write various data types such as bytes, integers, shorts, and UTF-8 strings. Additionally, the class supports extracting the entire byte array of the packet and encoding custom data for transmission. Advanced use cases involve combining these capabilities for constructing or parsing complex packet structures.
Writes an UTF-8 encoded string into the packet, at the current index.
Properties Detailed
index
Returns the current position in the byte array of the packet. The next read/write operation will occur at this position.
This is a 0 based index.
TypeNumber The zero-based index indicating the current read/write position in the packet's byte array.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
application.output('an int is: ' + packet.readInt());
application.output('moved to index: ' + packet.index);
application.output('a short is: ' + packet.readShort());
application.output('moved to index: ' + packet.index);
application.output('a byte is: ' + packet.readByte());
application.output('moved to index: ' + packet.index);
application.output('a byte is: ' + packet.readByte());
application.output('moved to index: ' + packet.index);
}
else {
application.output('end of communication.');
break;
}
}
Methods Detailed
getByteArray()
Returns the content of the package into a byte array.
Returns:Array A copy of the packet's payload data as a byte array.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
var bytes = packet.getByteArray();
application.output('received a packet of length: ' + bytes.length);
for (var i=0; i<bytes.length; i++)
application.output(bytes[i]);
}
else {
application.output('end of communication.');
break;
}
}
getHost()
Returns the name of the host that sent the packet.
Returns:String The hostname or IP address from which this packet was sent.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
application.output('message is: ' + packet.readUTF());
}
else {
application.output('end of communication.');
break;
}
}
getLength()
Returns the length of the packet in bytes.
Returns:Number The total number of bytes contained in the packet's payload.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
application.output('message is: ' + packet.readUTF());
}
else {
application.output('end of communication.');
break;
}
}
getPort()
Returns the port where the packet originated from.
Returns:Number The port number associated with the sender of this packet.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
application.output('message is: ' + packet.readUTF());
}
else {
application.output('end of communication.');
break;
}
}
readByte()
Reads an 8 bits byte value from the packet, starting from the current index. Advances the index with one position.
Returns:Number A single byte from the current position in the packet, represented as an integer.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
application.output('an int is: ' + packet.readInt());
application.output('moved to index: ' + packet.index);
application.output('a short is: ' + packet.readShort());
application.output('moved to index: ' + packet.index);
application.output('a byte is: ' + packet.readByte());
application.output('moved to index: ' + packet.index);
application.output('a byte is: ' + packet.readByte());
application.output('moved to index: ' + packet.index);
}
else {
application.output('end of communication.');
break;
}
}
readInt()
Reads a 32 bits int value from the packet, starting from the current index. Advances the index with 4 positions.
Returns:Number A 32-bit integer extracted from the current position in the packet.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
application.output('an int is: ' + packet.readInt());
application.output('moved to index: ' + packet.index);
application.output('a short is: ' + packet.readShort());
application.output('moved to index: ' + packet.index);
application.output('a byte is: ' + packet.readByte());
application.output('moved to index: ' + packet.index);
application.output('a byte is: ' + packet.readByte());
application.output('moved to index: ' + packet.index);
}
else {
application.output('end of communication.');
break;
}
}
readShort()
Reads a 32 bits short value from the packet, starting from the current index. Advances the index with 2 positions.
Returns:Number A 16-bit short value extracted from the current position in the packet.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
application.output('an int is: ' + packet.readInt());
application.output('moved to index: ' + packet.index);
application.output('a short is: ' + packet.readShort());
application.output('moved to index: ' + packet.index);
application.output('a byte is: ' + packet.readByte());
application.output('moved to index: ' + packet.index);
application.output('a byte is: ' + packet.readByte());
application.output('moved to index: ' + packet.index);
}
else {
application.output('end of communication.');
break;
}
}
readUTF()
Reads a UTF-8 string from the packet, starting from the current index. If an argument is specified, then it represents the length (in UTF-8 encoded bytes, not characters) of the string to read. If no argument is specified, then first a 32 bits (4 byte) int is read from the packet and that will be the byte length of the string. Advances the index with a number of positions that depends on the length of the read string.
Returns:String A string decoded from the packet, with its length determined by the first four bytes.
Sample
var packet;
while (packet = plugins.udp.getReceivedPacket()) {
application.output('packet received from ' + packet.getHost() + ':' + packet.getPort());
if (packet.getLength() > 0) {
application.output('message is: ' + packet.readUTF());
}
else {
application.output('end of communication.');
break;
}
}
readUTF(length)
Reads a UTF-8 string from the packet, starting from the current index. If an argument is specified, then it represents the length (in UTF-8 encoded bytes, not characters) of the string to read. If no argument is specified, then first a 32 bits (4 byte) int is read from the packet and that will be the byte length of the string. Advances the index with a number of positions that depends on the length of the read string.
Writes an array of bytes into the packet, at the current index. The index is advanced with a number of positions equal to the length of the written array.
Writes an UTF-8 encoded string into the packet, at the current index. First the length of the string is written on 4 bytes, then the string is written. The index is advanced with a number of positions equal to the length of the string plus 4.