Skip to content

Instantly share code, notes, and snippets.

@ejamesc
Created April 6, 2012 09:10
Show Gist options
  • Select an option

  • Save ejamesc/2318355 to your computer and use it in GitHub Desktop.

Select an option

Save ejamesc/2318355 to your computer and use it in GitHub Desktop.
Java Socket Readline
/* Crude implementation of a readLine function -> used to get HTTP headers from a socket InputStream
* Then you may get the rest of the binary data by just calling sock.getInputStream().read()
*/
public static String readLine(Socket sock) {
String line = new String();
int c;
try {
while ((c = sock.getInputStream().read()) != '\n') {
line += (char) c;
}
} catch (IOException e) {
System.err.println("Error reading line from stream");
System.exit(-14);
}
// We may have a trailing return
line = line.trim();
// System.out.println("Read Line " + line);
return line;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment