import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class HTTPServer {
public static void main(String[] args) throws Exception {
ServerSocket sSocket = new ServerSocket(1777);
while (true) {
System.out.println("Waiting for a client...");
Socket newSocket = sSocket.accept();
System.out.println("accepted the socket");
OutputStream os = newSocket.getOutputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(newSocket.getInputStream()));
String inLine = null;
while (((inLine = br.readLine()) != null) && (!(inLine.equals("")))) {
System.out.println(inLine);
}
System.out.println("");
StringBuffer sb = new StringBuffer();
sb.append("<html>\n");
sb.append("<head>\n");
sb.append("<title>Java \n");
sb.append("</title>\n");
sb.append("</head>\n");
sb.append("<body>\n");
sb.append("<H1>HTTPServer Works!</H1>\n");
sb.append("</body>\n");
sb.append("</html>\n");
String string = sb.toString();
byte[] byteArray = string.getBytes();
os.write("HTTP/1.0 200 OK\n".getBytes());
os.write(new String("Content-Length: " + byteArray.length + "\n").getBytes());
os.write("Content-Type: text/html\n\n".getBytes());
os.write(byteArray);
os.flush();
os.close();
br.close();
newSocket.close();
}
}
}
|