Skip to content

Instantly share code, notes, and snippets.

@factorsofx
Created July 30, 2017 17:46
Show Gist options
  • Save factorsofx/73a675f7ea53a3358cb91c3c22b2428c to your computer and use it in GitHub Desktop.
Save factorsofx/73a675f7ea53a3358cb91c3c22b2428c to your computer and use it in GitHub Desktop.
VERY basic BYOND topic request
public final class Byond
{
public static String makeRequest(String url, int port, String topic) throws IOException
{
Socket sock = new Socket(url, port);
//Socket sock = new Socket("localhost", 4567);
InputStream is = sock.getInputStream();
OutputStream os = sock.getOutputStream();
byte[] msg = ("?" + topic).getBytes();
os.write(0x00);
os.write(0x83);
os.write(0);
os.write(msg.length + 6);
os.write(new byte[]{0, 0, 0, 0, 0});
os.write(msg);
os.write(0);
os.flush();
byte[] header = new byte[5];
is.read(header);
int msb = Byte.toUnsignedInt(header[2]);
int lsb = Byte.toUnsignedInt(header[3]);
int respLen = (int)Integer.toUnsignedLong((msb << 8) | lsb);
//System.out.println("Reading " + respLen + " bytes...");
byte[] responseBuffer = new byte[respLen];
is.read(responseBuffer);
CharsetDecoder decoder = StandardCharsets.ISO_8859_1.newDecoder();
CharBuffer charBuffer = decoder.decode(ByteBuffer.wrap(responseBuffer));
sock.close();
return charBuffer.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment