View Single Post
  #1 (permalink)  
Old 07-04-2007, 12:56 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,119
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Icon11 Tutorial: How to create a web server?

In this tutorial, I'll introduce you on how to create a Web Server. It's fairly super simple and stupid program. However, in this tutorial, I'm just going to give you only a simple introduction that you can built yourself upon by modifying the tutorial or to improve it!

Of course, hopefully by now, you understand on how Multithreading works, if not, then you probably need to consider that and learn it first before getting into the networking part because we will use multithreading to create web server that will listen each of the client's request.

Introduction to Networking Application

Server
- I'm assuming that all of you are already know what does this thing do on your networking application right...? hopefully...
- Depending on your application, you will need to configure this. But in this tutorial, I'll create a web server application that will just listen to the client's request and send back the request to the client's browser.

Client
- Requesting the page that want to be seen and hosted in the server.
- Using web browser to send these requests.

Background of Networking Application
Server needs something to communicate to the client, how does Server and Client communicates? Well, they're basically communicating through SOCKET. Please google for the detailed information of Socket, I'm not going through it one by one... sorry... However, so that more than one application can run at the same time in one PC, what do we need to have a port. There are total for about 36000 ports in the standard PC, it may be different depending on your hardware. I think about the first 2400 are reserved for the application run by the Administrator of the PC. (these numbers are only estimation, so don't sue me if you answer this and you get an F on your exam..)

Server needs to specify PORT in which the application will be used. And client will need to IDENTIFY to which server to connect to by identifying the IP addresses and to which port to which it will send the request to. Server is just listening to its own port, so Server doesn't need to identify to which one it should connect to, because it is ITSELF!!! sorry if i'm just being so confusing for some people, but just want to make it *hopefully* clear..

Server.java

PHP Code:
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
 
public class WebServer {
 
private final static int port 90;
 
public WebServer() {
  
try {
   
ServerSocket ss = new ServerSocket(port);
   while (
true) {
    
Socket sock ss.accept();
    
ServerListener sl = new ServerListener(sock);
    
sl.start();
   }
  } 
catch (IOException ioe) {
   
System.out.println("IOException: " ioe.getMessage());
  }
 }
 
 
public static void main(String[] args) {
  new 
WebServer();
 }

ServerListener.java
PHP Code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.StringTokenizer;
 
public class ServerListener extends Thread {
 
 
private Socket sock;
 
public ServerListener(Socket sock) {
  
this.sock sock;
 }
 
 
public void run() {
  
try {
   
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
   
String line br.readLine();
 
   
StringTokenizer st = new StringTokenizer(line" ");
   
String file st.nextToken();
   
file st.nextToken();
   
file file.replaceAll("/""");
 
   
BufferedReader fr = new BufferedReader(new FileReader(file));
   
String fileLine fr.readLine();
 
   
PrintWriter pw = new PrintWriter(sock.getOutputStream());
 
   while (
fileLine != null) {
    
pw.println(fileLine);
    
pw.flush();
    
fileLine fr.readLine();
   }
   
br.close();
   
fr.close();
   
pw.close();
  } 
catch (IOException ioe) {
   
System.out.println("IOException: " ioe.getMessage());
  }
 }

Remember that we will need to listen to the HTTP protocol in order to listen HTTP request. You will probably need to figure out your own by looking at my code, OR, you can research about it

Hint: it's really simple!!! Well, I thought it was difficult too at the beginning, but try it out and you will find it easy... hopefully...

P.S I'm using port 90 in this Server application because I use port 80 for the IIS server. so to test my exact code, you need to specify the localhost:90 with the colon sign ":" and followed by the port number.

P.S II: I haven't turned in this HW, so if you're my friend from my university, lucky you!!!

Good Luck!

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 07-05-2007 at 08:31 PM.
Reply With Quote
The Following 2 Users Say Thank You to HelloWorld For This Useful Post:
ccoonen (07-05-2007), Leech (07-24-2007)