There's no such thing as a stupid question, but they're the easiest to answer.


Software Development Software Development
Search Search
Search for:
Tech Support Guy > > >

Homework Aid


Ryuumaru's Avatar
Ryuumaru Ryuumaru is offline
Computer Specs
Member with 2 posts.
Join Date: Sep 2010
Experience: Computer Science Major
02-May-2012, 11:27 PM #1
Post Homework Aid
I know you don't technically don't do homework here, but can you at least help with homework? There's a slight problem with my code that I hope you will help me with.

Basically, I'm learning to make a simple multithreaded HTTP request/response server using Java. I could make a single thread that does one HTTP request/response... and I've done multithreading in the past, though not quite in this way. My problem mostly seems to be in mixing the two. I keep getting nullPointerException errors, and I dunno why. I've debugged it over and over, and nothing seems wrong.

Could you point out any possible solutions I should try? Effectively what it's doing, or at least what it should be doing, with each thread is making a socket for talking to the browser, takes the GET request, and sends the requested file as raw byte data. I managed to get it working with a single thread, and it would send the basic index.html file, but then it'd break on the second loop on a nullPointerException. I thought that setting up as multiple threads might help fix that, but it doesn't seem to.

package lab4;


 * A server that handles multiple HTTP requests
 * @author dmloveless
public class WebServer {
    public static String array[];
    public static void main(String[] args) {
        BufferedReader jin = new BufferedReader(new InputStreamReader(;
        int port = 0;
        try {
          if (args.length == 0) {
          String input;
          System.out.print("Server Port: ");
          input = jin.readLine();
          array = input.split(" ");
          port = Integer.parseInt(array[0]);
          else { port = Integer.parseInt(args[0]); }
            TServSock Server = new TServSock(port);
            while ( true ) {
              TIMSock client = Server.engage();
              new Thread( new WorkerRunnable( client ) ).start();
        catch (Exception ex) {
           System.out.println("Exception caught in thread: " + ex);

class TIMSock {
   private Socket sock;
   private BufferedReader jin;
   private PrintWriter jout;
   TIMSock(InetAddress Host, int Port) throws SocketException, IOException{
      sock = new Socket(Host, Port);
   TIMSock(Socket sock) throws IOException {
      this.sock = sock;
   private void setIO() throws IOException{
      InputStream inStream = sock.getInputStream();
      jin = new BufferedReader(new InputStreamReader(inStream));
      OutputStream outStream = sock.getOutputStream();
      jout = new PrintWriter(new OutputStreamWriter(outStream));
   public InputStream rawIn() throws IOException {	
      return sock.getInputStream();
   public OutputStream rawOut() throws IOException {	
      return sock.getOutputStream();
   public void IMsend(String message) throws IOException {	
   public String IMreceive() throws IOException {	
      String message = jin.readLine();  
      return message;
   public void timer(int time) throws SocketException {
   public void close() throws IOException{ sock.close(); }

class TServSock {
   private ServerSocket serv;
   TServSock(int Port) throws SocketException, IOException{
      serv = new ServerSocket(Port);
   TServSock(ServerSocket serv) throws IOException {
      this.serv = serv;
   public TIMSock engage() throws IOException, NullPointerException {
      return new TIMSock(serv.accept());
   public void close() throws IOException{ serv.close(); }

class WorkerRunnable implements Runnable{
    private TIMSock sock = null;
    private String input = null;
    public WorkerRunnable(TIMSock clientSocket) {
        this.sock = clientSocket;
    public void run() {
        try {
              input = sock.IMreceive();
              if (input != null) {
              HttpRequest HR = new HttpRequest(input);
        } catch (IOException e) {
package lab4;

import java.util.*;

 * Handles a individual HTTP request
 * @author dmloveless
public class HttpRequest {
    private String method;
    private String file;
    private String protocol;
    private Scanner fis;
    public String request;
    HttpRequest( String request ) throws SocketException, IOException {
        this.request = request;
    public void response( OutputStream fout ) throws SocketException, IOException {
        String response = ""; 
        StringTokenizer message = new StringTokenizer(request);
        method = message.nextToken().toString();
        file = message.nextToken().toString();
        protocol = message.nextToken().toString();
        final int BUFFER = 2048;
        byte[] bytes = new byte[BUFFER];
        FileInputStream fis = null;
        if ( !method.equals("GET") ) {
          response = "HTTP/1.1 504 UNKNOWN METHOD/r/n";
        boolean fileExists = true;
        try {
          File HTML = new File(file.replaceFirst("/",""));
          if (HTML.exists()) {
            fis = new FileInputStream(HTML);
        catch (FileNotFoundException e) {
          if ( file.equals("/") ) {
              response = "HTTP/1.1 200 OK\r\n" +
                "Content-Type: text/html\r\n" +
                "Content-Length: 46\r\n" +
                "\r\n" +
                "<html>\r\n" +
                "<body>\r\n" +
                "<p>Hello, World!</p>\r\n" +
                "</body>\r\n" +
          else fileExists = false;
        boolean validProtocol;
        if (protocol.equals("HTTP/1.0") || protocol.equals("HTTP/1.1")) { validProtocol = true; }
        else { validProtocol = false; }
        if ( fileExists && validProtocol ) {
            Scanner fin = new Scanner(fis);
            response = "HTTP/1.1 200 OK\r\n" +
            "Content-Type: text/html\r\n" + 
            while (fin.hasNextLine()) { response += fin.nextLine().toString() + "\r\n"; }
        else { 
            if ( !fileExists ) response = "HTTP/1.1 404 FILE NOT FOUND\r\n" +
                "Content-Type: text/html\r\n" +
                "Content-Length: 23\r\n" +
                "\r\n" +
                "<h1>File Not Found</h1>";
            if ( !validProtocol ) response = "HTTP/1.1 505 INVALID PROTOCOL\r\n" +
                "Content-Type: text/html\r\n" +
                "Content-Length: 25\r\n" +
                "\r\n" +
                "<h1>Version too old!</h1>";
Squashman's Avatar
Trusted Advisor with 19,725 posts.
Join Date: Apr 2003
Location: 1265 Lombardi Ave
03-May-2012, 07:18 AM #2
Originally Posted by Ryuumaru View Post
I know you don't technically don't do homework here, but can you at least help with homework?
That's a double negative.
ckphilli's Avatar
ckphilli   (Chris) ckphilli is offline
Member with 4,383 posts.
Join Date: Apr 2006
Location: Down South
Experience: Intermediate
03-May-2012, 01:57 PM #3
I'm sure a mod will be along shortly to close it is homework assistance.

Before they do I'd like to offer some advice...develop your own logical troubleshooting practices and use google. I have yet to hit a problem that I couldn't solve with google and breaking each step down logically. 'Debugging' is a small percentage of solving a problem.

Here's a start:

Please understand I am not trying to be a jerk.
valis's Avatar
Moderator with 67,194 posts.
Join Date: Sep 2004
Location: as above, so below
03-May-2012, 02:13 PM #4
aaaaaaand closing for reasons stated.


As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Search Tech Support Guy

Find the solution to your
computer problem!

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools

You Are Using: Server ID
Trusted Website Back to the Top ↑