The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > Java


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Tags: , , , , ,

Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-03-2007, 11:56 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
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 07: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)
  #2 (permalink)  
Old 07-24-2007, 10:29 PM
Leech
Posts: n/a
What about subdirectories? Doesn't the line

file = file.replaceAll("/", "");

cram the link we type in the browser into a long string.

For example, if I were to have a file.fil under folder. Wouldn't localhost:80/Folder/file.fil make the file name Folderfile.lif?

I tested it out and it throws IOExceptions and of course, the file Folderfile.fil doesn't exist.

But overall, this is a helpful tutorial.

__________________

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!
Reply With Quote
The Following User Says Thank You to For This Useful Post:
HelloWorld (07-24-2007)
  #3 (permalink)  
Old 07-24-2007, 10:37 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,111
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Thank you for your comment, I didn't think about subdirectories at all... XD
However, if you want sub directories, you may want to add additional IF... ELSE... statement on that line ...
and probably additional StringTokenizer also at "\" and see how many "\" are there, if it's more than one, then there's sub directories and will go to the different part of the program, something like that lol...

I haven't tried it out, but will surely will have the update version of this code in the future

Thanx for popping up one important logic that I shouldn't forget in the real life application

__________________
PHP Code:
System.out.println("Hello World!"); 

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!
Reply With Quote
Reply


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

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



All times are GMT -7. The time now is 12:28 AM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50