Created
April 6, 2012 09:10
-
-
Save ejamesc/2318355 to your computer and use it in GitHub Desktop.
Java Socket Readline
This file contains hidden or 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
| /* 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