Make your own Java webServer

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(“\r\n”);
//Write your HTML Code Here
out.println(“Hello world “);
out.flush();

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

}
}

What  is MIME type?

MIME stands for Multi-purpose Internet Mail Extensions. MIME types form a standard way of classifying file types on the Internet. Internet programs such as Web servers and browsers all have a list of MIME types, so that they can transfer files of the same type in the same way, no matter what operating system they are working in.[2]

A MIME type has two parts: a type and a subtype. They are separated by a slash (/). For example, the MIME type for Microsoft Word files is application and the subtype is msword. Together, the complete MIME type is application/msword.[2]

 

 

ref

[1] http://www.prasannatech.net/2008/10/simple-http-server-java.html

[2] http://www.aibn.com/help/Learn/mimetypes.html

Share Post