Thursday 28 July 2011

Http Server Implementing GET

Hi all,

Had to create a very very simple http server, that only implements the GET function, in Java for one of our assignments.
When run you can get file from your computer using http://127.0.0.1:8001/filename.jpg.

Could use this kind of thing for some multiplayer game in the future.

Am currently working on an augmented reality program. Should be AWESOME!


Am also testing out a new code formatter. Hopefully things become readable. Unfortunately tabs do not appear as tabs... Big thanks to David Craft.

Here's the code for the server.

import java.net.ServerSocket;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.Socket;
import java.util.StringTokenizer;

/**
 * @author Milk
 *
 */
public class HttpServer {

 /**
  * 
  * @param args
  */
 public static void main(String[] args) {
  try {
   ServerSocket socket = new ServerSocket(8001);
   InetAddress serverHost = InetAddress.getLocalHost();
   System.out.println("Server destination: " + serverHost.getHostAddress() + 
     ", "+ socket.getLocalPort());
   // Repeatedly handle requests for processing.
   while(true){
    Socket clientConnection = socket.accept();
    System.err.println("Connection established.");
    BufferedReader in = new BufferedReader(new InputStreamReader(
      clientConnection.getInputStream()));
    DataOutputStream out = new DataOutputStream(clientConnection.getOutputStream());
    // Read string.
    String inString = in.readLine();
    System.out.println(inString);
    StringTokenizer st = new StringTokenizer(inString);
    st.nextToken();
    String request = "." + st.nextToken();
    // Read the requested file.
    FileInputStream f;
    try{
     f = new FileInputStream(new File(request));
     sendFile(f, out);
    } catch (FileNotFoundException e){
     try{
     // Display 404 screen.
     f = new FileInputStream(new File("./404.jpg"));
     sendFile(f, out);
     } catch (FileNotFoundException e2){
      out.writeBytes("HTTP/1.0 404 Not Found\r\n");
     }
    }
    // Close this connection.
    clientConnection.close();
    System.err.println("Connection closed.");
   }
  }
  catch(IOException e) {
   e.printStackTrace();
  }
 }
 
 /**
  * Sends the file.
  * @param f The file to be sent.
  * @param out The DataOutputStream.
  * @throws IOException
  */
 private static void sendFile(FileInputStream f, DataOutputStream out) throws IOException{
  // Send the file byte by byte.
  while(true){
   int b = f.read();
   if (b == -1){
    break;
   }
   out.write(b);
  }
 }
}

Cheers,
Milk

Wednesday 20 July 2011

General Life Update

Hello all,

A general life update seems to be in order. And what better way to do it then with colon separated sections.
Python anyone?

Holidays:
    LAN party:
        Some guy had 8Tb worth of games and movies. Needless to say I have a lot of new games.

    Work:
        So much work. Lots of money for me though.
        #This isn't me, it was the result of Google searching 'dollar dollar bills yo'.


Back at University:
    Marks:
        I got a B+, 2 B's and a B-. Solid B average student.

    This semester:
        Using C and C++, also doing http and web servers, so expect something awesome this semester.
        Maybe a new website?
        Or a multiplayer game?

    Project:
        One of our projects involves creating motion capture technology etc. That will be awesome.


Games:
    #The most important section.
    Starcraft II:
        The ladder is now locked. Waiting on the next season.
        I still suck. Bronze league.
        Watch this video. If you only ever watch one SC2 video, this should be it.

    Fire Emblem - Sacred Stones:
        Friend gave this to me yesterday. It's a GameBoy game.
        SO GOOD!
        Play that shit.
   
    New game:
        Obviously I have left Tanks for too long now.
        Going to make a new game with the help of someone who has the interest.
        #Foo, if you have an idea and the time let me know, comment.
        Looking to maybe do something similar to Fire Emblem, and Final Fantasy Tactics.


Thats about it. Will keep you posted on interesting finding etc. Hopefully I get something useful out of this semester.

Cheers,
Milk