The user selects an option, in this case option 1 – Brew. They then enter the URL, in this case BREW. I’m only checking the word BREW at the moment so coffee://127… is irrelevant at this stage.
A message response is received back from the server. Notice the 200 OK being sent in the first server response message.
The application continues to loop where the user enters to URL again, again it’s BREW.
The problem this time is that a 400 Bad Request message is returned. As the URL is correct, they should receive 200 OK like the first pass around the application.
Client Console:
WELCOME TO THE COFFEE POT APPLICATION!
Select an option:
1. Brew
2. Quit
1
Enter URL (e.g. BREW coffee://127.0.0.1/pot-1 HTCPCP-new )
BREW
MESSAGE FROM SERVER:
Message: BREW Accept-Additions: *, *, *, *, *, *, *,
HTCPCP-new 200 OK BREW START command completed.
Content-length: 44
Content-type: application/octet-stream
@@
Select an option:
1. Brew
2. Quit
1
Enter URL (e.g. BREW coffee://127.0.0.1/pot-1 HTCPCP-new )
BREW
MESSAGE FROM SERVER:
Message: BREW Accept-Additions: *, *, *, *, *, *, *,
HTCPCP-new 400 Bad Request.
Content-length: 5
Content-type: application/octet-stream
@@
Select an option:
1. Brew
2. Quit
Client Thread Handler:
import java.io.*;
import java.net.*;
import javax.activation.MimetypesFileTypeMap;
public class HTCPCPClientWorker extends Thread {
Socket cwsocket = null;
public HTCPCPClientWorker(Socket cwsocket) {
super("ClientWorker");
this.cwsocket = cwsocket;
}
@Override
public void run() {
String clientSentence = null;
BufferedReader inFromClient = null;
PrintWriter outToClient = null;
try {
inFromClient = new BufferedReader(new InputStreamReader(cwsocket.getInputStream()));
outToClient = new PrintWriter(cwsocket.getOutputStream(), true);
} catch (IOException ex) {
System.err.println("Cannot create streams");
}
try {
do { // end when client says QUIT
StringBuffer clientInputLine[] = new StringBuffer[3];
clientInputLine[0] = new StringBuffer();
clientInputLine[1] = new StringBuffer();
// Get next message from client
for (int i = 0; i <= clientInputLine.length; i++) {
// read input line from BufferedReader
clientSentence = inFromClient.readLine();
if (clientSentence.contains("BREW")) {
outToClient.println("Message: " + clientSentence);
outToClient.println("HTCPCP-new 200 OK BREW START command completed.");
outToClient.println("Content-length: " + clientSentence.length()); // length needs to correspond to above line
outToClient.println("Content-type: " + new MimetypesFileTypeMap().getContentType(clientSentence));
outToClient.println("@@");
outToClient.flush();
} else {
outToClient.println("Message: " + clientSentence);
outToClient.println("HTCPCP-new 400 Bad Request.");
outToClient.println("Content-length: " + clientSentence.length()); // length needs to correspond to above line
outToClient.println("Content-type: " + new MimetypesFileTypeMap().getContentType(clientSentence));
outToClient.println("@@");
outToClient.flush();
}
// wait for EOF = @@
System.out.println("\tInput: " + clientSentence);
if (clientSentence.equals("@@") == true) {
break;
}
clientInputLine[i].append(clientSentence);
} // end for loop
} while (!clientSentence.contains("QUIT"));
outToClient.println("GOODBYE!");
outToClient.flush();
System.out.println("\tClient has disconnected.");
cwsocket.close();
} catch (IOException e) {
e.printStackTrace();
}
} // end run
} // end HTCPCPClientWorker.java
I thought the issue would be rectified by recreating the StringBuffer on each pass but this doesn’t appear to be the case.
3rd pass and onwards appear to function correctly.
Any thoughts?
I believe you should recreate the BufferedReader (inFromClient) each time you want to read from the socket. The BufferedReader gets empty after you use readLine, so the next time you put input on the socket he wouldn’t read it.
There’s more information about reading and writing to sockets here.