public String readResponse(HttpResponse response) { String output = ""; HttpEntity entity = response.getEntity(); try { trapException(response.getStatusLine().getStatusCode()); } catch (CrowdFlowerException e1) { e1.printStackTrace(); } InputStream instream; try { instream = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); // do something useful with the response output = output + reader.readLine(); instream.close(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return output; }
// sends a response to the Client based upon the contents of the receive packet private void reply(DatagramPacket receivedPacket) { // verify the packet byte serverMsg[] = {}; try { Request recReq = new Request(receivedPacket.getData(), receivedPacket.getLength()); if (!recReq.isValid()) { throw new IllegalStateException(); } switch (recReq.getFormat()) { case RRQ: serverMsg = new byte[] {0, 3, 0, 1}; break; case WRQ: serverMsg = new byte[] {0, 4, 0, 0}; break; default: throw new IllegalStateException(); } } catch (IllegalStateException e) { e.printStackTrace(); System.exit(1); } DatagramPacket sendPacket = new DatagramPacket( serverMsg, serverMsg.length, receivedPacket.getAddress(), receivedPacket.getPort()); readSendPacket(sendPacket); try { // Send the datagram packet to the client via the send socket. DatagramSocket sendSocket = new DatagramSocket(); sendSocket.send(sendPacket); sendSocket.close(); } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } System.out.println("Server: packet sent.\n"); }