Skip to content

Instantly share code, notes, and snippets.

@sleepdeprecation
Created October 27, 2011 03:49
Show Gist options
  • Save sleepdeprecation/1318741 to your computer and use it in GitHub Desktop.
Save sleepdeprecation/1318741 to your computer and use it in GitHub Desktop.
Read and send data - server style
// This is used inside of my own parseCommand(InputStream inStream, OutputStream outStream) method.
// The method has some other stuff included, but his is the gist of it.
// I changed some stuff up so that it would be more universal.
//
// This protocol uses \n as the end of a command, you can change that by changing the variable
// protocolEOL.
char protocolEOL = "\n";
String inStr = "";
BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
int inr = in.read();
while (inr != -1 && inr != (int)protocolEOL) {
inStr += Character.toString((char)inr);
System.out.print((char)inr);
inr = in.read();
}
System.out.println("[Client]: " + inStr);
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(outStream));
String outStr = "Hey There, Client";
char[] tmpc = outStr.toCharArray();
for (char c : tmpc) {
out.write(c);
}
out.write(protocolEOL);
out.flush();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment