端口(Port):通信线路中的一条专车道。
TCP/IP协议端口号范围为0到65535。
cmd:netstat -anto
Web Container
Tomcat
JBoss
Apache
Nginx
HTTP协议:
请求-响应、一次访问一次连接。不是长连接。
HTTP协议通讯报文
请求报文:
GET /garysu/index.html?name=100 HTTP/1.1
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:zh-CN,zh;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:localhost:8080
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/55.0.2883.87 Safari/537.36
响应报文:
•响应行、响应头、响应数据构成!
HTTP/1.1 200 OK
Accept-Ranges:bytes
Content-Length:396
Content-Type:text/html
Date:Sun, 02 Apr 2017 06:48:41 GMT
ETag:W/"396-1491115714597"
Last-Modified:Sun, 02 Apr 2017 06:48:34 GMT
Server:Apache-Coyote/1.1
<!DOCTYPE html>
<html>
...
</html>
·
web服务器:
public class HTTPTest {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket =new ServerSocket(18887);
Socket socket=serverSocket.accept();
InputStream inputStream=socket.getInputStream();
byte[] b=new byte[1024];
int p=inputStream.read(b);
String data=new String(b,0,p);
System.out.println(data);
System.out.println("分析数据报===================");
String FileName=data.split("\n")[0].split(" ")[1];//"/"后的要从服务器拿的文件信息
System.out.println(data.split("\n")[0].split(" ")[1]);
String str = System.getProperty("user.dir");
String path="src/main/resources"+FileName;
System.out.println(path);
String responseBody= IOUtils.readText(path);
//文件读取代码
System.out.println("拿去的文本内容:"+responseBody);
//将内容输出,给客户端(浏览器)http协议
String httpResponse="HTTP/1.1 200 OK\n" +
"Accept-Ranges:bytes\n" +
"Content-Length:396\n" +
"Content-Type:text/html\n" +
"Date:Sun, 02 Apr 2017 06:48:41 GMT\n" +
"ETag:W/\"396-1491115714597\"\n" +
"Last-Modified:Sun, 02 Apr 2017 06:48:34 GMT\n" +
"Server:Apache-Coyote/1.1\n"+"\n"+responseBody;//响应标头+体
System.out.println(httpResponse);
OutputStream outputStream=socket.getOutputStream();
outputStream.write(httpResponse.getBytes());
socket.close();
}
}
浏览器传输过来的请求报文,服务器返回给浏览器响应报文