Java Blog

 
 
 
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;

    }
    public void 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 = new byte[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
{
    public static void main(String args[]) throws Exception
    {
        ServerSocket soc=new ServerSocket(5217);
        while(true)
        {
            Socket inSoc=soc.accept();
            HttpRequest request=new HttpRequest(inSoc);
            request.process();
        }


    }
}

    Related Post:
  1. Write a class whose objects holds a current value and have a method to add that value, printing the new value

  2. Corba program to develop a simple calculator with addition, subtraction, multiplication and division capabilities, data collection from client side

  3. Program that will read real number from the keyboard and print the following output in one line: small Integer not less The Given Number Largest Int

  4. Program to show an example of extracting subStrings from a String

  5. Program to declare, initialize and print an array of integers

  6. CORBA PROGRAM TO GET THE HTML CODE FROM ANY URL

  7. An applet program to find maximum of three numbers using textField

  8. Program to extract a portion of a character string and print the extracted string

  9. Program to Concat Strings

  10. RMI servlet program to display message

  11. Socket program of HTTP Server

  12. Corba program of UDP client server application which sends the news to the client. Server takes the news from the NewsDataFile located at the server

  13. An applet program to display barchart of given data

  14. An applet program to draw Polygon Graph with given data

  15. Application which takes your marks of six subjects from the client side and displays the percentage that you have obtained

  16. Program to show the use of Conditional OR Operator (||) in If Statements

  17. Program to show an example of Copying i.e. Clonning Objects

  18. Game application in CORBA for which player will guess a number between 1 to 100, which will be compared to the random number generated by the class

  19. Program to declare and initialize Boolean variables and print them on the Standard Output Device

  20. Program to show an example of Overriding a Base Class Method

 
 
Didn't find what you were looking for? Find more on Socket program of HTTP Server