Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Socket ProgrammingRSS Feeds

Socket program of HTTP Server

Posted By: Easy Tutor     Category: Java     Views: 10855

Write a Socket program of HTTP Server.

Code for Socket program of HTTP Server in Java


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

class HttpRequest
{
    private Socket ClientConn;
    public HttpRequest(Socket ClientConn) throws Exception
    {
        this.ClientConn=ClientConn;
        
    }
    publicvoid process() throws Exception
    {
        DataInputStream din=new DataInputStream(ClientConn.getInputStream());
        
        OutputStream ot=ClientConn.getOutputStream();
        BufferedOutputStream out=new BufferedOutputStream(ot);

        String request=din.readLine().trim();
        System.out.println(request);
        StringTokenizer st=new StringTokenizer(request);
        
        String header=st.nextToken();
        
        if(header.equals("GET"))
        {
            String fileName=st.nextToken();
            FileInputStream fin=null;
            boolean fileExist=true;
            try
            {
                fin=new FileInputStream(fileName);
            }
            catch(Exception ex)
            {
                fileExist=false;
            }
            
            String ServerLine="Simple HTTP Server";
            String StatusLine=null;
            String ContentTypeLine=null;
            String ContentLengthLine=null;
            String ContentBody=null;
            
            if(fileExist)
            {
                StatusLine="HTTP/1.0 200 OK";
                ContentTypeLine="Content-type: text/html";
                ContentLengthLine="Content-Length: "+ (new Integer(fin.available()).toString());                
            }
            else
            {
                StatusLine = "HTTP/1.0 200 OK";
                ContentTypeLine="Content-type: text/html";
                ContentBody = "<HTML>" + 
                                "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
                            "<BODY>404 Not Found" +                                               
                                "</BODY></HTML>" ;
                ContentLengthLine=(new Integer(ContentBody.length()).toString());
            }
            
            out.write(StatusLine.getBytes());
            out.write( ServerLine.getBytes());
            out.write(ContentTypeLine.getBytes());
            out.write( ContentLengthLine.getBytes());

            if(fileExist)    
            {

                byte[] buffer = newbyte[1024] ;
                int bytes = 0 ;
                while ((bytes = fin.read(buffer)) != -1 ) 
                    {
                    out.write(buffer, 0, bytes);
                    for(int iCount=0;iCount<bytes;iCount++)
                    {
                        int temp=buffer[iCount];
                        System.out.print((char)temp);
                    }
                }    
                
                fin.close();
            }
            else
            {
                out.write(ContentBody.getBytes());
            }

                            out.close();
                                ClientConn.close();

            
        }
        
    }
}
class HttpServer
{
    publicstaticvoid main(String args[]) throws Exception
    {
        ServerSocket soc=new ServerSocket(5217);
        while(true)
        {
            Socket inSoc=soc.accept();
            HttpRequest request=new HttpRequest(inSoc);
            request.process();
        }
        
        
    }
}
  
Share: 


Didn't find what you were looking for? Find more on Socket program of HTTP Server Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Socket program of HTTP Server is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
BILAL SARWAR from United States Comment on: Dec 13
What should be entered in browser to get access to server

View All Comments