!"# $ %!&' # &! ())*!$



Similar documents
Building a Multi-Threaded Web Server

Creating a Simple, Multithreaded Chat System with Java

Division of Informatics, University of Edinburgh

Application Development with TCP/IP. Brian S. Mitchell Drexel University

String sentence = new String(receivePacket.getData()); InetAddress IPAddress = receivepacket.getaddress(); int port = receivepacket.

Advanced Network Programming Lab using Java. Angelos Stavrou

Mail User Agent Project

Assignment 4 Solutions

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Question1-part2 What undesirable consequences might there be in having too long a DNS cache entry lifetime?

Lesson: All About Sockets

Java Network. Slides prepared by : Farzana Rahman

Threads in der Client/Server-Programmierung mit Java

Network Communication

TP1 : Correction. Rappels : Stream, Thread et Socket TCP

Introduction to Java. Module 12: Networking (Java Sockets) Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Network Programming using sockets

Learning Outcomes. Networking. Sockets. TCP/IP Networks. Hostnames and DNS TCP/IP

Java Network Programming. The java.net package contains the Socket class. This class speaks TCP (connection-oriented protocol).

Using Files as Input/Output in Java 5.0 Applications

CS506 Web Design and Development Solved Online Quiz No. 01

Data Communication & Networks G

Java Programming Unit 10. Stock Price Quotes with URL, Sockets, and RMI

public static void main(string[] args) { System.out.println("hello, world"); } }

Threads & Tasks: Executor Framework

Manual. Programmer's Guide for Java API

This material is built based on, Patterns covered in this class FILTERING PATTERNS. Filtering pattern

Chapter 2 Introduction to Java programming

INPUT AND OUTPUT STREAMS

DNS: Domain Names. DNS: Domain Name System. DNS: Root name servers. DNS name servers

! "# $%&'( ) * ).) "%&' 1* ( %&' ! "%&'2 (! ""$ 1! ""3($

Network/Socket Programming in Java. Rajkumar Buyya

Socket-based Network Communication in J2SE and J2ME

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Socket Programming. Announcement. Lectures moved to

13 File Output and Input

ExempleRMI.java. // Fichier de defintion des droits et proprietes // System.setProperty("java.security.policy","../server.java.

Transport layer protocols. Message destination: Socket +Port. Asynchronous vs. Synchronous. Operations of Request-Reply. Sockets

The Java I/O System. Binary I/O streams (ascii, 8 bits) The decorator design pattern Character I/O streams (Unicode, 16 bits)

Socket Programming in Java

Socket programming. Socket Programming. Languages and Platforms. Sockets. Rohan Murty Hitesh Ballani. Last Modified: 2/8/2004 8:30:45 AM

Files and input/output streams

An Overview of Java. overview-1

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

EVALUATION OF TOOLS FOR CYBER SECURITY

public class demo1swing extends JFrame implements ActionListener{

Apéndice C: Código Fuente del Programa DBConnection.java

gomobi Traffic Switching Guide Version 0.9, 28 September 2010

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Input / Output Framework java.io Framework

Word Count Code using MR2 Classes and API

CISC 4700 L01 Network & Client- Server Programming Spring Harold, Chapter 8: Sockets for Clients

What is an I/O Stream?

COSC 6397 Big Data Analytics. Distributed File Systems (II) Edgar Gabriel Spring HDFS Basics

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

A Simple Client-Server Application

Using DOTS as Apache Derby System Test

Hadoop WordCount Explained! IT332 Distributed Systems

WRITING DATA TO A BINARY FILE

An Android-based Instant Message Application

When the transport layer tries to establish a connection with the server, it is blocked by the firewall. When this happens, the RMI transport layer

COSC 6397 Big Data Analytics. Mahout and 3 rd homework assignment. Edgar Gabriel Spring Mahout

Crash Course in Java

Tutorial: Getting Started

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Lecture J - Exceptions

Event-Driven Programming

Xiaoming Gao Hui Li Thilina Gunarathne

Continuous Integration Part 2

CS 1302 Ch 19, Binary I/O

File I/O - Chapter 10. Many Stream Classes. Text Files vs Binary Files

NETWORK PROGRAMMING IN JAVA USING SOCKETS

php-crypto-params Documentation

Building a Java chat server

Communicating with a Barco projector over network. Technical note

Chapter 20 Streams and Binary Input/Output. Big Java Early Objects by Cay Horstmann Copyright 2014 by John Wiley & Sons. All rights reserved.

CS 111 Classes I 1. Software Organization View to this point:

Istanbul Şehir University Big Data Camp 14. Hadoop Map Reduce. Aslan Bakirov Kevser Nur Çoğalmış

ResellerPlus - Bulk Http API Specification. (This Document gives details on how to send messages via the Bulk HTTP API for the RouteSms SMPP System)

Programmation Orientée Objets. et langage Java

Capario B2B EDI Transaction Connection. Technical Specification for B2B Clients

Java Fundamental Classes Reference

Stream Classes and File I/O

SETTING UP YOUR JAVA DEVELOPER ENVIRONMENT

Consuming a Web Service(SOAP and RESTful) in Java. Cheat Sheet For Consuming Services in Java

Network Programming. Writing network and internet applications.

Java Programming: Sockets in Java

3.5. cmsg Developer s Guide. Data Acquisition Group JEFFERSON LAB. Version

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

Java Programming Fundamentals

Running Hadoop on Windows CCNP Server

READING DATA FROM KEYBOARD USING DATAINPUTSTREAM, BUFFEREDREADER AND SCANNER

Transcription:

!"# $ %!&' # &! ())*!$

!" #!$ %& ' ( ) * +,--!&"" $.!! /+010!"

+!, 31! 4)&/0! $ #"5!! & + 6+ " 5 0 7 /&8-9%& ( +" 5)& /*#.! &( $ 1(" 5 # ( 8 +1 + # + 7$ (+!98 +&%& ( +!" (!+!/ (!-. 5 7 (! 7 1+1( & + ":! + + 7 " (&, 71( $ $ 1( 0" 5;<=>/ &&! 9?@!=0 1"A!070$ 1! $0&7 B & + " 5@?9 1& 0"&7$ +1 &&70$ &&$1 &" 71( + 1( $"C $ 0/ & +" 2

(!(!/0&1&2 5 @?9 E( 7 " 5@?9 0 = 12+"5@?98-9;" ("5 0+!( * (.0 " (01 7 ( & ( " 5 & +! *9."51107+ + *8.+0 +" 7&"";""""+ & (" 5F'8=<;/ 0 9071!1" 5 $! 1 (1+" 5! 9&2 0G 27(1" 5 1 ( 1 1 " D

590 + " (1& 70$ 1! 1& "5 &0$ 7 $ 1 777 7 & +" " 9 7 1 7 7 1 " 8 & 70$ +"&!& "9 7 0( $1&7" 5 8/ F'8 =</ 0 " 8! ( 7 0( 0 " 1! & "#! $ 1 1 7 + (" 5+& 1 & $+ 1 " 3$+1( 4*+"0+1. " : / $ (" 5>E /; &7 (" & $+ 1 IJ 7 4! ((" 58+10+ " $0 " & &7, 5K/,?LM?NOP K5/, 58?NOP?LM?NOL 5K/, 58?NOL 5" 0$ + (0 0" H

85 8K?NO;2?LM?NO58O;D?LM58?NO;D58O258!" & +1 " 8! 0 0 & & $ ( 1 &"5 1 1 $&" 5 +1 0 & 0 '9M &!" 0,70! & 1 0(0+!+1 +1"5 $ ( +1 77! $ " # $%& 59/1+7 +C3/ +8/!"C3/+!"#*3M?. 0 7& &91 "8+ $ *'./ +!*?:@5@ 9:5.&QQQ *#." (!*!/ 3 5 $ $! +1 (+&((!$"5%& &!1/+!1" C G 0$ 1(*98.&( $ 1( &+ =

& 1/ " 5 1/ 1!$B $ & ( " 5 1/+ + E &!" $ ( *&. B*.+77 & " 5 & &! 0$( " & 1/! ( *+.! 10&&!&7"5! 1/! & & & 1 1/ 1& $ $ 1(" 5 +1 0+ $ 0 " 0 " 51&/ 1 1 ++ & "# $ & 1 +!&70!0 " 0 & 1! 1/! $ 1( $ 1( ( 0 1 &! 1 0" & & + $ ++ &"1 7 +1 7 (! 1 & " 3+ *+"C3.! 01/+$ 0"1(0 " #$ '$?1/ 0 ( +1 $ + " 5 7 ( 1 0 $ " 9 & + G+1 ( 1 " 5 1/ ( 0 + 7 11(0 (+ 1 "! 0 +&(!( + 1((!(" 5 ( 47 & &$ + (+1(07$ "++ 9 +1( ( & ( R

+ +" 8/ &! * + ". (!4!/-.5 &3 5#+ 7& &9 " 7 &(! #->"< & " # 0& #-;">! F'8 H;H/ F'8 H;=/ 0! & &7 #-;"; & " 5 + &0 + " 5 + ( 8 +1 + &! 7 8 0 " :& ( +1 + &( * " 0/$+G. 1 & + /0&" 5+E&+"5+&7 E&& 4 #/ ( 4 &( ( "5 1(,+10+ $ #&&"5+1 0! &0!77+" 0 ( 7I?+J! " 5#7, 5#+ 170 #0!1 " 0! +,! "# $%&'"'( % )$$# )****( +,- ).$$*"/0123+3124*"'3#,%+3)'"*56//7'//8( - ) 9 9$$ 9$$ 9# $:9$ 9 # $3;</"= 9 $3;</">?3;</"6@@3;</"'( -,A )#, 3;</"*( -,)$ ( -,B# )CD,>>*=,6,>3;</"8@3;</"8( E,-$)7//( B ),$( <

( 5 S! & ($! " Q ) 8FTA' C G + A'" 5 0!/! & " " 5 7 # + 1! $! & 70" -$1- U11" 0"5 (#-;";! +#;";&(!&" 5&7/0!1 01( 7"5F'8H;H/ 2< 0!1& 0 &"+ +7, '!1 # C/5 511+ % 570 / (!+!"G& 0 " 5 *! 7.!" 5 0 +$B IVOJ +$ +0 " 511+/A $ 50 &07" 511+/ 1 50(+0$"$I+J! +/+$ $ $ " 511+/8 +/5& 8 1 5 0" 8 #-;";/ "9! >>+(! & / &! &$ +1 " 5 0!1 +10! &! &7+G& " 5#&7, /&($ W0!1 W 5& +$&7, %&'"'6//DE( F )C 6GD 6//7'*)*6)'6.( C).1HC"/"6( B,A #)>/( B,) 9 # $( ( I# $JI#JI $J( %$$I $JI#J( IHJ%$$IHJI# $J( ;>

5& #&(! & 7"!7 ($(&&&"&&0!1 &+( &" 5 0!1! &" 5 8 + R ( & +7 1 & *+"+&(". 5 0!10, '!1?& 8 /A 8 /+ % 5& (&!" 5&!/ " 5+$" 5# + 1 + 1 @?" 5 70 &7/ & / @? 0& 4, & " ($) $ 5 # 7 $ +1 0+&$"5 %&'#!&0 $ & +1 "5% /$ &! +1" 5 4 & )& +!$ $ $ " *!/" 5%& & +&7 (/!&" 1!" A 7 & /+ / +1 ;;

" %&! 8 +! /+$ 1(&(" 5%& & +8-9 (+1 1/!$+ 1(!0K?3 1/! " *!+!637 5 %&/ 1/ &( 0 7 K?3 1 01/ 0 "?1/(#))&+ & $ " 5$ 1(%&07 (/&(($& &(!$ "! 7 1 & 9-@ 4&" 5 $ 1( 0!& (#))& ($& 1/ "?& 1 (#))#& $ $+! 1/&!$ " serversocket = new ServerSocket(4444); catch (IOException e) { System.out.println("Could not listen on port: 4444"); System.exit(-1); 5+10+ & ( & ($ $$ 0 " "" + 07 &! 1, Socket clientsocket = null; clientsocket = serversocket.accept(); catch (IOException e) { System.out.println("Accept failed: 4444"); System.exit(-1); C3 1 &!$ 7 $ 1( %& (#))!*&(#))!+&&" 5 $ 1( + $ &!$" A 7 1 "!& $ 1( 0 7 (,) 5 -+ ($ ;

!"! 1 5 +1( & ( / 0 +$00 " C3 G 7 & 0 ( 1!" 5.#))&&#&+ 1(! +( 07 7 8/ & + &(( "!$+ ( * &!$(. *101.&!" 4!/.38 5 #-;"; 0+ $ 1 +!$"?4)&0+ 1"5&+ +1 0 " 5+&7 + 0$!$ G 0 (+1 7 7+& +$00 " 7 ( 7 $ $!&"? & D (" + &7, Q&,07++1007?&,$! ++ &0++1 8 1 :, 01(0++1( Q1 3, &47$/1 $&(! 8 0,)& 0$ (+ 4!+!/039 5 &40+4"8!0++1 " 8$+ $ + + ;

+!( 7" +1 0+ " & " # ($" 5 +" ($ 0++1!&" 4!*!/0 58 0+)& 0$ (+ "? $ 7 + ++ 7 $0/#)0& +0" 5 )&" 0 4 7 & 0" 5 )& 0$1(! $!, &( ($ & *+" && & 1 &$ + B )&" & (& ". 0 0$0 &("5/ 4 " 5 & $ 07 +/0$"X!! 0/ & 70 &!$*+"!." M + 0+ 7 +1 G!Y 5 )&" 0& (, K9B #9 H $ 9B <GL 5+(, if (templine.touppercase().startswith("maxconnections")){ int index=templine.lastindexof("="); buffersize=integer.parseint(templine.substring(index+1)); catch (Exception ex){ System.out.println("Config(): Set maxconnections to "+buffersize); 5$00?&( 7 +! 0$ +" IOJ!$ & &" 58 0 $ 7& 7! +, ;2

A 5 & & +7 70 "5& & &" G8 1 5G 0+ 7+1" 6+ 1 #!& /" @$ : +1 & ( "#7!$" $00? 570 +$00" 5+0 $ " 4!*!/: ($+++!!"5 ($$ $&(($*(!$+!!.4 + "Z ($(+ " public static void main(string args[]) { WebServer myserver=new WebServer(); myserver.serve(args); 5#++01($&& 1"5+ $ & +! + + 1 +" 5 + $ $+ 0 7, $ +<>>>/++" 5 ($ +! + &( $ &)& 0$1(!"5+ $!#&' *& 0$. + " 1/ 7 1 1 +$ 0 1! 0 ++ 10 1!" ServerSocket srvr = new ServerSocket(port); System.out.println( "Serving requests on port "+port+"..." ); Socket tempclientsocket; (# *+ 5 $!$ $ %& +"5";"+!&7+1( ;D

($ && $$ 0 " 5 11+ ($ & +! + 0$& 7 1 & 0$ + "! + 1!&7)& 10" 0$ & +$ (&!(& &0$( +" &$ & + & 8C 7+$ "Z (& & " A $ # 2 +$!$ & ( 0 1 0+1$ &(!$" ServerThread tempthread; while( true ) { if (ConnectionManager.getNrOfConnections()<Config.maxConnections){ tempclientsocket = srvr.accept(); tempthread=new ServerThread (tempclientsocket); System.out.println("Accepting connection, this is nr. "+ConnectionManager.getNrOfConnections()); ConnectionManager.addConnection(); tempthread.start(); else{ System.out.println("Error: too many connections, denying request."); 0 7 7 +/ 0$1( G +1" 5 ($ 1 0 1!& & # 2 + +!$ "5+1 &!!!($ &!$" '! 7 0$ 0$ "5%&7!7(&! +! 70$"5 &$ # 2 (!!$+ 0" 5 ( 0 70 1 1(" ;H

4!4!6"2 5 $ & 1 & ( 0 1! 7 3 +$!$ " + # & / " 5 &(*5 (!$., ($ 0$ %& 7! &! 0$ (" 7 7! #:A! &" %& / 7 &! 4 " 5 7 7 " 7!$ $ " outtoserver = new PrintStream(clientSocket.getOutputStream() ); infromserver = new BufferedReader(new InputStreamReader( clientsocket.getinputstream())); 5 & +,, & +, ($ 17 $( & 7 0 &(( / &" 5 /4!$(+ (!$ $ "1 $47/1 $ &! / 47 I $J /0$ $ $! &B+" ($ & ( / 4, #))))) &" 5 / 47 I $ J 0 &( & & +$00 &(" $ / &$ $#-($ &&"5-E ( ( ($ & 0$(&! &( + " + 1 $ ( & " 5 & &$ "'! 1 & & 0!1(B $ 7& " 5 7.#),)+ & &!$" 5 7! IJ &!$#*($B F'8 H;H! I2>> K FV$J $ $ & 456,($" ;=

5 & ($ & $ *& ( &( 0.0!7 1 (" ((,-.,/0 5 ($ 7 + &$ 7 70 (!! &&& 7 +/ " 1 $& " M 7 & I511+/ 1 J/I+J! I+BVO>J! +0!&" for (int i=0; i<requests.size();i++){ if ((requests.get(i).tostring().touppercase().startswith("accept- ENCODING")) && (requests.get(i).tostring().touppercase().indexof("gzip")!=-1)) { clientsupportsgzip = true; if ((requests.get(i).tostring().touppercase().indexof("gzip;q=0.") ==-1) && (requests.get(i).tostring().touppercase().indexof ("GZIP;Q=0")!=-1)){ clientsupportsgzip = false; 57D"+ 7& 01(" &7!$ +70/!&!&0!1, $ $ &! & +$" 5 4 &1#:A0/$ IG- J+$ +$ " infile=new FileInputStream(requestedFile); outtoserver.print("http/1.1 200 OK\r\n"); currentdate=new Date(); outtoserver.print("date: "+currentdate.togmtstring()+"\r\n"); outtoserver.print("server: Mini WebServer v."+config.version+"\r\n"); outtoserver.print("content-type: text/html\r\n"); 5 70& +$00 &&" 4!$ ($ && 0! ( ;R

*8FTA'A'.70$ 70($ (! &(" 5 +$00 + ( &$ $ +$ 7 ( " 5 $) ($ & &$ 0/ (!& 1!" byte [] buffer = new byte[config.buffersize]; try{ int numbytes = infile.read(buffer, 0, Config.bufferSize); while (numbytes!= -1) { outtoserver.write(buffer, 0, numbytes); numbytes = infile.read(buffer, 0, Config.bufferSize); 5%&&4 7!& 4"# 0 $"$,7&( 7 2 &! I2>2E'M'$ J " 5?& &($ +1 + + " 1 &! 8 1 : # ($ && 1 & & +1 " 5 & ($ (Q1 3 &!" 4!;!/:3"%3"# 5 & 4 70$ ( &*+" 8+1 (1&." (&(+7& ( & & $" X 70 +$ IJ +1 & G 0 ( +1 " G +1 IJ + +1!+1 0 $ " 5+ " Q1 3 1 $&! ": +1 0 $ Q1 3/ 0 0! +1 &" 5 & 0$1(! 7 &1 +1!$" ;<

5 Q1 3 7&( 0 $( &( +1 +1 & " 8C/7& &7" public void run(){ while (canrun){ sleep (3000); catch (InterruptedException ex) { counter+=3; if (counter >= Config.timeOut){ theserver.killconnection(); canrun=false; # ( 0$1( Q1 3 +1!+7!$&($ &&!+1" 5($ $("5( &$ & ($"' & 0$1(!,, & +$00 &$ $/ 70$ +$00 & & $Q1 37+&!+1" +1 5, 7&( 0!$ Q1 30!0$" ;! 8 5 7! # 0 7 "! ( 6+ & CM9P 8+ &! 07 0!11! *+" 8 / 1, +. 70 (! + ( &+ 01&! " >

5! )&! & "A!$! &7/$++17! " ;!+!.3 5 %& 47 4 &" : *8,, 47 & 1! 0 " 5 *8,,!&"$"+ 1 (" outtoserver.flush(); mywatchdog.reset(); gzout=new GZIPOutputStream (outtoserver); int numbytes = infile.read(buffer, 0, Config.bufferSize); while (numbytes!= -1) { mywatchdog.reset(); gzout.write(buffer, 0, numbytes); numbytes = infile.read(buffer, 0, Config.bufferSize); gzout.close(); 5, #),2 &!$ +$00 ( 1 & &" 5%& $ 7 1 +, ($&, # ($&!$ "' *8,, + & 7&7! $ $ & 1! 69 0!1 $ & "51 +,) &!$" ;!(!8 5 0/ 7! & "! 4+1&! +" 5 8C!! &!$ 0/ & B& & " X +$( 07 07 &" (+1! " 11 ;

"9 (07 1$(" 0/ &!$0/!" 5 & 0/*1 G ". & &" HDR 7! #:A ;2; P 0/ 4!! 69&"5 &7 (, +$ :7*K. :$ *K. [ #:A ''*=*> 6*''L 6'G*M P G/G** ==78 'G7>M : +$ & $ 69 " 5 ( 0 1 7 ( &"# & 0/( 0 0! QK 1#:AP("5+&(%&1 &$' 1( 7 & (&" 5 : 7 0!7 $ &" 5 8MM +!! &" : R"R + 4 4&H2"2+1 ;R2"2+1 ">[/& "

<=9:68 9:68 28 /** Mini Web-Server *This is a mini web server for demonstration purposes. *It is written by Istvan Farkas with the very useful help of Gabor Paller. */ import java.io.*; import java.net.*; import java.util.*; import java.util.zip.gzipoutputstream; import java.text.*; class WatchDog extends Thread{ private ServerThread theserver; private int counter=0; private boolean canrun=true; WatchDog(ServerThread theserver){ this.theserver=theserver; this.setdaemon(true); public void reset(){ counter=0; public void disable (){ canrun=false; public void run(){ while (canrun){ sleep (3000); catch (InterruptedException ex) { counter+=3; if (counter >= Config.timeOut){ theserver.killconnection(); canrun=false; // run //---------------------------------------------------------- class ServerThread extends Thread{ private Socket clientsocket; private Vector requests; // vector of all lines of request private Vector currentrequest; // elements of request line being parsed private String beforelaststringread,laststringread; // the last string read while reading the req private PrintStream outtoserver; private GZIPOutputStream gzout; private BufferedReader infromserver; private Date currentdate; //will get value when comm ing with client private StringTokenizer tokenizer=null; //used to tokenize the current

private WatchDog mywatchdog; request ServerThread (Socket clientsocket){ this.clientsocket=clientsocket; requests=new Vector(); currentrequest=new Vector(); mywatchdog=new WatchDog(this); mywatchdog.start(); public void serveget(){ String filename=null; // name of requested file File requestedfile=null; //the requested file as an object FileInputStream infile=null; boolean clientsupportsgzip=false; // Now serving a get. At the call of this function, currentrequest contains line 1 of the request. if (currentrequest.get(1).tostring().equals("/")) { filename = "index.html"; else { filename = currentrequest.get(1).tostring(); System.out.println("serveGet(): Requested filename: "+filename); //now detect whether client supports gzip for (int i=0; i<requests.size();i++){ if ((requests.get(i).tostring().touppercase().startswith("accept- ENCODING")) && (requests.get(i).tostring().touppercase().indexof("gzip")!=-1)) { clientsupportsgzip = true; if ((requests.get(i).tostring().touppercase().indexof ("GZIP;Q=0.")==-1) && (requests.get(i).tostring().touppercase(). indexof ("GZIP;Q=0")!=-1)){ clientsupportsgzip = false; // false if q=0 but not q=0.x requestedfile=new File(Config.basePath,fileName); infile=new FileInputStream(requestedFile); mywatchdog.reset(); outtoserver.print("http/1.1 200 OK\r\n"); currentdate=new Date(); outtoserver.print("date: "+currentdate.togmtstring()+"\r\n"); outtoserver.print("server: Mini WebServer v."+config.version+"\r\n"); outtoserver.print("content-type: text/html\r\n"); if (Config.enableGZipEncoding && clientsupportsgzip) { outtoserver.print("content-encoding: gzip\r\n"); else { outtoserver.print("content-length: "+requestedfile.length()+"\r\n"); outtoserver.print("\r\n"); 2

if (Config.enableGZipEncoding && clientsupportsgzip){ //GZipped byte [] buffer = new byte[config.buffersize]; try{ outtoserver.flush(); mywatchdog.reset(); gzout=new GZIPOutputStream (outtoserver); int numbytes = infile.read(buffer, 0, Config.bufferSize); while (numbytes!= -1) { mywatchdog.reset(); gzout.write(buffer, 0, numbytes); numbytes = infile.read(buffer, 0, Config.bufferSize); gzout.close(); catch (IOException ioex){ System.out.println("DEBUG: Exception occured while GZiptransferring file."); ioex.printstacktrace(); else { //normal, uncompressed transfer byte [] buffer = new byte[config.buffersize]; try{ mywatchdog.reset(); int numbytes = infile.read(buffer, 0, Config.bufferSize); while (numbytes!= -1) { mywatchdog.reset(); outtoserver.write(buffer, 0, numbytes); numbytes = infile.read(buffer, 0, Config.bufferSize); catch (IOException ioex){ System.out.println("DEBUG: Exception occured while transferring file."); ioex.printstacktrace(); // end of else infile.close(); catch (Exception ex){ System.out.println("DEBUG: error occured while closing input file"); catch (FileNotFoundException ex){ // 404 String messagecontent="<html>\n<h1>404 - File Not Found</H1>\n"+ "Mini WebServer could not find the file you requested.<br>\n"+ "The filename you specified was: <strong>"+filename+ "</strong><br>\nplease try again.<br><br>mini WebServer v."+config.version+ " - written by Istvan Farkas in 2003\n"+ "<br>many heartfelt thanks to Gabor Paller. </HTML>"; mywatchdog.reset(); outtoserver.print("http/1.1 404 File Not Found\r\n"); currentdate=new Date(); outtoserver.print("date: "+currentdate.togmtstring()+"\r\n"); outtoserver.print("server: Mini WebServer D

v."+config.version+"\r\n"); outtoserver.print("content-type: text/html\r\n"); outtoserver.print("content-length: "+messagecontent.length()+"\r\n\r\n"); outtoserver.print(messagecontent); return; public void tellbadrequest (){ // Called on unsupported HTTP command String messagecontent = "<HTML>\n<H1>400 - Bad Request</H1>\n" + "Sorry, but Mini WebServer does not support the HTTP command you requested.<br>\n" + "\nplease try again.<br><br>mini WebServer v."+config.version + " - written by Istvan Farkas in 2003\n" + "<br>many heartfelt thanks to Gabor Paller. </HTML>"; mywatchdog.reset(); outtoserver.print("http/1.1 400 Bad Request\r\n"); currentdate = new Date(); outtoserver.print("date: " + currentdate.togmtstring()+"\r\n"); outtoserver.print("server: Mini WebServer v." + Config.version+"\r\n"); outtoserver.print("content-type: text/html\r\n"); outtoserver.print("content-length: " + messagecontent.length() + "\r\n\r\n"); outtoserver.print(messagecontent); return; catch (Exception ex) { System.out.println("DEBUG: Exception occured while writing to outtoserver"); public void killconnection(){ // to terminate connection System.out.println("ERROR: No active transfer for the determined timeout period - closing socket."); System.out.println("Closing connection on port "+clientsocket.getport()+" with address: "+clientsocket.getinetaddress()); ConnectionManager.removeConnection(); try{ clientsocket.close(); catch (Exception ex){ System.out.println("Debug: exception occured while closing the timed out transfer socket."); public void run(){ System.out.println("Activated connection on port "+clientsocket.getport()+" with address: "+clientsocket.getinetaddress()); outtoserver = new PrintStream( clientsocket.getoutputstream() ); infromserver = new BufferedReader( new InputStreamReader( clientsocket.getinputstream())); //now read all requests into a vector do { H

laststringread=infromserver.readline(); requests.add(laststringread); catch (IOException ex){ while ((laststringread.length()>0)); tokenizer=new StringTokenizer((String)requests.get(0)); while (tokenizer.hasmoretokens()) { currentrequest.add(tokenizer.nexttoken()); // Calls to serve different HTTP commands if (currentrequest.get(0).tostring().touppercase().startswith("get")) { serveget(); // else if... else { // Unsupported command tellbadrequest(); // The approriate function has finished serving request, now close connection. System.out.println("Closing connection on port "+clientsocket.getport()+" with address: "+clientsocket.getinetaddress()); ConnectionManager.removeConnection(); mywatchdog.disable(); clientsocket.close(); // The Thread will finish after this point. catch (IOException ex){ ex.printstacktrace(); //-------------------------------------------------------------------- class Config{ //here we declare the config variables as static ones. We also give them default values. static final String version="0.2"; static String basepath="."; // the directory from which we are serving files static int buffersize=1024; static int maxconnections=64; static boolean enablegzipencoding=true; static int timeout=60; Config(){ //The constructor will read config from WebServer.ini BufferedReader inifile=null; inifile = new BufferedReader( new FileReader("WebServer.ini")); catch (FileNotFoundException ex){ System.out.println("Unable to read WebServer.ini, falling back to default configuration values..."); return; String templine=null; // the last line we read while parsing ini file do { =

templine=inifile.readline(); if (templine.touppercase().startswith("basepath")){ int index=templine.lastindexof("="); basepath=templine.substring(index+1); System.out.println("Config(): Set basepath to "+basepath); if (templine.touppercase().startswith("buffersize")){ int index=templine.lastindexof("="); buffersize=integer.parseint(templine.substring(index+1)); catch (Exception ex){ System.out.println("Config(): Set buffersize to "+buffersize+ " bytes"); if (templine.touppercase().startswith("maxconnections")){ int index=templine.lastindexof("="); buffersize=integer.parseint(templine.substring(index+1)); catch (Exception ex){ System.out.println("Config(): Set maxconnections to "+buffersize); if (templine.touppercase().startswith("enablegzipencoding")){ int index=templine.lastindexof("="); if (Integer.parseInt(tempLine.substring(index+1))==0) enablegzipencoding=false; catch (Exception ex){ System.out.println("Config(): Set enablegzipencoding to "+enablegzipencoding); if (templine.touppercase().startswith("timeout")){ int index=templine.lastindexof("="); timeout=integer.parseint(templine.substring(index+1)); catch (Exception ex){ if (timeout<10) timeout=10; System.out.println("Config(): Set timeout to "+timeout+" seconds."); catch (Exception ex){ while (templine!= null); //---------------------------------------------------------------------- class ConnectionManager{ private static int nrofconnections=0; public static void addconnection(){ nrofconnections++; R

public static void removeconnection(){ if (nrofconnections!=0) { nrofconnections--; public static int getnrofconnections(){ return nrofconnections; //---------------------------------------------------------------------- public class WebServer { int port=9000; // Where we are listening, default value is 9000 public void serve(string args[]){ System.out.println("\nSimple WebServer version "+Config.version+" \n"); port=integer.parseint(args[0]); catch (Exception ex){ System.out.println("Error parsing input port from args[0], falling back to default (9000)."); Config myconfig=new Config(); ServerSocket srvr = new ServerSocket(port); System.out.println( "Serving requests on port "+port+"..." ); Socket tempclientsocket; ServerThread tempthread; while( true ) { if (ConnectionManager.getNrOfConnections()<Config.maxConnections){ tempclientsocket = srvr.accept(); tempthread=new ServerThread (tempclientsocket); System.out.println("Accepting connection, this is nr. "+ConnectionManager.getNrOfConnections()); ConnectionManager.addConnection(); tempthread.start(); else { System.out.println("Error: too many connections, denying request."); catch( IOException ex ) { ex.printstacktrace(); System.out.println("Could not create server socket, maybe already in use."); return; public static void main(string args[]) { WebServer myserver=new WebServer(); myserver.serve(args); //---------------------------------------------------------------------- <

" 2 F'8=<; +,--)))"0V"-01-01=<;" F'8=< +,--)))"0V"-01-01=<" F'8H;H +,--)))"0V"-01-01H;H" 5 )?" $,?(+/ ( * / 1/#;<<<. % 1K,0 1,#1+ +,--)))""-+!1-+1 -+- K$$ ),#)%&&Q +,--)))" "1-+$--!&->>->2--!&U)&" N$#": $,?1+ %&,$ +,--)))"!&)"1-!&)-!)/;/;<<H-!)/;/1"?$ %&$,8$M) +,--!&"$ "1-1--$- ) -1-1?&" >