Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 3

Code for Socket program of HTTP Server in Java

import
import
import
import

java.net.*;
java.io.*;
java.util.*;
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
String
String
String
String

ServerLine="Simple HTTP Server";


StatusLine=null;
ContentTypeLine=null;
ContentLengthLine=null;
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();

}
}
}

You might also like