📄 httpserver.java
字号:
/*
* HTTPServer.java
*
* Created on June 18, 2002, 3:07 PM
*/
import java.io.*;
import java.net.*;
/**
*
* @author Stephen Potts
* @version
*/
public class HTTPServer
{
/** Creates new HTTPServer */
public HTTPServer()
{
}
public static void main(String[] args)
{
try
{
ServerSocket sSocket = new ServerSocket(1777);
System.out.println("Created the socket");
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 Primer Plus\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();
//Put the output into a byte array
byte[] byteArray = string.getBytes();
//add some header information
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());
//add the output
os.write(byteArray);
os.flush();
//close it up
os.close();
br.close();
newSocket.close();
}//while
}catch(Exception e)
{
e.printStackTrace();
}
}//main
}//class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -