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 (1) Thread Tools    Display Modes   
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 07-14-2007, 10:44 PM
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,118
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 read and write image on web server?

In this tutorial, I will just add one more implementation on the ServerListener class. This is the continuation from the previous thread, where I was able to read and write ASCII file from the Web Server to the Web Browser.

Introduction to Web Server Application

PHP Code:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
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;
    }
 
    
private static void sendBytes(FileInputStream fisOutputStream osthrows Exception {
        
// Construct a 1K buffer to hold bytes on their way to the socket.
        
byte[] buffer = new byte[1024];
        
int bytes 0;
 
        
// Copy requested file into the socket's output stream.
        
while((bytes fis.read(buffer)) != -)
            
os.write(buffer0bytes);
    }
 
    
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("/""");
 
            
// DETERMINE THE TYPE
            
StringTokenizer st2 = new StringTokenizer(file".");
            
String fType st2.nextToken();
            
fType st2.nextToken();
 
            if (
fType.equals("html") || fType.equals("txt")) {
                
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();
            } else {
                
FileInputStream fis = new FileInputStream(file);
                
sendBytes(fissock.getOutputStream());
            }
        } 
catch (IOException ioe) {
            
System.out.println("IOException: " ioe.getMessage());
        } 
catch (Exception e) {
            
System.out.println("Exception: " e.getMessage());
        }
    }

Hopefully some comment on the code is self explanatory. Feel free to ask questions and add some more implementation which you think is more effective than what I have, but the most important one in this new implementation is the sendByte() method implementation:

PHP Code:
    private static void sendBytes(FileInputStream fisOutputStream osthrows Exception {
        
// Construct a 1K buffer to hold bytes on their way to the socket.
        
byte[] buffer = new byte[1024];
        
int bytes 0;
 
        
// Copy requested file into the socket's output stream.
        
while((bytes fis.read(buffer)) != -)
            
os.write(buffer0bytes);
    } 
This is the method which enables our Web Server to be able to send back response by bytes back to the client's requests.

STILL FOR MY CLASSMATES, DON'T JUST COPY AND PASTE

__________________
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!

Last edited by HelloWorld : 07-20-2007 at 01:53 PM.
Reply With Quote
The Following User Says Thank You to HelloWorld For This Useful Post:
Leech (07-24-2007)
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

LinkBacks (?)
LinkBack to this Thread: http://www.programmerstalk.net/thread910.html
Posted By For Type Date
java: Blogs, Photos, Videos and more on Technorati This thread Refback 07-14-2007 11:00 PM


All times are GMT -7. The time now is 06:25 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