Forum     

Go Back   Digit Technology Discussion Forum > Software > Open Source
Register FAQ Calendar Mark Forums Read

Open Source A place where you can talk to like-minded people about the fastest growing software movement today! Discuss anything and everything about Open Source software and Operating Systems.


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 25-05-2005, 02:05 AM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: May 2005
Location: canada
Posts: 2
Default writing small http server in Java/C++


can anybody help me with that.
cheers
sarabdeep is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-05-2005, 10:22 AM   #2 (permalink)
In The Zone
 
Join Date: Feb 2005
Location: Anonymous
Posts: 204
Default

you cud have started @ www.programmersheaven.com , that's a good place for posting programming queries/help.
h4xbox is offline  
Old 25-05-2005, 12:03 PM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: May 2005
Location: canada
Posts: 2
Default thanks

thanks for the suggestion
sarabdeep is offline  
Old 25-05-2005, 03:35 PM   #4 (permalink)
In The Zone
 
#/bin/sh's Avatar
 
Join Date: Apr 2004
Location: 42.65 N 73.76 W
Posts: 213
Default

//**************************************
//
// Name: HTTP SERVER
// Description:This is a Fully fledged H
//This code came directly from "JAVA
//Network Programming" by Elliotte Rusty
//Harold.....
//

import java.net.*;
import java.io.*;
import java.util.*;


public class jhttp extends Thread {
Socket theConnection;
static File docroot;
static String indexfile = "index.html";



public jhttp(Socket s) {
theConnection = s;
}


public static void main(String[] args) {
int thePort;
ServerSocket ss;
// get the Document root

try {
docroot = new File(args[0]);
}


catch (Exception e) {
docroot = new File(".");
}


// set the port to listen on

try {
thePort = Integer.parseInt(args[1]);
if (thePort < 0 || thePort > 65535) thePort = 80;
}


catch (Exception e) {
thePort = 80;
}


try {
ss = new ServerSocket(thePort);
System.out.println("Accepting connections on port "
+ ss.getLocalPort());
System.out.println("Document Root:" + docroot);


while (true) {
jhttp j = new jhttp(ss.accept());
j.start();
}

}


catch (IOException e) {
System.err.println("Server aborted prematurely");
}


}


public void run() {

String method;
String ct;
String version = "";
File theFile;



try {
PrintStream os = new PrintStream(theConnection.getOutputStream());
DataInputStream is = new DataInputStream(theConnection.getInputStream());
String get = is.readLine();
StringTokenizer st = new StringTokenizer(get);
method = st.nextToken();


if (method.equals("GET")) {
String file = st.nextToken();
if (file.endsWith("/")) file += indexfile;
ct = guessContentTypeFromName(file);


if (st.hasMoreTokens()) {
version = st.nextToken();
}

// loop through the rest of the input li
// nes

while ((get = is.readLine()) != null) {
if (get.trim().equals("")) break;
}


try {
theFile = new File(docroot, file.substring(1,file.length()));
FileInputStream fis = new FileInputStream(theFile);
byte[] theData = new byte[(int) theFile.length()];
// need to check the number of bytes rea
// d here
fis.read(theData);
fis.close();
if (version.startsWith("HTTP/")) { // send a MIME header
os.print("HTTP/1.0 200 OK\r\n");
Date now = new Date();
os.print("Date: " + now + "\r\n");
os.print("Server: jhttp 1.0\r\n");
os.print("Content-length: " + theData.length + "\r\n");
os.print("Content-type: " + ct + "\r\n\r\n");
} // end try


// send the file
os.write(theData);
os.close();
} // end try

catch (IOException e) { // can't find the file
if (version.startsWith("HTTP/")) { // send a MIME header
os.print("HTTP/1.0 404 File Not Found\r\n");
Date now = new Date();
os.print("Date: " + now + "\r\n");
os.print("Server: jhttp 1.0\r\n");
os.print("Content-type: text/html" + "\r\n\r\n");
}

os.println("<HTML><HEAD><TITLE>File Not Found</TITLE></HEAD>");
os.println("<BODY><H1>HTTP Error 404: File Not Found</H1></BODY></HTML>");
os.close();
}

}

else { // method does not equal "GET"
if (version.startsWith("HTTP/")) { // send a MIME header
os.print("HTTP/1.0 501 Not Implemented\r\n");
Date now = new Date();
os.print("Date: " + now + "\r\n");
os.print("Server: jhttp 1.0\r\n");
os.print("Content-type: text/html" + "\r\n\r\n");
}

os.println("<HTML><HEAD><TITLE>Not Implemented</TITLE></HEAD>");
os.println("<BODY><H1>HTTP Error 501: Not Implemented</H1></BODY></HTML>");
os.close();
}

}


catch (IOException e) {

}


try {
theConnection.close();
}


catch (IOException e) {
}

}



public String guessContentTypeFromName(String name) {
if (name.endsWith(".html") || name.endsWith(".htm")) return "text/html";
else if (name.endsWith(".txt") || name.endsWith(".java")) return "text/plain";
else if (name.endsWith(".gif") ) return "image/gif";
else if (name.endsWith(".class") ) return "application/octet-stream";
else if (name.endsWith(".jpg") || name.endsWith(".jpeg")) return "image/jpeg";
else return "text/plain";
}

}
__________________
\"99 little bugs in the code, 99 bugs in the code, fix one bug, compile it again, 148 little bugs in the code. 148 little bugs in the code....\"
#/bin/sh is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by clmlbx
- by Charan
- by Sujeet
- by reddick

Advertisement




All times are GMT +5.5. The time now is 12:11 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2