http协议是使用非常广泛的网络协议,作为网站开发人员,了解http协议是十分重要的。
但是,我不想读官方的http文档,我想直接自己去一点点了解http协议,通过编写socket与浏览器通信。代码如下
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String args[]) throws IOException{
ServerSocket server = new ServerSocket(2222);
while(true){
try{
Socket client = server.accept();
OutputStream out = client.getOutputStream();
DataOutputStream outStream = new DataOutputStream(client.getOutputStream());
outStream.write("Hello".getBytes());
InputStream in = client.getInputStream();
byte[] buf = new byte[1024];
in.read(buf);
in.close();
System.out.println("request from client " + client.getInetAddress().getHostAddress());
System.out.prin