Created
July 30, 2017 17:46
-
-
Save factorsofx/73a675f7ea53a3358cb91c3c22b2428c to your computer and use it in GitHub Desktop.
VERY basic BYOND topic request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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