public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket client = null;
try{
serverSocket = new ServerSocket(9999);
System.out.println("服务器初始化完毕,端口号"+9999);
while(true){
client = serverSocket.accept();
InputStream in = client.getInputStream();
byte[] buff = new byte[1024];
int len = in.read(buff);
if(len>0){
String msg = new String(buff,0,len);
System.out.println("客户端请求信息======="+msg+"========");
OutputStream out = client.getOutputStream();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
StringBuffer sb = new StringBuffer();
sb.append("HTTP/1.1 200 OK\n");
sb.append("Content-Type:text/html;charset=UTF-8\n");
sb.append("\r\n");
String html = "<html><head><title>welcome></title></head><body>当前时间:"
+"<font size='14' color='blue'>"
+format.format(new Date())
+"</font>"
+"<br/>服务器回复:<font size='14' color='blue'>hello world</font></body></html>";
sb.append(html);
out.write(sb.toString().getBytes());
out.flush();
out.close();
client.close();
}
}
}catch(Exception e){
e.printStackTrace();
}