~/ learn/ comp-348/ cards/ Streams: the I/O every socket speaks
1 of 24

Pack a protocol line into bytes, write once, flush

Pack a protocol line into bytes, write once, flush

Answer

byte[] line = ("PING " + n + "\r\n").getBytes(StandardCharsets.US_ASCII); out.write(line); out.flush();

One write means at most one segment on the wire instead of eight. The charset is named rather than defaulted, and flush() is what turns a queued buffer into a sent request — without it the peer waits forever.

Harold 4e ch2 §Output Streams

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/c45e7d9b-60d3-4222-a0d0-054076a3617b/flashcard utf-8 LF