HTTP Webserver Using Java

import java.net.*;
import java.io.*;

class myserver
{
public static void main(String str[])
{

String myIp=null;
int port=786;
try{

//Get Systems current ip address
myIp= Inet4Address.getLocalHost().getHostAddress();
System.out.println("Server is Running with "+ myIp +":"+ port);

//Create Server Socket
ServerSocket welcome = new ServerSocket(port);

//Accept incomming connection Socket
Socket sskt = welcome.accept();

PrintWriter out = new PrintWriter(sskt.getOutputStream());

//Create MIME type to describe that, This is HTML Code
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println("rn");
//Write your HTML Code Here
out.println("Hello worldn");
out.flush();

out.close();
}
catch(Exception e)
{
System.out.println(e.toString());
}

}
}

Share Post