{"id":59907,"date":"2026-01-14T17:41:39","date_gmt":"2026-01-14T09:41:39","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/59907.html"},"modified":"2026-01-14T17:41:39","modified_gmt":"2026-01-14T09:41:39","slug":"http%e5%ae%9e%e7%8e%b0%e6%9c%8d%e5%8a%a1%e5%99%a8%e4%b8%8e%e6%b5%8f%e8%a7%88%e5%99%a8%e9%80%9a%e4%bf%a1","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/59907.html","title":{"rendered":"http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1"},"content":{"rendered":"<h3>\u4e00\u3001\u6d4f\u89c8\u5668\u8bbf\u95ee Web \u670d\u52a1\u5668\u7684 HTTP \u901a\u4fe1\u5168\u6d41\u7a0b<\/h3>\n<p>\u5305\u542b DNS \u89e3\u6790\u3001TCP \u8fde\u63a5\u3001HTTP \u6536\u53d1\u3001\u8fde\u63a5\u7ba1\u7406 4 \u4e2a\u6838\u5fc3\u73af\u8282&#xff0c;\u6211\u5206\u6b65\u9aa4\u62c6\u89e3&#xff08;\u6e05\u6670\u5230\u6bcf\u4e00\u6b65&#xff09;&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"389\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114094137-696764d1b35a0.png\" width=\"711\" \/><\/p>\n<h4>\u6b65\u9aa4 1&#xff1a;DNS \u89e3\u6790&#xff08;\u6d4f\u89c8\u5668\u5148\u627e\u5230\u670d\u52a1\u5668\u7684 IP&#xff09;<\/h4>\n<ul>\n<li>\u6d4f\u89c8\u5668\u8f93\u5165www.baidu.com&#xff08;\u57df\u540d&#xff09;&#xff0c;\u4f46\u7f51\u7edc\u901a\u4fe1\u9700\u8981IP \u5730\u5740&#xff1b;<\/li>\n<li>\u6d4f\u89c8\u5668\u5411DNS \u670d\u52a1\u5668\u53d1\u8d77\u8bf7\u6c42&#xff0c;\u67e5\u8be2www.baidu.com\u5bf9\u5e94\u7684 IP&#xff08;\u6bd4\u5982180.101.50.242&#xff09;&#xff1b;<\/li>\n<li>DNS \u8fd4\u56de\u8be5\u57df\u540d\u7684 IP&#xff0c;\u6d4f\u89c8\u5668\u62ff\u5230\u76ee\u6807\u670d\u52a1\u5668\u7684 IP \u540e&#xff0c;\u51c6\u5907\u5efa\u7acb\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<h4>\u6b65\u9aa4 2&#xff1a;\u5efa\u7acb TCP \u8fde\u63a5&#xff08;\u4e09\u6b21\u63e1\u624b&#xff09;<\/h4>\n<ul>\n<li>\u6d4f\u89c8\u5668\u8c03\u7528connect()&#xff0c;\u5411\u670d\u52a1\u5668\u768480 \u7aef\u53e3&#xff08;HTTP \u9ed8\u8ba4\u7aef\u53e3&#xff09;\u53d1\u8d77 TCP \u4e09\u6b21\u63e1\u624b&#xff1a;\n<li>\u6d4f\u89c8\u5668\u53d1SYN \u2192 2. \u670d\u52a1\u5668\u56deSYN&#043;ACK \u2192 3. \u6d4f\u89c8\u5668\u56deACK&#xff1b;<\/li>\n<\/li>\n<li>\u63e1\u624b\u5b8c\u6210\u540e&#xff0c;\u6d4f\u89c8\u5668\u4e0e\u670d\u52a1\u5668\u5efa\u7acb\u53ef\u9760\u7684 TCP \u8fde\u63a5&#xff08;\u56fe\u4e2d \u201ctcp \u8fde\u63a5\u201d&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u6b65\u9aa4 3&#xff1a;\u53d1\u9001 HTTP \u8bf7\u6c42\u62a5\u6587<\/h4>\n<ul>\n<li>\u6d4f\u89c8\u5668\u901a\u8fc7\u5df2\u5efa\u7acb\u7684 TCP \u8fde\u63a5&#xff0c;\u5411\u670d\u52a1\u5668\u53d1\u9001HTTP \u8bf7\u6c42\u62a5\u6587&#xff08;\u6bd4\u5982GET \/index.html HTTP\/1.1&#xff09;&#xff0c;\u5305\u542b\u8bf7\u6c42\u65b9\u6cd5\u3001\u8d44\u6e90\u8def\u5f84\u3001\u534f\u8bae\u7248\u672c\u7b49\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"774\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114094137-696764d1d3b29.png\" width=\"1019\" \/><\/p>\n<p>GET http:\/\/www.baidu.com\/index.html HTTP\/1.0  \u2192 \u8bf7\u6c42\u884c<br \/>\nUser-Agent: Wget\/1.12 (linux-gnu)             \u2192 \u8bf7\u6c42\u59341<br \/>\nHost: www.baidu.com                          \u2192 \u8bf7\u6c42\u59342<br \/>\nConnection: close                            \u2192 \u8bf7\u6c42\u59343 <\/p>\n<p>\u2460\u8bf7\u6c42\u884c&#xff08;\u7b2c 1 \u884c&#xff0c;HTTP \u8bf7\u6c42\u7684\u6838\u5fc3\u6307\u4ee4&#xff09;<\/p>\n<p>\u8bf7\u6c42\u884c\u7531 \u300c\u8bf7\u6c42\u65b9\u6cd5\u300d&#043;\u300c\u8bf7\u6c42 URL\u300d&#043;\u300c\u534f\u8bae\u7248\u672c\u300d \u4e09\u90e8\u5206\u7ec4\u6210&#xff0c;\u6bcf\u90e8\u5206\u90fd\u6709\u660e\u786e\u4f5c\u7528&#xff1a;<\/p>\n<li>\u8bf7\u6c42\u65b9\u6cd5&#xff08;GET&#xff09;&#xff1a;\n<ul>\n<li>\u662f\u5ba2\u6237\u7aef\u5bf9\u670d\u52a1\u5668\u7684 \u201c\u64cd\u4f5c\u6307\u4ee4\u201d&#xff0c;\u56fe\u4e2dGET\u8868\u793a \u201c\u8bfb\u53d6\u670d\u52a1\u5668\u8d44\u6e90\u201d&#xff08;\u53ea\u8bfb\u3001\u4e0d\u4fee\u6539\u670d\u52a1\u5668\u6570\u636e&#xff09;&#xff1b;<\/li>\n<li>\u4e0d\u540c\u65b9\u6cd5\u5bf9\u5e94\u4e0d\u540c\u64cd\u4f5c&#xff08;\u6bd4\u5982 POST \u662f \u201c\u63d0\u4ea4\u6570\u636e\u201d&#xff0c;PUT \u662f \u201c\u4e0a\u4f20\u8d44\u6e90\u201d&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u8bf7\u6c42 URL&#xff08;http:\/\/www.baidu.com\/index.html&#xff09;&#xff1a;\n<ul>\n<li>\u660e\u786e\u8981\u8bbf\u95ee\u7684\u670d\u52a1\u5668\u8d44\u6e90\u8def\u5f84&#xff0c;\u8fd9\u91cc\u662f\u767e\u5ea6\u9996\u9875\u7684index.html\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u534f\u8bae\u7248\u672c&#xff08;HTTP\/1.0&#xff09;&#xff1a;\n<ul>\n<li>\u8868\u793a\u4f7f\u7528\u7684 HTTP \u534f\u8bae\u7248\u672c&#xff0c;\u4e0d\u540c\u7248\u672c\u652f\u6301\u7684\u7279\u6027\u4e0d\u540c&#xff08;\u6bd4\u5982 HTTP\/1.0 \u9ed8\u8ba4\u77ed\u8fde\u63a5&#xff0c;HTTP\/1.1 \u9ed8\u8ba4\u957f\u8fde\u63a5&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u2461\u8bf7\u6c42\u5934&#xff08;\u7b2c 2-4 \u884c&#xff0c;\u4f20\u9012\u9644\u52a0\u4fe1\u606f&#xff09;<\/p>\n<p>\u8bf7\u6c42\u5934\u662f \u201c\u952e\u503c\u5bf9\u201d \u683c\u5f0f&#xff08;\u952e: \u503c&#xff09;&#xff0c;\u7528\u6765\u544a\u8bc9\u670d\u52a1\u5668\u8bf7\u6c42\u7684\u9644\u52a0\u4fe1\u606f&#xff0c;\u56fe\u4e2d\u8fd9\u51e0\u4e2a\u5934\u662f\u9ad8\u9891\u5fc5\u77e5\u7684&#xff1a;<\/p>\n<li>User-Agent: Wget\/1.12 (linux-gnu)&#xff1a;\n<ul>\n<li>\u544a\u8bc9\u670d\u52a1\u5668 \u201c\u5ba2\u6237\u7aef\u7684\u8eab\u4efd\u201d&#xff1a;\u8fd9\u91cc\u662f Linux \u7cfb\u7edf\u4e0b\u7684Wget\u5de5\u5177&#xff08;\u4e00\u79cd\u547d\u4ee4\u884c\u4e0b\u8f7d\u5de5\u5177&#xff09;&#xff1b;<\/li>\n<li>\u670d\u52a1\u5668\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u5934\u8bc6\u522b\u5ba2\u6237\u7aef\u7c7b\u578b&#xff08;\u6bd4\u5982\u662f\u6d4f\u89c8\u5668\u3001\u624b\u673a APP \u8fd8\u662f\u722c\u866b&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Host: www.baidu.com&#xff1a;\n<ul>\n<li>\u544a\u8bc9\u670d\u52a1\u5668 \u201c\u76ee\u6807\u57df\u540d\u201d&#xff1a;\u56e0\u4e3a\u4e00\u53f0\u670d\u52a1\u5668\u53ef\u80fd\u7ed1\u5b9a\u591a\u4e2a\u57df\u540d&#xff0c;\u8fd9\u4e2a\u5934\u7528\u6765\u6307\u5b9a\u8981\u8bbf\u95ee\u7684\u5177\u4f53\u57df\u540d&#xff1b;<\/li>\n<li>\u662f HTTP\/1.1 \u7684\u5fc5\u9009\u5934&#xff08;HTTP\/1.0 \u53ef\u9009&#xff0c;\u4f46\u5b9e\u9645\u4e5f\u4f1a\u4f20&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Connection: close&#xff1a;\n<ul>\n<li>\u544a\u8bc9\u670d\u52a1\u5668 \u201c\u672c\u6b21\u8bf7\u6c42\u7528\u77ed\u8fde\u63a5\u201d&#xff1a;HTTP\/1.0 \u9ed8\u8ba4\u662f\u77ed\u8fde\u63a5&#xff0c;\u8bf7\u6c42\u5b8c\u6210\u540e\u670d\u52a1\u5668\u4f1a\u5173\u95ed TCP \u8fde\u63a5&#xff1b;<\/li>\n<li>\u5982\u679c\u662fConnection: keep-alive&#xff0c;\u5c31\u662f\u957f\u8fde\u63a5&#xff08;HTTP\/1.1 \u9ed8\u8ba4&#xff09;&#xff0c;\u8fde\u63a5\u4f1a\u4fdd\u7559\u4f9b\u540e\u7eed\u8bf7\u6c42\u590d\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u2462\u8865\u5145&#xff1a;GET \u8bf7\u6c42\u7684\u7279\u70b9&#xff08;\u7ed3\u5408\u793a\u4f8b&#xff09;<\/p>\n<p>\u56fe\u4e2d\u662fGET\u8bf7\u6c42&#xff0c;\u5b83\u7684\u6838\u5fc3\u7279\u70b9\u662f&#xff1a;<\/p>\n<ul>\n<li>\u65e0\u8bf7\u6c42\u4f53&#xff1a;\u56e0\u4e3a\u662f \u201c\u8bfb\u53d6\u8d44\u6e90\u201d&#xff0c;\u4e0d\u9700\u8981\u5411\u670d\u52a1\u5668\u63d0\u4ea4\u6570\u636e&#xff0c;\u6240\u4ee5\u8bf7\u6c42\u53ea\u6709 \u201c\u8bf7\u6c42\u884c &#043; \u8bf7\u6c42\u5934\u201d&#xff1b;<\/li>\n<li>\u6570\u636e\u5728 URL \u4e2d&#xff1a;\u5982\u679c\u8981\u4f20\u53c2\u6570&#xff0c;\u4f1a\u62fc\u63a5\u5728 URL \u540e\u9762&#xff08;\u6bd4\u5982index.html?name&#061;test&#xff09;&#xff1b;<\/li>\n<li>\u5e42\u7b49\u6027&#xff1a;\u591a\u6b21\u8c03\u7528GET\u8bf7\u6c42&#xff0c;\u4e0d\u4f1a\u4fee\u6539\u670d\u52a1\u5668\u7684\u72b6\u6001&#xff08;\u6bd4\u5982\u591a\u6b21\u8bf7\u6c42index.html&#xff0c;\u670d\u52a1\u5668\u6570\u636e\u4e0d\u4f1a\u53d8&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u6b65\u9aa4 4&#xff1a;\u670d\u52a1\u5668\u8fd4\u56de HTTP \u5e94\u7b54\u62a5\u6587<\/h4>\n<ul>\n<li>\u670d\u52a1\u5668\u63a5\u6536\u8bf7\u6c42\u540e&#xff0c;\u5904\u7406\u8bf7\u6c42&#xff08;\u6bd4\u5982\u8bfb\u53d6index.html\u6587\u4ef6&#xff09;&#xff0c;\u901a\u8fc7 TCP \u8fde\u63a5\u5411\u6d4f\u89c8\u5668\u8fd4\u56deHTTP \u5e94\u7b54\u62a5\u6587&#xff08;\u5305\u542b\u72b6\u6001\u7801\u3001\u54cd\u5e94\u5934\u3001\u54cd\u5e94\u4f53&#xff09;&#xff0c;\u62a5\u6587\u5b9e\u529b\u5982\u4e0b\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"137\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114094138-696764d227979.png\" width=\"684\" \/><\/p>\n<p>\u4e00\u3001\u72b6\u6001\u884c&#xff08;\u7b2c 1 \u884c&#xff09;<\/p>\n<p>HTTP\/1.0 200 OK<\/p>\n<ul>\n<li>HTTP\/1.0&#xff1a;\u4f7f\u7528\u7684 HTTP \u534f\u8bae\u7248\u672c&#xff1b;<\/li>\n<li>200&#xff1a;\u72b6\u6001\u7801&#xff0c;\u8868\u793a \u201c\u8bf7\u6c42\u6210\u529f\u201d&#xff08;\u662f\u6700\u5e38\u89c1\u7684\u6210\u529f\u72b6\u6001\u7801&#xff09;&#xff1b;<\/li>\n<li>OK&#xff1a;\u72b6\u6001\u7801\u7684\u63cf\u8ff0\u6587\u672c&#xff0c;\u4e0e200\u5bf9\u5e94\u3002<\/li>\n<\/ul>\n<p>\u4e8c\u3001\u54cd\u5e94\u5934&#xff08;\u7b2c 2-6 \u884c&#xff0c;\u670d\u52a1\u5668\u8fd4\u56de\u7684\u9644\u52a0\u4fe1\u606f&#xff09;<\/p>\n<li>Server: BWS\/1.0\n<ul>\n<li>\u670d\u52a1\u5668\u6807\u8bc6&#xff1a;\u8868\u793a\u8fd9\u662f\u767e\u5ea6\u7684 BWS \u670d\u52a1\u5668&#xff08;Baidu Web Server&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Content-Length: 8024\n<ul>\n<li>\u54cd\u5e94\u4f53\u7684\u5b57\u8282\u6570&#xff1a;\u544a\u8bc9\u5ba2\u6237\u7aef&#xff0c;\u540e\u7eed\u8fd4\u56de\u7684\u8d44\u6e90&#xff08;\u6bd4\u5982\u7f51\u9875&#xff09;\u5927\u5c0f\u662f 8024 \u5b57\u8282\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Content-Type: text\/html;charset&#061;gbk\n<ul>\n<li>\u54cd\u5e94\u4f53\u7684\u7c7b\u578b\u548c\u7f16\u7801&#xff1a;\n<ul>\n<li>text\/html&#xff1a;\u8d44\u6e90\u662f HTML \u7f51\u9875&#xff1b;<\/li>\n<li>charset&#061;gbk&#xff1a;\u7f51\u9875\u5185\u5bb9\u7684\u7f16\u7801\u683c\u5f0f\u662f GBK\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<li>Set-Cookie: BAIDUID&#061;&#8230;\n<ul>\n<li>\u670d\u52a1\u5668\u5411\u5ba2\u6237\u7aef\u8bbe\u7f6e Cookie&#xff1a;\u7528\u4e8e\u8eab\u4efd\u8bc6\u522b\u3001\u4f1a\u8bdd\u4fdd\u6301&#xff08;\u6bd4\u5982\u767b\u5f55\u72b6\u6001&#xff09;&#xff0c;\u540e\u7eed\u5ba2\u6237\u7aef\u8bf7\u6c42\u4f1a\u81ea\u52a8\u643a\u5e26\u8fd9\u4e2a Cookie\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Via: 1.0 localhost (squid\/3.0.STABLE18)\n<ul>\n<li>\u4ee3\u7406\u4fe1\u606f&#xff1a;\u8868\u793a\u8bf7\u6c42\u7ecf\u8fc7\u4e86\u672c\u5730\u7684 Squid \u4ee3\u7406\u670d\u52a1\u5668&#xff08;\u7248\u672c 3.0&#xff09;\u8f6c\u53d1\u3002<\/li>\n<\/ul>\n<\/li>\n<p>HTTP \u5e94\u7b54\u62a5\u6587\u4e2d\u7684\u300c\u72b6\u6001\u7801\u5206\u7c7b\u4e0e\u542b\u4e49\u300d<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"519\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114094138-696764d23326b.png\" width=\"784\" \/><\/p>\n<h4>\u6b65\u9aa4 5&#xff1a;\u8fde\u63a5\u7ba1\u7406&#xff08;\u77ed\u8fde\u63a5 \/ \u957f\u8fde\u63a5&#xff09;<\/h4>\n<ul>\n<li>\u77ed\u8fde\u63a5&#xff08;\u56fe\u4e2d \u201cclose \u77ed\u8fde\u63a5\u201d&#xff09;&#xff1a;HTTP 1.0 \u9ed8\u8ba4\u662f\u77ed\u8fde\u63a5&#xff0c;\u670d\u52a1\u5668\u8fd4\u56de\u5e94\u7b54\u540e&#xff0c;\u4e3b\u52a8\u5173\u95ed TCP \u8fde\u63a5&#xff1b;<\/li>\n<li>\u957f\u8fde\u63a5&#xff08;\u56fe\u4e2d \u201c\u957f\u8fde\u63a5\u201d&#xff09;&#xff1a;HTTP 1.1 \u9ed8\u8ba4\u662f\u957f\u8fde\u63a5&#xff0c;\u670d\u52a1\u5668\u8fd4\u56de\u5e94\u7b54\u540e&#xff0c;TCP \u8fde\u63a5\u4e0d\u5173\u95ed&#xff0c;\u540e\u7eed\u6d4f\u89c8\u5668\u53ef\u590d\u7528\u8be5\u8fde\u63a5\u53d1\u9001\u65b0\u7684 HTTP \u8bf7\u6c42&#xff08;\u51cf\u5c11\u91cd\u590d\u63e1\u624b\u7684\u5f00\u9500&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u603b\u7ed3\u6574\u4e2a\u6d41\u7a0b<\/h4>\n<p>\u6d4f\u89c8\u5668\u901a\u8fc7DNS \u89e3\u6790\u57df\u540d\u2192TCP \u4e09\u6b21\u63e1\u624b\u5efa\u8fde\u2192\u53d1 HTTP \u8bf7\u6c42\u2192\u6536 HTTP \u54cd\u5e94\u2192\u6839\u636e\u77ed \/ \u957f\u8fde\u63a5\u7b56\u7565\u7ba1\u7406 TCP \u8fde\u63a5&#xff0c;\u5b8c\u6210\u4e00\u6b21 Web \u8d44\u6e90\u7684\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<h3>\u4e8c\u3001\u5199myhttp.c http\u670d\u52a1\u5668\u8ba9\u5176\u548c\u6d4f\u89c8\u5668\u901a\u4fe1\u00a0<\/h3>\n<p>\u6211\u4eec\u73b0\u5728\u5199&#xff1a;\u4e00\u4e2atcp\u7684\u670d\u52a1\u5668\u7aef&#xff0c;\u4ed6\u7684\u4e1a\u52a1\u65f6\u548c\u6d4f\u89c8\u5668&#xff08;\u5ba2\u6237\u7aef&#xff09;\u8fdb\u884c\u901a\u4fe1&#xff0c;\u6240\u4ee5\u4e0d\u7528\u5199\u5ba2\u6237\u7aef \u53ea\u7528\u5427\u670d\u52a1\u5668\u7aef\u5199\u597d\u3002<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;pthread.h&gt;<\/p>\n<p>\/\/ 1. \u670d\u52a1\u5668\u521d\u59cb\u5316\u51fd\u6570&#xff1a;\u521b\u5efa\u5957\u63a5\u5b57&#043;\u7ed1\u5b9a&#043;\u76d1\u542c<br \/>\nint socket_init()<br \/>\n{<br \/>\n    \/\/ \u521b\u5efaTCP\u5957\u63a5\u5b57<br \/>\n    int sockfd &#061; socket(AF_INET, SOCK_STREAM, 0);<br \/>\n    if (sockfd &#061;&#061; -1)<br \/>\n    {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ \u914d\u7f6e\u670d\u52a1\u5668\u5730\u5740&#xff08;IP:127.0.0.1&#xff0c;\u7aef\u53e3:80&#xff09;<br \/>\n    struct sockaddr_in saddr;<br \/>\n    memset(&amp;saddr, 0, sizeof(saddr));<br \/>\n    saddr.sin_family &#061; AF_INET;<br \/>\n    saddr.sin_port &#061; htons(80);<br \/>\n    saddr.sin_addr.s_addr &#061; inet_addr(&#034;127.0.0.1&#034;);<\/p>\n<p>    \/\/ \u7ed1\u5b9a\u5730\u5740<br \/>\n    int res &#061; bind(sockfd, (struct sockaddr*)&amp;saddr, sizeof(saddr));<br \/>\n    if (res &#061;&#061; -1)<br \/>\n    {<br \/>\n        printf(&#034;bind err\\\\n&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ \u76d1\u542c&#xff08;\u961f\u5217\u957f\u5ea65&#xff09;<br \/>\n    if(listen(sockfd,5) &#061;&#061; -1 )<br \/>\n    {<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    return sockfd;<br \/>\n}<\/p>\n<p>\/\/ 2. \u7ebf\u7a0b\u5904\u7406\u51fd\u6570&#xff1a;\u4e0e\u5ba2\u6237\u7aef\u901a\u4fe1<br \/>\nvoid* thread_fun(void* arg)<br \/>\n{<br \/>\n    \/\/ \u63a5\u6536\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<br \/>\n    int *p &#061; (int*)arg;<br \/>\n    int c &#061; *p;<br \/>\n    free(p); \/\/ \u91ca\u653e\u52a8\u6001\u5206\u914d\u7684\u5185\u5b58<\/p>\n<p>    char buff[1024] &#061; {0};<br \/>\n    \/\/ \u63a5\u6536\u5ba2\u6237\u7aef\u6570\u636e<br \/>\n    int n &#061; recv(c, buff, 1023, 0);<br \/>\n    if (n &lt;&#061; 0)<br \/>\n    {<br \/>\n        close(c);<br \/>\n        pthread_exit(NULL); \/\/ \u7ebf\u7a0b\u9000\u51fa<br \/>\n    }<\/p>\n<p>    printf(&#034;buff&#061;%s\\\\n&#034;, buff);<br \/>\n    send(c, &#034;OK&#034;, 2, 0); \/\/ \u56de\u590d\u5ba2\u6237\u7aef<br \/>\n    close(c);<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>\/\/ 3. \u4e3b\u51fd\u6570&#xff1a;\u63a5\u6536\u8fde\u63a5&#043;\u521b\u5efa\u7ebf\u7a0b<br \/>\nint main()<br \/>\n{<br \/>\n    int sockfd &#061; socket_init();<br \/>\n    if (sockfd &#061;&#061; -1)<br \/>\n    {<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    while(1)<br \/>\n    {<br \/>\n        \/\/ \u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u5f97\u5230\u5ba2\u6237\u7aef\u5957\u63a5\u5b57c<br \/>\n        int c &#061; accept(sockfd, NULL, NULL);<br \/>\n        if (c &lt; 0)<br \/>\n        {<br \/>\n            continue;<br \/>\n        }<\/p>\n<p>        \/\/ \u52a8\u6001\u5206\u914d\u5185\u5b58\u5b58c&#xff08;\u907f\u514d\u7ebf\u7a0b\u95f4\u5171\u4eab\u6808\u53d8\u91cf&#xff09;<br \/>\n        int *p &#061; (int*)malloc(sizeof(int));<br \/>\n        *p &#061; c;<br \/>\n        \/\/ \u521b\u5efa\u7ebf\u7a0b&#xff0c;\u6267\u884cthread_fun\u5904\u7406\u8be5\u5ba2\u6237\u7aef<br \/>\n        pthread_t tid;<br \/>\n        pthread_create(&amp;tid, NULL, thread_fun, (void*)p);<br \/>\n    }<\/p>\n<p>    exit(1);<br \/>\n} <\/p>\n<p>\u6838\u5fc3\u903b\u8f91\u8bb2\u89e3<\/p>\n<li>\n<p>socket_init()&#xff1a;\u670d\u52a1\u5668\u521d\u59cb\u5316<\/p>\n<ul>\n<li>\u5b8c\u6210socket&#xff08;\u521b\u5efa TCP \u5957\u63a5\u5b57&#xff09;\u2192 bind&#xff08;\u7ed1\u5b9a 127.0.0.1:80&#xff09;\u2192 listen&#xff08;\u5f00\u542f\u76d1\u542c&#xff09;&#xff0c;\u8fd4\u56de\u76d1\u542c\u5957\u63a5\u5b57 sockfd\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>main()&#xff1a;\u4e3b\u7ebf\u7a0b\u63a5\u6536\u8fde\u63a5 &#043; \u521b\u5efa\u5de5\u4f5c\u7ebf\u7a0b<\/p>\n<ul>\n<li>\u5faa\u73af\u8c03\u7528accept\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u5f97\u5230\u5ba2\u6237\u7aef\u5957\u63a5\u5b57c&#xff1b;<\/li>\n<li>\u52a8\u6001\u5206\u914d\u5185\u5b58\u5b58\u50a8c&#xff08;\u907f\u514d\u7ebf\u7a0b\u5171\u4eab\u6808\u53d8\u91cf&#xff09;&#xff0c;\u901a\u8fc7pthread_create\u521b\u5efa\u65b0\u7ebf\u7a0b&#xff0c;\u5c06c\u4f20\u7ed9thread_fun\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>thread_fun()&#xff1a;\u5de5\u4f5c\u7ebf\u7a0b\u5904\u7406\u5ba2\u6237\u7aef\u901a\u4fe1<\/p>\n<ul>\n<li>\u63a5\u6536c&#xff0c;\u91ca\u653e\u5185\u5b58&#xff1b;<\/li>\n<li>\u901a\u8fc7recv\u63a5\u6536\u5ba2\u6237\u7aef\u6570\u636e&#xff0c;send\u56de\u590d \u201cOK\u201d&#xff1b;<\/li>\n<li>\u901a\u4fe1\u5b8c\u6210\u540e\u5173\u95edc&#xff0c;\u7ebf\u7a0b\u9000\u51fa\u3002<\/li>\n<\/ul>\n<\/li>\n<p>\u591a\u7ebf\u7a0b\u5e76\u53d1\u7684\u7279\u70b9<\/p>\n<ul>\n<li>\u76f8\u6bd4\u591a\u8fdb\u7a0b&#xff0c;\u7ebf\u7a0b\u66f4\u8f7b\u91cf&#xff08;\u8d44\u6e90\u5f00\u9500\u5c0f&#xff09;&#xff1b;<\/li>\n<li>\u9700\u6ce8\u610f\u5185\u5b58\u7ba1\u7406&#xff08;\u7528malloc\u4f20\u9012c&#xff0c;\u907f\u514d\u6808\u53d8\u91cf\u88ab\u591a\u7ebf\u7a0b\u5171\u4eab&#xff09;&#xff1b;<\/li>\n<li>\u5b9e\u73b0 \u201c\u4e00\u4e2a\u5ba2\u6237\u7aef\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b\u201d \u7684\u5e76\u53d1\u670d\u52a1&#xff0c;\u9002\u5408\u9ad8\u5e76\u53d1\u573a\u666f\u3002<\/li>\n<\/ul>\n<p>\u6d4b\u8bd5<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"426\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114094138-696764d26c59c.png\" width=\"859\" \/><\/p>\n<p>\u2460\u6d4b\u8bd5\u51c6\u5907&#xff08;\u7ec8\u7aef\u64cd\u4f5c&#xff09;<\/p>\n<li>\u7f16\u8bd1\u4ee3\u7801&#xff1a;\u7528gcc -o myhttp myhttp.c -lpthread\u7f16\u8bd1\u670d\u52a1\u5668\u4ee3\u7801&#xff08;-lpthread\u662f\u94fe\u63a5\u7ebf\u7a0b\u5e93&#xff09;\u3002<\/li>\n<li>\u9996\u6b21\u542f\u52a8\u5931\u8d25&#xff1a;\u76f4\u63a5\u8fd0\u884c.\/myhttp\u63d0\u793abind err\u2014\u2014 \u56e0\u4e3a\u670d\u52a1\u5668\u7ed1\u5b9a\u7684\u662f80 \u7aef\u53e3&#xff08;HTTP \u9ed8\u8ba4\u7aef\u53e3&#xff09;&#xff0c;\u666e\u901a\u7528\u6237\u65e0\u6743\u9650\u4f7f\u7528 1024 \u4ee5\u4e0b\u7aef\u53e3\u3002<\/li>\n<li>\u63d0\u6743\u542f\u52a8&#xff1a;\u7528sudo su\u5207\u6362\u5230 root \u7528\u6237&#xff0c;\u518d\u8fd0\u884c.\/myhttp&#xff0c;\u670d\u52a1\u5668\u6210\u529f\u542f\u52a8&#xff08;\u83b7\u5f97 80 \u7aef\u53e3\u7684\u4f7f\u7528\u6743\u9650&#xff09;\u3002<\/li>\n<p>\u2461\u901a\u4fe1\u6d4b\u8bd5&#xff08;\u6d4f\u89c8\u5668 &#043; \u670d\u52a1\u5668\u4ea4\u4e92&#xff09;<\/p>\n<li>\u6d4f\u89c8\u5668\u53d1\u8d77\u8bf7\u6c42&#xff1a;\u5728\u6d4f\u89c8\u5668\u4e2d\u8bbf\u95ee127.0.0.1\/index.html&#xff08;\u672c\u5730\u670d\u52a1\u5668&#xff09;&#xff0c;\u6d4f\u89c8\u5668\u81ea\u52a8\u53d1\u9001HTTP GET \u8bf7\u6c42&#xff08;\u5305\u542b\u8bf7\u6c42\u884c\u3001\u8bf7\u6c42\u5934&#xff0c;\u5982GET \/index.html HTTP\/1.1\u3001User-Agent\u7b49&#xff09;\u3002<\/li>\n<li>\u670d\u52a1\u5668\u63a5\u6536\u5e76\u5904\u7406&#xff1a;\u670d\u52a1\u5668\u7684accept\u63a5\u6536\u5230\u6d4f\u89c8\u5668\u8fde\u63a5&#xff0c;\u521b\u5efa\u7ebf\u7a0b\u6267\u884cthread_fun&#xff1a;\n<ul>\n<li>\u901a\u8fc7recv\u63a5\u6536\u6d4f\u89c8\u5668\u7684 HTTP \u8bf7\u6c42&#xff0c;\u7ec8\u7aef\u6253\u5370\u8bf7\u6c42\u5185\u5bb9&#xff08;buff&#061;GET \/index.html&#8230;&#xff09;&#xff1b;<\/li>\n<li>\u8c03\u7528send\u56de\u590d \u201cOK\u201d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u6d4f\u89c8\u5668\u63a5\u6536\u56de\u590d&#xff1a;\u6d4f\u89c8\u5668\u663e\u793a\u670d\u52a1\u5668\u8fd4\u56de\u7684 \u201cOK\u201d&#xff0c;\u5b8c\u6210\u4e00\u6b21 HTTP \u901a\u4fe1\u3002<\/li>\n<p>\u2462\u6838\u5fc3\u9a8c\u8bc1\u70b9<\/p>\n<ul>\n<li>\u9a8c\u8bc1\u4e86 \u201c\u591a\u7ebf\u7a0b\u670d\u52a1\u5668 &#043; 80 \u7aef\u53e3\u6743\u9650 &#043; HTTP \u8bf7\u6c42\u5904\u7406\u201d \u7684\u6d41\u7a0b&#xff1b;<\/li>\n<li>\u89e3\u51b3\u4e86 \u201c\u666e\u901a\u7528\u6237\u65e0\u6cd5\u7ed1\u5b9a\u4f4e\u7aef\u53e3\u201d \u7684\u95ee\u9898&#xff08;\u901a\u8fc7 root \u63d0\u6743&#xff09;&#xff1b;<\/li>\n<li>\u5b9e\u73b0\u4e86\u6d4f\u89c8\u5668\u4e0e\u670d\u52a1\u5668\u7684 HTTP \u8bf7\u6c42 &#8211; \u54cd\u5e94\u4ea4\u4e92\u3002<\/li>\n<\/ul>\n<p>\u89e3\u6790\u6a21\u5757&#xff1a;<\/p>\n<p>\u89e3\u6790\u62ff\u51fa\u8bf7\u6c42\u65b9\u6cd5<\/p>\n<h3>\u4e09\u3001\u5728\u539f\u591a\u7ebf\u7a0b TCP \u670d\u52a1\u5668\u57fa\u7840\u4e0a&#xff0c;\u6269\u5c55\u4e3a\u7b80\u6613 HTTP \u6587\u4ef6\u670d\u52a1\u5668<\/h3>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;pthread.h&gt;<br \/>\n#include &lt;fcntl.h&gt;<br \/>\n#include &lt;sys\/stat.h&gt;<\/p>\n<p>\/\/ \u4f60\u7684\u4ee3\u7801\u91cc\u5b9a\u4e49\u7684\u6587\u4ef6\u6839\u76ee\u5f55&#xff0c;\u548c\u622a\u56fe\u4e00\u81f4<br \/>\n#define PATH &#034;\/home\/stu\/c2305\/day22&#034;<\/p>\n<p>\/\/ \u89e3\u6790HTTP\u8bf7\u6c42\u884c&#xff0c;\u63d0\u53d6\u8bf7\u6c42\u7684\u6587\u4ef6\u540d \u5982 GET \/index.html HTTP\/1.1 -&gt; \/index.html<br \/>\nchar* get_filename(char buff[])<br \/>\n{<br \/>\n    char* ptr &#061; NULL;<br \/>\n    char* s &#061; strtok_r(buff, &#034; &#034;, &amp;ptr); \/\/ \u5206\u5272\u51faGET\/POST<br \/>\n    if(s &#061;&#061; NULL)<br \/>\n    {<br \/>\n        return NULL;<br \/>\n    }<br \/>\n    s &#061; strtok_r(NULL, &#034; &#034;, &amp;ptr); \/\/ \u5206\u5272\u51fa\u8bf7\u6c42\u7684\u6587\u4ef6\u8def\u5f84<br \/>\n    return s;<br \/>\n}<\/p>\n<p>\/\/ \u670d\u52a1\u5668\u521d\u59cb\u5316&#xff1a;\u521b\u5efasocket\u3001\u7ed1\u5b9a\u3001\u76d1\u542c<br \/>\nint socket_init()<br \/>\n{<br \/>\n    int sockfd &#061; socket(AF_INET,SOCK_STREAM,0);<br \/>\n    if(sockfd &#061;&#061; -1)<br \/>\n    {<br \/>\n        perror(&#034;socket err&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    struct sockaddr_in saddr;<br \/>\n    memset(&amp;saddr,0,sizeof(saddr));<br \/>\n    saddr.sin_family &#061; AF_INET;<br \/>\n    saddr.sin_port &#061; htons(80);  \/\/ HTTP\u9ed8\u8ba480\u7aef\u53e3<br \/>\n    saddr.sin_addr.s_addr &#061; inet_addr(&#034;127.0.0.1&#034;);<\/p>\n<p>    int res &#061; bind(sockfd,(struct sockaddr*)&amp;saddr,sizeof(saddr));<br \/>\n    if(res &#061;&#061; -1)<br \/>\n    {<br \/>\n        perror(&#034;bind err&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    if(listen(sockfd,5) &#061;&#061; -1)<br \/>\n    {<br \/>\n        perror(&#034;listen err&#034;);<br \/>\n        return -1;<br \/>\n    }<br \/>\n    return sockfd;<br \/>\n}<\/p>\n<p>\/\/ \u7ebf\u7a0b\u5904\u7406\u51fd\u6570&#xff1a;\u5904\u7406\u5ba2\u6237\u7aefHTTP\u8bf7\u6c42&#043;\u8fd4\u56de\u6587\u4ef6<br \/>\nvoid* thread_fun(void* arg)<br \/>\n{<br \/>\n    int c &#061; *(int*)arg;<br \/>\n    free(arg); \/\/ \u91ca\u653emalloc\u7684\u5806\u5185\u5b58<\/p>\n<p>    char buff[1024] &#061; {0};<br \/>\n    int n &#061; recv(c,buff,1023,0);<br \/>\n    if(n &lt;&#061; 0)<br \/>\n    {<br \/>\n        close(c);<br \/>\n        pthread_exit(NULL);<br \/>\n    }<br \/>\n    printf(&#034;\u6536\u5230\u5ba2\u6237\u7aef\u8bf7\u6c42:\\\\n%s\\\\n&#034;,buff);<\/p>\n<p>    \/\/ 1. \u89e3\u6790\u8bf7\u6c42\u7684\u6587\u4ef6\u540d<br \/>\n    char* filename &#061; get_filename(buff);<br \/>\n    if(filename &#061;&#061; NULL)<br \/>\n    {<br \/>\n        close(c);<br \/>\n        pthread_exit(NULL);<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u62fc\u63a5\u5b8c\u6574\u6587\u4ef6\u8def\u5f84<br \/>\n    char file_path[256] &#061; {0};<br \/>\n    strcpy(file_path,PATH);<br \/>\n    \/\/ \u5982\u679c\u8bbf\u95ee\u6839\u76ee\u5f55 \/ &#xff0c;\u9ed8\u8ba4\u6253\u5f00index.html<br \/>\n    if(strcmp(filename,&#034;\/&#034;) &#061;&#061; 0)<br \/>\n    {<br \/>\n        strcat(file_path,&#034;\/index.html&#034;);<br \/>\n    }<br \/>\n    else<br \/>\n    {<br \/>\n        strcat(file_path,filename);<br \/>\n    }<br \/>\n    printf(&#034;\u8981\u6253\u5f00\u7684\u6587\u4ef6\u8def\u5f84: %s\\\\n&#034;,file_path);<\/p>\n<p>    \/\/ 3. \u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u6587\u4ef6<br \/>\n    int fd &#061; open(file_path,O_RDONLY);<br \/>\n    if(fd &#061;&#061; -1)<br \/>\n    {<br \/>\n        \/\/ \u6587\u4ef6\u6253\u5f00\u5931\u8d25&#xff0c;\u8fd4\u56deHTTP 404\u54cd\u5e94<br \/>\n        char err_msg[512] &#061; &#034;HTTP\/1.1 404 Not Found\\\\r\\\\nContent-Type:text\/html;charset&#061;utf-8\\\\r\\\\n\\\\r\\\\n&lt;html&gt;&lt;head&gt;&lt;meta charset&#061;&#039;utf-8&#039;&gt;&lt;\/head&gt;&lt;body&gt;&lt;h1&gt;404 \u9875\u9762\u4e0d\u5b58\u5728&lt;\/h1&gt;&lt;\/body&gt;&lt;\/html&gt;&#034;;<br \/>\n        send(c,err_msg,strlen(err_msg),0);<br \/>\n        close(c);<br \/>\n        pthread_exit(NULL);<br \/>\n    }<\/p>\n<p>    \/\/ 4. \u83b7\u53d6\u6587\u4ef6\u5927\u5c0f<br \/>\n    struct stat st;<br \/>\n    fstat(fd,&amp;st);<br \/>\n    int file_size &#061; st.st_size;<\/p>\n<p>    \/\/ 5. \u62fc\u63a5HTTP 200\u6210\u529f\u54cd\u5e94\u5934<br \/>\n    char head_msg[512] &#061; {0};<br \/>\n    sprintf(head_msg,&#034;HTTP\/1.1 200 OK\\\\r\\\\nContent-Length:%d\\\\r\\\\nContent-Type:text\/html;charset&#061;utf-8\\\\r\\\\n\\\\r\\\\n&#034;,file_size);<br \/>\n    send(c,head_msg,strlen(head_msg),0);<\/p>\n<p>    \/\/ 6. \u5faa\u73af\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668<br \/>\n    char file_buff[1024] &#061; {0};<br \/>\n    int read_len &#061; 0;<br \/>\n    while((read_len &#061; read(fd,file_buff,1024)) &gt; 0)<br \/>\n    {<br \/>\n        send(c,file_buff,read_len,0);<br \/>\n        memset(file_buff,0,sizeof(file_buff));<br \/>\n    }<\/p>\n<p>    \/\/ \u6536\u5c3e&#xff1a;\u5173\u95ed\u6587\u4ef6\u3001\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n    close(fd);<br \/>\n    close(c);<br \/>\n    pthread_exit(NULL);<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n    int sockfd &#061; socket_init();<br \/>\n    if(sockfd &#061;&#061; -1)<br \/>\n    {<br \/>\n        exit(1);<br \/>\n    }<br \/>\n    printf(&#034;HTTP\u670d\u52a1\u5668\u542f\u52a8\u6210\u529f&#xff0c;\u76d1\u542c 127.0.0.1:80 &#8230;\\\\n&#034;);<\/p>\n<p>    pthread_attr_t attr;<br \/>\n    pthread_attr_init(&amp;attr);<br \/>\n    \/\/ \u8bbe\u7f6e\u7ebf\u7a0b\u5206\u79bb\u5c5e\u6027&#xff1a;\u7ebf\u7a0b\u9000\u51fa\u540e\u81ea\u52a8\u91ca\u653e\u8d44\u6e90&#xff0c;\u89e3\u51b3\u5185\u5b58\u6cc4\u6f0f&#xff01;<br \/>\n    pthread_attr_setdetachstate(&amp;attr,PTHREAD_CREATE_DETACHED);<\/p>\n<p>    while(1)<br \/>\n    {<br \/>\n        int c &#061; accept(sockfd,NULL,NULL);<br \/>\n        if(c &lt; 0)<br \/>\n        {<br \/>\n            continue;<br \/>\n        }<br \/>\n        printf(&#034;\u6709\u5ba2\u6237\u7aef\u8fde\u63a5\u6210\u529f&#xff01;\\\\n&#034;);<\/p>\n<p>        int* p &#061; (int*)malloc(sizeof(int));<br \/>\n        *p &#061; c;<br \/>\n        pthread_t tid;<br \/>\n        \/\/ \u521b\u5efa\u5206\u79bb\u5c5e\u6027\u7684\u7ebf\u7a0b<br \/>\n        pthread_create(&amp;tid,&amp;attr,thread_fun,(void*)p);<br \/>\n    }<br \/>\n    pthread_attr_destroy(&amp;attr);<br \/>\n    close(sockfd);<br \/>\n    exit(0);<br \/>\n} <\/p>\n<h4>\u4e00\u3001\u6838\u5fc3\u529f\u80fd\u5347\u7ea7&#xff1a;\u4ece \u201c\u56de\u590d\u56fa\u5b9a OK\u201d \u5230 \u201c\u8fd4\u56de\u6587\u4ef6\u201d<\/h4>\n<p>\u539f\u4ee3\u7801\u53ea\u662f\u63a5\u6536\u8bf7\u6c42\u540e\u56de\u590d \u201cOK\u201d&#xff0c;\u8fd9\u6bb5\u4ee3\u7801\u65b0\u589e\u4e86HTTP \u8bf7\u6c42\u89e3\u6790 &#043; \u6587\u4ef6\u8def\u5f84\u62fc\u63a5\u7684\u903b\u8f91&#xff0c;\u76ee\u6807\u662f&#xff1a;\u6d4f\u89c8\u5668\u8bbf\u95ee127.0.0.1\/index.html \u2192 \u670d\u52a1\u5668\u89e3\u6790\u51fa\u8bf7\u6c42\u7684\u6587\u4ef6\u662f\/index.html \u2192 \u62fc\u63a5\u672c\u5730\u8def\u5f84\/home\/stu\/c2305\/day22\/index.html \u2192 \u8bfb\u53d6\u8be5\u6587\u4ef6\u5e76\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u3002<\/p>\n<h4>\u4e8c\u3001\u9010\u6bb5\u89e3\u6790\u65b0\u589e\u903b\u8f91<\/h4>\n<h5>1. get_filename(char buff[])&#xff1a;\u89e3\u6790 HTTP \u8bf7\u6c42\u4e2d\u7684\u6587\u4ef6\u8def\u5f84<\/h5>\n<p>\u4f5c\u7528&#xff1a;\u4ece\u6d4f\u89c8\u5668\u53d1\u9001\u7684 HTTP \u8bf7\u6c42&#xff08;\u5b58\u5728buff\u4e2d&#xff09;\u91cc&#xff0c;\u63d0\u53d6\u8981\u8bbf\u95ee\u7684\u6587\u4ef6\u8def\u5f84&#xff08;\u6bd4\u5982\u4eceGET \/index.html HTTP\/1.1\u4e2d\u63d0\u53d6\/index.html&#xff09;<\/p>\n<p>char* get_filename(char buff[])<br \/>\n{<br \/>\n    char* ptr &#061; NULL;<br \/>\n    \/\/ \u7b2c\u4e00\u6b65&#xff1a;\u7528\u7a7a\u683c\u5206\u5272\u8bf7\u6c42\u884c&#xff0c;\u62ff\u5230\u7b2c\u4e00\u4e2a\u5b57\u6bb5&#xff08;\u8bf7\u6c42\u65b9\u6cd5&#xff0c;\u6bd4\u5982GET&#xff09;<br \/>\n    char* s &#061; strtok_r(buff, &#034; &#034;, &amp;ptr);<br \/>\n    if (s &#061;&#061; NULL) { return NULL; }<br \/>\n    printf(&#034;\u8bf7\u6c42\u65b9\u6cd5: %s\\\\n&#034;, s);<\/p>\n<p>    \/\/ \u7b2c\u4e8c\u6b65&#xff1a;\u5206\u5272\u5f97\u5230\u7b2c\u4e8c\u4e2a\u5b57\u6bb5&#xff08;\u6587\u4ef6\u8def\u5f84&#xff0c;\u6bd4\u5982\/index.html&#xff09;<br \/>\n    s &#061; strtok_r(NULL, &#034; &#034;, &amp;ptr);<br \/>\n    return s; \/\/ \u8fd4\u56de\u89e3\u6790\u51fa\u7684\u6587\u4ef6\u8def\u5f84<br \/>\n}<\/p>\n<ul>\n<li>\u793a\u4f8b&#xff1a;\u82e5buff\u662fGET \/index.html HTTP\/1.1&#xff0c;strtok_r\u7b2c\u4e00\u6b21\u5206\u5272\u51faGET&#xff0c;\u7b2c\u4e8c\u6b21\u5206\u5272\u51fa\/index.html&#xff0c;\u6700\u7ec8\u8fd4\u56de\/index.html\u3002<\/li>\n<\/ul>\n<h5>2. \u7ebf\u7a0b\u51fd\u6570thread_fun&#xff1a;\u65b0\u589e\u6587\u4ef6\u8def\u5f84\u62fc\u63a5\u903b\u8f91<\/h5>\n<p>\u5728\u539f \u201c\u63a5\u6536\u8bf7\u6c42\u201d \u540e&#xff0c;\u65b0\u589e\u4e86\u89e3\u6790\u8def\u5f84\u3001\u62fc\u63a5\u672c\u5730\u76ee\u5f55\u7684\u6b65\u9aa4&#xff1a;<\/p>\n<p>\/\/ 1. \u89e3\u6790HTTP\u8bf7\u6c42\u4e2d\u7684\u6587\u4ef6\u8def\u5f84<br \/>\nchar* filename &#061; get_filename(buff);<br \/>\nif (filename &#061;&#061; NULL ) { break; }<\/p>\n<p>\/\/ 2. \u62fc\u63a5\u672c\u5730\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84<br \/>\nchar path[256] &#061; {PATH}; \/\/ PATH\u662f\u5b8f\u5b9a\u4e49\u7684\u672c\u5730\u76ee\u5f55&#xff1a;\/home\/stu\/c2305\/day22<br \/>\n\/\/ \u5904\u7406\u6839\u8def\u5f84\u8bf7\u6c42&#xff08;\u6bd4\u5982\u6d4f\u89c8\u5668\u8bbf\u95ee127.0.0.1&#xff0c;filename\u662f&#034;\/&#034;&#xff09;<br \/>\nif ( strcmp(filename,&#034;\/&#034;) &#061;&#061; 0 )<br \/>\n{<br \/>\n    filename &#061; &#034;\/index.html&#034;; \/\/ \u9ed8\u8ba4\u8fd4\u56deindex.html<br \/>\n}<br \/>\nstrcat(path,filename); \/\/ \u62fc\u63a5\u6210\u5b8c\u6574\u8def\u5f84&#xff1a;\u6bd4\u5982\/home\/stu\/c2305\/day22\/index.html<br \/>\nprintf(&#034;path:%s\\\\n&#034;,path);<\/p>\n<ul>\n<li>\u793a\u4f8b&#xff1a;\u82e5\u89e3\u6790\u51fafilename&#061;\/index.html&#xff0c;\u62fc\u63a5\u540epath\u4e3a\/home\/stu\/c2305\/day22\/index.html&#xff0c;\u540e\u7eed\u4f1a\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6\u5e76\u8fd4\u56de\u7ed9\u6d4f\u89c8\u5668\u3002<\/li>\n<\/ul>\n<h3>\u56db\u3001\u6211\u4eec\u73b0\u5728\u8bf7\u6c42\u670d\u52a1\u5668\u67e5\u770b\u4e00\u5f20\u56fe\u7247<\/h3>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;   \/\/ \u7f51\u7edc\u5957\u63a5\u5b57\u6838\u5fc3\u5934\u6587\u4ef6 socket\/bind\/listen\/accept\/recv\/send<br \/>\n#include &lt;netinet\/in.h&gt;   \/\/ \u7f51\u7edc\u5730\u5740\u7ed3\u6784\u4f53 sockaddr_in \u7f51\u7edc\u5b57\u8282\u5e8f\u8f6c\u6362\u51fd\u6570<br \/>\n#include &lt;arpa\/inet.h&gt;    \/\/ \u5730\u5740\u8f6c\u6362\u76f8\u5173\u51fd\u6570<br \/>\n#include &lt;pthread.h&gt;      \/\/ \u7ebf\u7a0b\u5e93\u5934\u6587\u4ef6 pthread_create\/pthread_exit<br \/>\n#include &lt;fcntl.h&gt;        \/\/ \u6587\u4ef6\u6253\u5f00 open \u51fd\u6570\u5934\u6587\u4ef6<br \/>\n#include &lt;sys\/types.h&gt;    \/\/ \u7cfb\u7edf\u7c7b\u578b\u5b9a\u4e49<\/p>\n<p>\/\/ \u670d\u52a1\u5668\u5b58\u653e\u7f51\u9875\u6587\u4ef6\u7684\u6839\u76ee\u5f55&#xff0c;\u548c\u4f60\u7684\u622a\u56fe\u4e00\u81f4&#xff0c;\u4e0d\u8981\u4fee\u6539<br \/>\n#define PATH &#034;\/home\/stu\/c2305\/day22&#034;<\/p>\n<p>\/**************************************************************************<br \/>\n\u51fd\u6570\u540d&#xff1a;get_filename<br \/>\n\u51fd\u6570\u529f\u80fd&#xff1a;\u89e3\u6790\u6d4f\u89c8\u5668\u53d1\u9001\u7684HTTP\u8bf7\u6c42\u62a5\u6587&#xff0c;\u63d0\u53d6\u8bf7\u6c42\u7684\u6587\u4ef6\u8def\u5f84<br \/>\n\u53c2\u6570&#xff1a;buff \u63a5\u6536\u5ba2\u6237\u7aef\u8bf7\u6c42\u6570\u636e\u7684\u7f13\u51b2\u533a<br \/>\n\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u8bf7\u6c42\u7684\u6587\u4ef6\u8def\u5f84 \u5982:\/index.html  \u5931\u8d25\u8fd4\u56deNULL<br \/>\n**************************************************************************\/<br \/>\nchar* get_filename(char buff[])<br \/>\n{<br \/>\n    char* ptr &#061; NULL;<br \/>\n    \/\/ \u7b2c\u4e00\u6b21\u5206\u5272&#xff0c;\u83b7\u53d6\u8bf7\u6c42\u65b9\u6cd5 GET\/POST<br \/>\n    char* s &#061; strtok_r(buff, &#034; &#034;, &amp;ptr);<br \/>\n    if(s &#061;&#061; NULL) \/\/ \u5206\u5272\u5931\u8d25&#xff0c;\u8fd4\u56deNULL<br \/>\n    {<br \/>\n        return NULL;<br \/>\n    }<br \/>\n    \/\/ \u7b2c\u4e8c\u6b21\u5206\u5272&#xff0c;\u83b7\u53d6\u8bf7\u6c42\u7684\u6587\u4ef6\u8def\u5f84 \u6838\u5fc3\u63d0\u53d6\u903b\u8f91<br \/>\n    s &#061; strtok_r(NULL, &#034; &#034;, &amp;ptr);<br \/>\n    return s;<br \/>\n}<\/p>\n<p>\/**************************************************************************<br \/>\n\u51fd\u6570\u540d&#xff1a;socket_init<br \/>\n\u51fd\u6570\u529f\u80fd&#xff1a;TCP\u670d\u52a1\u5668\u521d\u59cb\u5316\u6838\u5fc3\u51fd\u6570&#xff0c;\u5b8c\u6210 \u521b\u5efa\u5957\u63a5\u5b57-\u7ed1\u5b9a\u7aef\u53e3-\u5f00\u542f\u76d1\u542c \u4e09\u6b65\u64cd\u4f5c<br \/>\n\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u76d1\u542c\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26  \u5931\u8d25\u8fd4\u56de-1<br \/>\n**************************************************************************\/<br \/>\nint socket_init()<br \/>\n{<br \/>\n    \/\/ \u521b\u5efaTCP\u6d41\u5f0f\u5957\u63a5\u5b57 AF_INET:IPv4\u534f\u8bae SOCK_STREAM:TCP\u534f\u8bae 0:\u9ed8\u8ba4<br \/>\n    int sockfd &#061; socket(AF_INET,SOCK_STREAM,0);<br \/>\n    if(sockfd &#061;&#061; -1)<br \/>\n    {<br \/>\n        printf(&#034;socket err\\\\n&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ \u521d\u59cb\u5316\u670d\u52a1\u5668\u5730\u5740\u7ed3\u6784\u4f53<br \/>\n    struct sockaddr_in saddr;<br \/>\n    memset(&amp;saddr,0,sizeof(saddr));          \/\/ \u6e05\u7a7a\u7ed3\u6784\u4f53\u5783\u573e\u503c<br \/>\n    saddr.sin_family &#061; AF_INET;              \/\/ \u5730\u5740\u65cf&#xff1a;IPv4<br \/>\n    saddr.sin_port &#061; htons(80);              \/\/ \u7ed1\u5b9aHTTP\u9ed8\u8ba4\u7aef\u53e380&#xff0c;\u4e3b\u673a\u5e8f\u8f6c\u7f51\u7edc\u5e8f<br \/>\n    saddr.sin_addr.s_addr &#061; inet_addr(&#034;127.0.0.1&#034;); \/\/ \u7ed1\u5b9a\u672c\u5730\u56de\u73af\u5730\u5740&#xff0c;\u4ec5\u672c\u673a\u53ef\u8bbf\u95ee<\/p>\n<p>    \/\/ \u7ed1\u5b9a&#xff1a;\u5c06\u5957\u63a5\u5b57\u4e0e\u6307\u5b9a\u7684IP&#043;\u7aef\u53e3\u8fdb\u884c\u7ed1\u5b9a<br \/>\n    int res &#061; bind(sockfd,(struct sockaddr*)&amp;saddr,sizeof(saddr));<br \/>\n    if(res &#061;&#061; -1)<br \/>\n    {<br \/>\n        printf(&#034;bind err\\\\n&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    \/\/ \u76d1\u542c&#xff1a;\u5f00\u542f\u5957\u63a5\u5b57\u76d1\u542c&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;5\u662f\u534a\u8fde\u63a5\u961f\u5217\u957f\u5ea6<br \/>\n    if(listen(sockfd,5) &#061;&#061; -1)<br \/>\n    {<br \/>\n        return -1;<br \/>\n    }<br \/>\n    return sockfd; \/\/ \u521d\u59cb\u5316\u6210\u529f&#xff0c;\u8fd4\u56de\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n}<\/p>\n<p>\/**************************************************************************<br \/>\n\u51fd\u6570\u540d&#xff1a;thread_fun<br \/>\n\u51fd\u6570\u529f\u80fd&#xff1a;\u7ebf\u7a0b\u5904\u7406\u4e1a\u52a1\u51fd\u6570&#xff0c;\u6bcf\u4e2a\u5ba2\u6237\u7aef\u8fde\u63a5\u5bf9\u5e94\u4e00\u4e2a\u72ec\u7acb\u7ebf\u7a0b<br \/>\n          \u5b8c\u6210&#xff1a;\u63a5\u6536HTTP\u8bf7\u6c42 -&gt; \u89e3\u6790\u6587\u4ef6\u8def\u5f84 -&gt; \u6253\u5f00\u6587\u4ef6 -&gt; \u8fd4\u56de\u6587\u4ef6\u7ed9\u6d4f\u89c8\u5668<br \/>\n\u53c2\u6570&#xff1a;arg \u4e3b\u7ebf\u7a0b\u4f20\u9012\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26\u5730\u5740<br \/>\n\u8fd4\u56de\u503c&#xff1a;\u7ebf\u7a0b\u9000\u51fa\u8fd4\u56deNULL<br \/>\n**************************************************************************\/<br \/>\nvoid* thread_fun(void* arg)<br \/>\n{<br \/>\n    \/\/ \u63a5\u6536\u4e3b\u7ebf\u7a0b\u4f20\u9012\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57\u63cf\u8ff0\u7b26<br \/>\n    int c &#061; *(int*)arg;<br \/>\n    free(arg);  \/\/ \u91ca\u653emalloc\u7533\u8bf7\u7684\u5806\u5185\u5b58&#xff0c;\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f<\/p>\n<p>    \/\/ \u5b9a\u4e49\u7f13\u51b2\u533a\u63a5\u6536\u5ba2\u6237\u7aef\u7684HTTP\u8bf7\u6c42\u6570\u636e<br \/>\n    char buff[1024] &#061; {0};<br \/>\n    \/\/ \u963b\u585e\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u8bf7\u6c42\u6570\u636e&#xff0c;\u6210\u529f\u8fd4\u56de\u63a5\u6536\u5b57\u8282\u6570&#xff0c;\u5931\u8d25\/\u65ad\u5f00\u8fd4\u56de&lt;&#061;0<br \/>\n    int n &#061; recv(c,buff,1023,0);<br \/>\n    if(n &lt;&#061; 0)  \/\/ \u63a5\u6536\u5931\u8d25\u6216\u5ba2\u6237\u7aef\u65ad\u5f00\u8fde\u63a5<br \/>\n    {<br \/>\n        close(c);          \/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57<br \/>\n        pthread_exit(NULL); \/\/ \u7ebf\u7a0b\u6b63\u5e38\u9000\u51fa<br \/>\n    }<br \/>\n    printf(&#034;recv buf&#061;%s\\\\n&#034;,buff); \/\/ \u6253\u5370\u6536\u5230\u7684HTTP\u8bf7\u6c42\u62a5\u6587<\/p>\n<p>    \/\/ 1. \u89e3\u6790HTTP\u8bf7\u6c42\u4e2d\u8981\u8bbf\u95ee\u7684\u6587\u4ef6\u8def\u5f84<br \/>\n    char* filename &#061; get_filename(buff);<br \/>\n    if(filename &#061;&#061; NULL) \/\/ \u89e3\u6790\u5931\u8d25&#xff0c;\u76f4\u63a5\u91ca\u653e\u8d44\u6e90\u9000\u51fa<br \/>\n    {<br \/>\n        close(c);<br \/>\n        pthread_exit(NULL);<br \/>\n    }<\/p>\n<p>    \/\/ 2. \u62fc\u63a5\u8981\u6253\u5f00\u7684\u6587\u4ef6\u5b8c\u6574\u8def\u5f84<br \/>\n    char path[256] &#061; {0};<br \/>\n    strcpy(path, PATH);       \/\/ \u62fc\u63a5\u6839\u76ee\u5f55<br \/>\n    if(strcmp(filename, &#034;\/&#034;) &#061;&#061; 0) \/\/ \u5982\u679c\u8bbf\u95ee\u6839\u76ee\u5f55 \/ &#xff0c;\u9ed8\u8ba4\u6253\u5f00\u9996\u9875index.html<br \/>\n    {<br \/>\n        filename &#061; &#034;\/index.html&#034;;<br \/>\n    }<br \/>\n    strcat(path, filename);   \/\/ \u62fc\u63a5\u5b8c\u6574\u6587\u4ef6\u8def\u5f84<br \/>\n    printf(&#034;path&#061;%s\\\\n&#034;,path); \/\/ \u6253\u5370\u62fc\u63a5\u540e\u7684\u6587\u4ef6\u8def\u5f84<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u4ee5\u4e0b\u662f\u4f60\u622a\u56fe\u7684\u6838\u5fc3\u4ee3\u7801 \u5b8c\u6574\u4fdd\u7559 &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    \/\/ \u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u62fc\u63a5\u597d\u7684\u6587\u4ef6<br \/>\n    int fd &#061; open(path,O_RDONLY);<br \/>\n    if(fd &#061;&#061; -1)  \/\/ \u6587\u4ef6\u6253\u5f00\u5931\u8d25&#xff0c;\u76f4\u63a5\u8df3\u51fa\u903b\u8f91<br \/>\n    {<br \/>\n        close(c);<br \/>\n        pthread_exit(NULL);<br \/>\n    }<br \/>\n    \/\/ \u901a\u8fc7\u6587\u4ef6\u504f\u79fb\u91cf\u8ba1\u7b97\u6587\u4ef6\u5927\u5c0f&#xff1a;\u5c06\u5149\u6807\u79fb\u5230\u6587\u4ef6\u672b\u5c3e&#xff0c;\u8fd4\u56de\u7684\u504f\u79fb\u91cf\u5c31\u662f\u6587\u4ef6\u603b\u5b57\u8282\u6570<br \/>\n    int filesize &#061; lseek(fd,0,SEEK_END);<br \/>\n    lseek(fd,0,SEEK_SET);  \/\/ \u5c06\u5149\u6807\u91cd\u65b0\u79fb\u56de\u6587\u4ef6\u5f00\u5934&#xff0c;\u51c6\u5907\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    \/\/ \u62fc\u63a5HTTP\u54cd\u5e94\u5934&#xff0c;\u4e25\u683c\u9075\u5b88HTTP\u534f\u8bae\u683c\u5f0f<br \/>\n    char head[256] &#061; {&#034;HTTP\/1.1 200 OK\\\\r\\\\n&#034;};  \/\/ \u72b6\u6001\u884c&#xff1a;\u8bf7\u6c42\u6210\u529f<br \/>\n    strcat(head,&#034;Server: myhttp\\\\r\\\\n&#034;);         \/\/ \u54cd\u5e94\u5934&#xff1a;\u670d\u52a1\u5668\u6807\u8bc6<br \/>\n    sprintf(head,&#034;%sContent-Length:%d\\\\r\\\\n&#034;,head,filesize); \/\/ \u54cd\u5e94\u5934&#xff1a;\u544a\u8bc9\u6d4f\u89c8\u5668\u6587\u4ef6\u5927\u5c0f<br \/>\n    strcat(head,&#034;\\\\r\\\\n&#034;);                       \/\/ \u54cd\u5e94\u5934\u7ed3\u675f\u6807\u5fd7&#xff1a;\u7a7a\u884c(\\\\r\\\\n)<br \/>\n    send(c,head,strlen(head),0);               \/\/ \u5c06\u54cd\u5e94\u5934\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668<\/p>\n<p>    \/\/ \u5faa\u73af\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9&#xff0c;\u5206\u6279\u6b21\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668<br \/>\n    char data[1024] &#061; {0}; \/\/ \u5b9a\u4e49\u6587\u4ef6\u8bfb\u53d6\u7f13\u51b2\u533a<br \/>\n    int num &#061; 0;<br \/>\n    \/\/ \u6bcf\u6b21\u8bfb\u53d61024\u5b57\u8282&#xff0c;\u8bfb\u5230\u5185\u5bb9&gt;0\u5219\u53d1\u9001&#xff0c;\u8bfb\u5b8c\u8fd4\u56de0\u5219\u7ed3\u675f\u5faa\u73af<br \/>\n    while((num &#061; read(fd,data,1024))&gt;0)<br \/>\n    {<br \/>\n        send(c,data,num,0); \/\/ \u5c06\u8bfb\u53d6\u7684\u6587\u4ef6\u5185\u5bb9\u53d1\u9001\u7ed9\u5ba2\u6237\u7aef<br \/>\n    }<br \/>\n    close(fd);  \/\/ \u6587\u4ef6\u8bfb\u53d6\u5b8c\u6bd5&#xff0c;\u5173\u95ed\u6587\u4ef6\u63cf\u8ff0\u7b26\u91ca\u653e\u8d44\u6e90<br \/>\n    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u4f60\u622a\u56fe\u7684\u6838\u5fc3\u4ee3\u7801 \u7ed3\u675f &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<\/p>\n<p>    close(c);   \/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff0c;\u91ca\u653e\u8fde\u63a5\u8d44\u6e90<br \/>\n    return NULL;<br \/>\n}<\/p>\n<p>\/**************************************************************************<br \/>\n\u4e3b\u51fd\u6570&#xff1a;\u7a0b\u5e8f\u5165\u53e3<br \/>\n\u529f\u80fd&#xff1a;\u521d\u59cb\u5316\u670d\u52a1\u5668 -&gt; \u5faa\u73af\u63a5\u6536\u5ba2\u6237\u7aef\u8fde\u63a5 -&gt; \u4e3a\u6bcf\u4e2a\u5ba2\u6237\u7aef\u521b\u5efa\u72ec\u7acb\u7ebf\u7a0b\u5904\u7406\u4e1a\u52a1<br \/>\n**************************************************************************\/<br \/>\nint main()<br \/>\n{<br \/>\n    \/\/ \u8c03\u7528\u521d\u59cb\u5316\u51fd\u6570&#xff0c;\u83b7\u53d6\u76d1\u542c\u5957\u63a5\u5b57<br \/>\n    int sockfd &#061; socket_init();<br \/>\n    if(sockfd &#061;&#061; -1) \/\/ \u521d\u59cb\u5316\u5931\u8d25&#xff0c;\u76f4\u63a5\u9000\u51fa\u7a0b\u5e8f<br \/>\n    {<br \/>\n        exit(1);<br \/>\n    }<\/p>\n<p>    \/\/ \u8bbe\u7f6e\u7ebf\u7a0b\u5206\u79bb\u5c5e\u6027&#xff0c;\u89e3\u51b3\u7ebf\u7a0b\u9000\u51fa\u540e\u7684\u8d44\u6e90\u6cc4\u6f0f\u95ee\u9898<br \/>\n    pthread_attr_t attr;<br \/>\n    pthread_attr_init(&amp;attr);  \/\/ \u521d\u59cb\u5316\u7ebf\u7a0b\u5c5e\u6027<br \/>\n    \/\/ \u8bbe\u7f6e\u5206\u79bb\u5c5e\u6027&#xff1a;\u7ebf\u7a0b\u8fd0\u884c\u7ed3\u675f\u540e&#xff0c;\u7cfb\u7edf\u81ea\u52a8\u56de\u6536\u7ebf\u7a0b\u8d44\u6e90&#xff0c;\u65e0\u9700\u4e3b\u7ebf\u7a0bjoin<br \/>\n    pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_DETACHED);<\/p>\n<p>    \/\/ \u6b7b\u5faa\u73af&#xff0c;\u6c38\u4e45\u76d1\u542c\u5ba2\u6237\u7aef\u8fde\u63a5\u8bf7\u6c42<br \/>\n    while(1)<br \/>\n    {<br \/>\n        \/\/ \u963b\u585e\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#xff0c;\u6210\u529f\u8fd4\u56de\u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff0c;\u5931\u8d25\u8fd4\u56de-1<br \/>\n        int c &#061; accept(sockfd,NULL,NULL);<br \/>\n        if(c &lt; 0) \/\/ \u8fde\u63a5\u5931\u8d25&#xff0c;\u7ee7\u7eed\u7b49\u5f85\u4e0b\u4e00\u4e2a\u5ba2\u6237\u7aef<br \/>\n        {<br \/>\n            continue;<br \/>\n        }<br \/>\n        \/\/ \u52a8\u6001\u7533\u8bf7\u5806\u5185\u5b58\u5b58\u653e\u5ba2\u6237\u7aef\u5957\u63a5\u5b57&#xff0c;\u9632\u6b62\u7ebf\u7a0b\u5171\u4eab\u6808\u53d8\u91cf\u5bfc\u81f4\u6570\u636e\u9519\u4e71<br \/>\n        int *p &#061; (int*)malloc(sizeof(int));<br \/>\n        *p &#061; c;<br \/>\n        pthread_t tid; \/\/ \u5b9a\u4e49\u7ebf\u7a0bID<br \/>\n        \/\/ \u521b\u5efa\u7ebf\u7a0b\u5904\u7406\u5f53\u524d\u5ba2\u6237\u7aef\u7684\u4e1a\u52a1\u8bf7\u6c42<br \/>\n        pthread_create(&amp;tid,&amp;attr,thread_fun,(void*)p);<br \/>\n    }<br \/>\n    close(sockfd); \/\/ \u5173\u95ed\u76d1\u542c\u5957\u63a5\u5b57(\u5b9e\u9645\u6b7b\u5faa\u73af\u4e0d\u4f1a\u6267\u884c\u5230\u8fd9\u91cc)<br \/>\n    exit(0);<br \/>\n} <\/p>\n<h4>\u4e00\u3001\u5148\u770b\u3010\u4ee3\u7801\u6574\u4f53\u67b6\u6784\u3011&#xff08;3 \u5927\u6838\u5fc3\u90e8\u5206&#xff0c;\u603b\u5206\u603b\u7ed3\u6784&#xff0c;\u7279\u522b\u6e05\u6670&#xff09;<\/h4>\n<p>\u6574\u4e2a\u4ee3\u7801\u4e00\u5171\u5206\u4e3a 3 \u4e2a\u6838\u5fc3\u51fd\u6570 &#043; \u5934\u6587\u4ef6 \/ \u5b8f\u5b9a\u4e49&#xff0c;\u6267\u884c\u903b\u8f91\u662f&#xff1a;\u5b8f\u5b9a\u4e49\/\u5934\u6587\u4ef6 \u2192 socket_init() \u670d\u52a1\u5668\u521d\u59cb\u5316 \u2192 main() \u4e3b\u7ebf\u7a0b&#xff1a;\u63a5\u8fde\u63a5&#043;\u521b\u7ebf\u7a0b \u2192 thread_fun() \u5b50\u7ebf\u7a0b&#xff1a;\u5904\u7406\u4e1a\u52a1&#043;\u8fd4\u56de\u6587\u4ef6 \u2192 get_filename() \u5de5\u5177\u51fd\u6570&#xff1a;\u89e3\u6790\u8bf7\u6c42\u6240\u6709\u529f\u80fd\u5404\u53f8\u5176\u804c&#xff0c;\u4e3b\u7ebf\u7a0b\u53ea\u505a\u300c\u63a5\u5f85\u300d&#xff0c;\u5b50\u7ebf\u7a0b\u53ea\u505a\u300c\u5e72\u6d3b\u300d&#xff0c;\u5206\u5de5\u660e\u786e\u3002<\/p>\n<hr \/>\n<h4>\u4e8c\u3001\u7b2c\u4e00\u90e8\u5206&#xff1a;\u5934\u6587\u4ef6 &#043; \u5b8f\u5b9a\u4e49&#xff08;\u7a0b\u5e8f\u7684\u57fa\u7840\u4f9d\u8d56&#xff09;<\/h4>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;string.h&gt;<br \/>\n#include &lt;unistd.h&gt;<br \/>\n#include &lt;sys\/socket.h&gt;   \/\/ \u7f51\u7edc\u6838\u5fc3\u51fd\u6570&#xff1a;socket\/bind\/listen\/accept\/recv\/send<br \/>\n#include &lt;netinet\/in.h&gt;   \/\/ \u7f51\u7edc\u5730\u5740\u7ed3\u6784\u4f53 sockaddr_in &#043; \u5b57\u8282\u5e8f\u8f6c\u6362 htons<br \/>\n#include &lt;arpa\/inet.h&gt;    \/\/ \u5730\u5740\u8f6c\u6362 inet_addr<br \/>\n#include &lt;pthread.h&gt;      \/\/ \u7ebf\u7a0b\u5e93&#xff1a;pthread_create \u521b\u5efa\u7ebf\u7a0b\u3001pthread_exit \u9000\u51fa\u7ebf\u7a0b<br \/>\n#include &lt;fcntl.h&gt;        \/\/ \u6587\u4ef6\u64cd\u4f5c&#xff1a;open \u6253\u5f00\u6587\u4ef6<br \/>\n#include &lt;sys\/types.h&gt;    \/\/ \u7cfb\u7edf\u57fa\u7840\u7c7b\u578b\u5b9a\u4e49<\/p>\n<p>#define PATH &#034;\/home\/stu\/c2305\/day22&#034;  \/\/ \u56fa\u5b9a\u5b8f&#xff1a;\u4f60\u7684\u670d\u52a1\u5668\u5b58\u653ehtml\u6587\u4ef6\u7684\u6839\u76ee\u5f55<\/p>\n<p>\u2705 \u6838\u5fc3\u8bf4\u660e&#xff1a;<\/p>\n<li>\u8fd9\u4e9b\u5934\u6587\u4ef6\u662f\u5fc5\u52a0\u7684\u4f9d\u8d56&#xff0c;\u5c11\u4e00\u4e2a\u90fd\u4f1a\u7f16\u8bd1\u62a5\u9519&#xff1b;<\/li>\n<li>#define PATH \u662f\u4f60\u672c\u5730\u5b58\u653e\u7f51\u9875\u6587\u4ef6\u7684\u76ee\u5f55&#xff0c;\u6d4f\u89c8\u5668\u8bbf\u95ee\u7684\u6240\u6709 html \u6587\u4ef6&#xff0c;\u90fd\u5fc5\u987b\u653e\u5728\u8fd9\u4e2a\u76ee\u5f55\u4e0b&#xff0c;\u5426\u5219\u670d\u52a1\u5668\u627e\u4e0d\u5230\u3002<\/li>\n<hr \/>\n<h4>\u4e09\u3001\u7b2c\u4e8c\u90e8\u5206&#xff1a;3 \u4e2a\u6838\u5fc3\u51fd\u6570 \u9010\u51fd\u6570\u8bb2\u89e3&#xff08;\u91cd\u4e2d\u4e4b\u91cd&#xff0c;\u5168\u90e8\u5403\u900f&#xff09;<\/h4>\n<h4>\u2705 \u51fd\u6570 1&#xff1a;char* get_filename(char buff[]) \u3010\u5de5\u5177\u51fd\u6570&#xff1a;\u89e3\u6790 HTTP \u8bf7\u6c42\u3011<\/h4>\n<h5>\u4f5c\u7528<\/h5>\n<p>\u4ece\u6d4f\u89c8\u5668\u53d1\u9001\u7684HTTP \u8bf7\u6c42\u62a5\u6587\u91cc&#xff0c;\u63d0\u53d6\u51fa\u4f60\u8981\u8bbf\u95ee\u7684\u6587\u4ef6\u8def\u5f84\u3002<\/p>\n<h5>\u6838\u5fc3\u903b\u8f91<\/h5>\n<p>\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/127.0.0.1\/index.html \u65f6&#xff0c;\u4f1a\u7ed9\u670d\u52a1\u5668\u53d1\u4e00\u6bb5\u8bf7\u6c42\u6570\u636e&#xff0c;\u683c\u5f0f\u662f&#xff1a;GET \/index.html HTTP\/1.1 &#8230;<\/p>\n<ul>\n<li>\u8fd9\u4e2a\u51fd\u6570\u7528 strtok_r \u6309\u7a7a\u683c\u5206\u5272\u8fd9\u6bb5\u5b57\u7b26\u4e32&#xff1b;<\/li>\n<li>\u7b2c\u4e00\u6b21\u5206\u5272&#xff1a;\u62ff\u5230 GET&#xff08;\u8bf7\u6c42\u65b9\u6cd5&#xff0c;\u6211\u4eec\u4e0d\u7528\u7ba1&#xff09;&#xff1b;<\/li>\n<li>\u7b2c\u4e8c\u6b21\u5206\u5272&#xff1a;\u62ff\u5230 \/index.html&#xff08;\u6838\u5fc3&#xff01;\u8fd9\u5c31\u662f\u8981\u6253\u5f00\u7684\u6587\u4ef6\u8def\u5f84&#xff09;&#xff1b;<\/li>\n<\/ul>\n<h5>\u5173\u952e\u70b9<\/h5>\n<ul>\n<li>\u8fd4\u56de\u503c&#xff1a;\u6210\u529f\u8fd4\u56de\u6587\u4ef6\u8def\u5f84&#xff0c;\u5931\u8d25\u8fd4\u56de NULL&#xff1b;<\/li>\n<li>\u7279\u6b8a\u60c5\u51b5&#xff1a;\u5982\u679c\u6d4f\u89c8\u5668\u53ea\u8bbf\u95ee http:\/\/127.0.0.1&#xff0c;\u5206\u5272\u51fa\u6765\u7684\u662f \/&#xff0c;\u540e\u7eed\u4f1a\u81ea\u52a8\u66ff\u6362\u6210 \/index.html&#xff08;\u8bbf\u95ee\u9996\u9875&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u2705 \u51fd\u6570 2&#xff1a;int socket_init() \u3010\u6838\u5fc3\u521d\u59cb\u5316\u51fd\u6570&#xff1a;TCP \u670d\u52a1\u5668\u4e09\u4ef6\u5957\u3011<\/h4>\n<h5>\u4f5c\u7528<\/h5>\n<p>\u4e00\u7ad9\u5f0f\u5b8c\u6210 TCP \u670d\u52a1\u5668\u7684\u521d\u59cb\u5316\u5de5\u4f5c&#xff0c;\u6267\u884c TCP \u670d\u52a1\u5668\u7684\u7ecf\u5178\u4e09\u6b65\u64cd\u4f5c&#xff0c;\u6210\u529f\u8fd4\u56de\u300c\u76d1\u542c\u5957\u63a5\u5b57\u300d&#xff0c;\u5931\u8d25\u8fd4\u56de &#8211; 1&#xff0c;\u662f\u6240\u6709\u7f51\u7edc\u901a\u4fe1\u7684\u57fa\u7840\u3002<\/p>\n<h5>\u5185\u90e8\u6267\u884c\u7684 3 \u4e2a\u6838\u5fc3\u6b65\u9aa4&#xff08;TCP \u670d\u52a1\u5668\u5fc5\u80cc&#xff09;<\/h5>\n<li>socket()&#xff1a;\u521b\u5efa TCP \u5957\u63a5\u5b57&#xff0c;\u5f97\u5230\u4e00\u4e2a\u6587\u4ef6\u63cf\u8ff0\u7b26sockfd&#xff0c;\u76f8\u5f53\u4e8e\u670d\u52a1\u5668\u7684\u300c\u603b\u5927\u95e8\u300d&#xff1b;<\/li>\n<li>bind()&#xff1a;\u628a\u8fd9\u4e2a\u300c\u5927\u95e8\u300d\u7ed1\u5b9a\u5230 127.0.0.1:80&#xff08;\u672c\u5730 IP&#043;HTTP \u9ed8\u8ba4 80 \u7aef\u53e3&#xff09;&#xff0c;\u4ece\u6b64\u8fd9\u4e2a\u7aef\u53e3\u5c31\u5f52\u670d\u52a1\u5668\u7528\u4e86&#xff1b;<\/li>\n<li>listen()&#xff1a;\u5f00\u542f\u76d1\u542c&#xff0c;\u8ba9\u670d\u52a1\u5668\u7684\u300c\u5927\u95e8\u300d\u5904\u4e8e\u7b49\u5f85\u72b6\u6001&#xff0c;\u7b49\u5f85\u6d4f\u89c8\u5668\u6765\u8fde\u63a5&#xff0c;\u53c2\u65705\u662f\u6700\u591a\u540c\u65f6\u7b49\u5f85 5 \u4e2a\u672a\u5904\u7406\u7684\u8fde\u63a5\u3002<\/li>\n<h5>\u6613\u9519\u70b9&#xff08;\u9762\u8bd5\u5e38\u95ee&#xff09;<\/h5>\n<ul>\n<li>\u8fd0\u884c\u65f6\u62a5 bind err&#xff1a;\u56e0\u4e3a 80 \u7aef\u53e3\u662f Linux \u7684\u7279\u6743\u7aef\u53e3&#xff08;1024 \u4ee5\u4e0b&#xff09;&#xff0c;\u666e\u901a\u7528\u6237\u6ca1\u6743\u9650\u7ed1\u5b9a&#xff0c;\u5fc5\u987b\u7528 sudo .\/myhttp \u8fd0\u884c&#xff1b;<\/li>\n<li>\u8fd4\u56de &#8211; 1 \u90fd\u662f\u521d\u59cb\u5316\u5931\u8d25&#xff0c;\u7a0b\u5e8f\u76f4\u63a5\u9000\u51fa\u3002<\/li>\n<\/ul>\n<h4>\u2705 \u51fd\u6570 3&#xff1a;void* thread_fun(void* arg) \u3010\u6838\u5fc3\u4e1a\u52a1\u51fd\u6570&#xff1a;\u5b50\u7ebf\u7a0b\u5e72\u6d3b\u7684\u6838\u5fc3\u3011<\/h4>\n<h5>\u4f5c\u7528<\/h5>\n<p>\u8fd9\u662f\u6574\u4e2a\u7a0b\u5e8f\u7684\u4e1a\u52a1\u6838\u5fc3&#xff01;\u6bcf\u4e00\u4e2a\u6d4f\u89c8\u5668\u8fde\u63a5&#xff0c;\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u72ec\u7acb\u7684\u8fd9\u4e2a\u7ebf\u7a0b&#xff0c;\u6240\u6709\u548c\u6d4f\u89c8\u5668\u7684\u300c\u901a\u4fe1\u3001\u89e3\u6790\u8bf7\u6c42\u3001\u8bfb\u6587\u4ef6\u3001\u8fd4\u56de\u6587\u4ef6\u300d\u7684\u5de5\u4f5c&#xff0c;\u90fd\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc\u5b8c\u6210\u3002<\/p>\n<h5>\u7ed9\u8c01\u5e72\u6d3b&#xff1f;<\/h5>\n<p>\u53c2\u6570arg\u662f\u4e3b\u7ebf\u7a0b\u4f20\u8fc7\u6765\u7684\u300c\u5ba2\u6237\u7aef\u5957\u63a5\u5b57 c\u300d&#xff0c;\u8fd9\u4e2ac\u662f\u6d4f\u89c8\u5668\u548c\u670d\u52a1\u5668\u7684\u4e13\u5c5e\u901a\u4fe1\u901a\u9053&#xff0c;\u6bcf\u4e2a\u6d4f\u89c8\u5668\u7684c\u90fd\u4e0d\u4e00\u6837&#xff0c;\u4e92\u4e0d\u5e72\u6270\u3002<\/p>\n<h5>\u5185\u90e8\u5b8c\u6574\u6267\u884c\u6d41\u7a0b&#xff08;\u6309\u987a\u5e8f&#xff0c;\u548c\u4ee3\u7801\u4e00\u4e00\u5bf9\u5e94&#xff0c;\u5fc5\u8bb0&#xff09;<\/h5>\n<p>plaintext<\/p>\n<p>1. \u63a5\u6536\u4e3b\u7ebf\u7a0b\u4f20\u7684\u5ba2\u6237\u7aef\u5957\u63a5\u5b57c \u2192 free\u91ca\u653e\u5806\u5185\u5b58&#xff08;\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f&#xff09;<br \/>\n2. recv(c, buff, &#8230;) \u963b\u585e\u63a5\u6536\u6d4f\u89c8\u5668\u53d1\u6765\u7684HTTP\u8bf7\u6c42\u6570\u636e<br \/>\n3. \u5982\u679c\u63a5\u6536\u5931\u8d25\/\u6d4f\u89c8\u5668\u65ad\u5f00 \u2192 \u5173\u95edc&#xff0c;\u7ebf\u7a0b\u9000\u51fa<br \/>\n4. \u8c03\u7528get_filename\u89e3\u6790\u51fa\u8981\u8bbf\u95ee\u7684\u6587\u4ef6\u8def\u5f84<br \/>\n5. \u62fc\u63a5\u5b8c\u6574\u6587\u4ef6\u8def\u5f84&#xff1a;\u6839\u76ee\u5f55PATH &#043; \u6587\u4ef6\u8def\u5f84 \u2192 \u6bd4\u5982 \/home\/stu\/c2305\/day22\/index.html<br \/>\n6. open(path, O_RDONLY) \u4ee5\u53ea\u8bfb\u65b9\u5f0f\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6<br \/>\n7. \u7528lseek\u8ba1\u7b97\u6587\u4ef6\u5927\u5c0f&#xff1a;\u5149\u6807\u79fb\u5230\u6587\u4ef6\u672b\u5c3e\u5f97\u5230\u603b\u5b57\u8282\u6570 \u2192 \u5149\u6807\u79fb\u56de\u5f00\u5934\u51c6\u5907\u8bfb\u53d6<br \/>\n8. \u62fc\u63a5HTTP\u54cd\u5e94\u5934&#xff1a;\u6309HTTP\u534f\u8bae\u89c4\u8303&#xff0c;\u544a\u8bc9\u6d4f\u89c8\u5668\u300c\u8bf7\u6c42\u6210\u529f&#xff0c;\u63a5\u4e0b\u6765\u7ed9\u4f60\u53d1\u6587\u4ef6\u300d<br \/>\n9. send\u53d1\u9001\u54cd\u5e94\u5934 \u2192 \u5faa\u73afread\u8bfb\u6587\u4ef6\u5185\u5bb9 &#043; send\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668<br \/>\n10. \u8bfb\u5b8c\u6587\u4ef6\u540e&#xff0c;close\u5173\u95ed\u6587\u4ef6\u3001close\u5173\u95ed\u5ba2\u6237\u7aef\u5957\u63a5\u5b57 \u2192 \u7ebf\u7a0b\u5b8c\u6210\u5de5\u4f5c&#xff0c;\u81ea\u52a8\u9000\u51fa<\/p>\n<p>\u8865\u5145\u7684\u300c\u6253\u5f00\u6587\u4ef6\u3001lseek \u7b97\u5927\u5c0f\u3001\u62fc\u54cd\u5e94\u5934\u3001\u8bfb\u6587\u4ef6\u53d1\u9001\u300d\u7684\u4ee3\u7801&#xff0c;\u5b8c\u6574\u653e\u5728\u8fd9\u4e2a\u51fd\u6570\u91cc&#xff0c;\u8fd9\u90e8\u5206\u662fHTTP \u670d\u52a1\u5668\u7684\u6838\u5fc3\u80fd\u529b&#xff1a;\u4ece\u300c\u53ea\u8fd4\u56de OK\u300d\u5347\u7ea7\u4e3a\u300c\u8fd4\u56de\u771f\u5b9e\u7f51\u9875\u6587\u4ef6\u300d\u3002<\/p>\n<hr \/>\n<h4>\u56db\u3001\u7b2c\u4e09\u90e8\u5206&#xff1a;main() \u4e3b\u51fd\u6570 \u3010\u7a0b\u5e8f\u5165\u53e3 &#043; \u603b\u6307\u6325&#xff0c;\u6700\u6838\u5fc3\u7684\u8c03\u5ea6\u903b\u8f91\u3011<\/h4>\n<p>\u8fd9\u662f\u7a0b\u5e8f\u7684\u5165\u53e3&#xff0c;\u6240\u6709\u4ee3\u7801\u4ece\u8fd9\u91cc\u5f00\u59cb\u6267\u884c&#xff0c;\u4e3b\u7ebf\u7a0b\u53ea\u505a\u4e00\u4ef6\u4e8b&#xff1a;\u65e0\u9650\u5faa\u73af\u63a5\u6536\u8fde\u63a5&#xff0c;\u4e3a\u6bcf\u4e2a\u8fde\u63a5\u521b\u5efa\u72ec\u7acb\u7ebf\u7a0b&#xff0c;\u4ec0\u4e48\u4e1a\u52a1\u90fd\u4e0d\u5904\u7406&#xff0c;\u4fdd\u8bc1\u6c38\u4e0d\u5361\u987f&#xff0c;\u662f\u591a\u7ebf\u7a0b\u5e76\u53d1\u7684\u7cbe\u9ad3&#xff01;<\/p>\n<h4>main \u51fd\u6570\u5b8c\u6574\u6267\u884c\u6d41\u7a0b&#xff08;\u9010\u884c\u903b\u8f91&#xff0c;\u5fc5\u80cc&#xff09;<\/h4>\n<li>\u8c03\u7528 socket_init() \u521d\u59cb\u5316\u670d\u52a1\u5668&#xff0c;\u62ff\u5230\u76d1\u542c\u5957\u63a5\u5b57sockfd&#xff0c;\u521d\u59cb\u5316\u5931\u8d25\u5219\u76f4\u63a5\u9000\u51fa\u7a0b\u5e8f&#xff1b;<\/li>\n<li>\u8bbe\u7f6e\u7ebf\u7a0b\u5206\u79bb\u5c5e\u6027&#xff1a;\u8fd9\u662f\u89e3\u51b3\u5185\u5b58\u6cc4\u6f0f\u7684\u5173\u952e&#xff01;\u5b50\u7ebf\u7a0b\u5e72\u5b8c\u6d3b\u9000\u51fa\u540e&#xff0c;\u7cfb\u7edf\u4f1a\u81ea\u52a8\u56de\u6536\u7ebf\u7a0b\u8d44\u6e90&#xff0c;\u4e0d\u7528\u4e3b\u7ebf\u7a0b\u64cd\u5fc3&#xff1b;<\/li>\n<li>\u8fdb\u5165 while(1) \u6b7b\u5faa\u73af&#xff1a;\u670d\u52a1\u5668\u6c38\u4e45\u8fd0\u884c&#xff0c;\u6c38\u4e0d\u9000\u51fa&#xff0c;\u4e00\u76f4\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#xff1b;<\/li>\n<li>\u8c03\u7528 accept(sockfd, &#8230;) \u963b\u585e\u7b49\u5f85&#xff1a;\u6709\u6d4f\u89c8\u5668\u6765\u8fde\u63a5\u65f6&#xff0c;\u5c31\u8fd4\u56de\u4e00\u4e2a\u300c\u5ba2\u6237\u7aef\u5957\u63a5\u5b57 c\u300d&#xff0c;\u8fd9\u4e2ac\u662f\u548c\u8be5\u6d4f\u89c8\u5668\u7684\u4e13\u5c5e\u901a\u9053&#xff1b;<\/li>\n<li>\u7528malloc\u7533\u8bf7\u5806\u5185\u5b58\u5b58\u653ec&#xff1a;\u91cd\u4e2d\u4e4b\u91cd&#xff01;\u9762\u8bd5\u5fc5\u8003&#xff01;\n<ul>\n<li>\u4e3a\u4ec0\u4e48\u4e0d\u7528 &amp;c \u76f4\u63a5\u4f20&#xff1f;\u56e0\u4e3ac\u662f\u4e3b\u7ebf\u7a0b\u7684\u6808\u53d8\u91cf&#xff0c;\u4e3b\u7ebf\u7a0b\u4f1a\u7acb\u523b\u56de\u5230\u5faa\u73af&#xff0c;\u6808\u53d8\u91cf\u7684\u503c\u4f1a\u88ab\u4e0b\u4e00\u4e2a\u6d4f\u89c8\u5668\u7684\u8fde\u63a5\u8986\u76d6&#xff0c;\u5b50\u7ebf\u7a0b\u4f1a\u62ff\u5230\u9519\u8bef\u7684\u503c&#xff1b;<\/li>\n<li>\u7528malloc\u7533\u8bf7\u7684\u662f\u5806\u5185\u5b58&#xff0c;\u72ec\u7acb\u4e0d\u53d7\u5f71\u54cd&#xff0c;\u5b50\u7ebf\u7a0b\u62ff\u5230\u540efree\u91ca\u653e\u5373\u53ef&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u8c03\u7528 pthread_create \u521b\u5efa\u5b50\u7ebf\u7a0b&#xff1a;\u628a\u5806\u5185\u5b58\u7684\u5730\u5740\u4f20\u7ed9\u5b50\u7ebf\u7a0b\u51fd\u6570thread_fun&#xff0c;\u5b50\u7ebf\u7a0b\u5f00\u59cb\u5e72\u6d3b&#xff0c;\u4e3b\u7ebf\u7a0b\u7acb\u523b\u56de\u5230accept\u7ee7\u7eed\u7b49\u4e0b\u4e00\u4e2a\u8fde\u63a5\u3002<\/li>\n<h4>\u6838\u5fc3\u7cbe\u9ad3<\/h4>\n<p>\u4e3b\u7ebf\u7a0b \u53ea\u505a\u300c\u63a5\u5ba2\u300d&#xff0c;\u5b50\u7ebf\u7a0b \u53ea\u505a\u300c\u5e72\u6d3b\u300d&#xff0c;\u4e3b\u7ebf\u7a0b\u6c38\u8fdc\u4e0d\u4f1a\u963b\u585e\u5728\u4e1a\u52a1\u903b\u8f91\u4e0a&#xff0c;\u53ef\u4ee5\u540c\u65f6\u63a5\u6536\u65e0\u6570\u4e2a\u6d4f\u89c8\u5668\u7684\u8fde\u63a5&#xff0c;\u8fd9\u5c31\u662f\u300c\u591a\u7ebf\u7a0b\u5e76\u53d1\u300d&#xff01;<\/p>\n<hr \/>\n<h4>\u4e94\u3001\u6574\u6bb5\u4ee3\u7801\u7684\u3010\u5b8c\u6574\u8fd0\u884c\u6d41\u7a0b\u3011&#xff08;\u4ece\u5934\u5230\u5c3e\u4e32\u4e00\u904d&#xff0c;\u6253\u901a\u4efb\u7763\u4e8c\u8109&#xff09;<\/h4>\n<p>\u628a\u6240\u6709\u903b\u8f91\u4e32\u8d77\u6765&#xff0c;\u4e00\u6b65\u4e00\u6b65\u6267\u884c&#xff0c;\u8fd9\u5c31\u662f\u4f60\u8fd0\u884c\u7a0b\u5e8f\u540e&#xff0c;\u5b8c\u6574\u7684\u6267\u884c\u8fc7\u7a0b&#xff0c;\u80cc\u4e0b\u6765&#xff0c;\u9762\u8bd5\u76f4\u63a5\u8bf4&#xff0c;\u6ee1\u5206\u7b54\u6848&#xff01;<\/p>\n<p>plaintext<\/p>\n<p>1. \u7f16\u8bd1\u4ee3\u7801&#xff1a;gcc -o myhttp myhttp.c -lpthread &#xff08;\u52a0-lpthread\u94fe\u63a5\u7ebf\u7a0b\u5e93&#xff09;<br \/>\n2. \u8fd0\u884c\u7a0b\u5e8f&#xff1a;sudo .\/myhttp &#xff08;sudo\u63d0\u6743&#xff0c;\u7ed1\u5b9a80\u7aef\u53e3\u6210\u529f&#xff0c;\u670d\u52a1\u5668\u542f\u52a8&#xff09;<br \/>\n3. \u4e3b\u7ebf\u7a0b\u8fdb\u5165\u6b7b\u5faa\u73af&#xff0c;\u963b\u585e\u5728accept()&#xff0c;\u7b49\u5f85\u6d4f\u89c8\u5668\u8fde\u63a5<br \/>\n4. \u6253\u5f00\u6d4f\u89c8\u5668&#xff0c;\u8f93\u5165 http:\/\/127.0.0.1 \u8bbf\u95ee\u672c\u673a<br \/>\n5. accept()\u6536\u5230\u8fde\u63a5&#xff0c;\u8fd4\u56de\u5ba2\u6237\u7aef\u5957\u63a5\u5b57c \u2192 malloc\u5b58c \u2192 \u521b\u5efa\u5b50\u7ebf\u7a0b<br \/>\n6. \u5b50\u7ebf\u7a0b\u6267\u884cthread_fun&#xff1a;<br \/>\n   &#8211; recv\u63a5\u6536\u6d4f\u89c8\u5668\u7684HTTP\u8bf7\u6c42 \u2192 get_filename\u89e3\u6790\u51fa \/ \u2192 \u66ff\u6362\u6210\/index.html<br \/>\n   &#8211; \u62fc\u63a5\u8def\u5f84 \/home\/stu\/c2305\/day22\/index.html \u2192 open\u6253\u5f00\u8fd9\u4e2a\u6587\u4ef6<br \/>\n   &#8211; lseek\u7b97\u6587\u4ef6\u5927\u5c0f \u2192 \u62fc\u63a5HTTP\u54cd\u5e94\u5934 \u2192 send\u53d1\u9001\u54cd\u5e94\u5934<br \/>\n   &#8211; \u5faa\u73afread\u8bfb\u6587\u4ef6\u5185\u5bb9&#xff0c;send\u53d1\u9001\u7ed9\u6d4f\u89c8\u5668<br \/>\n7. \u6d4f\u89c8\u5668\u6536\u5230\u6587\u4ef6\u5185\u5bb9&#xff0c;\u6e32\u67d3\u51fa\u7f51\u9875\u9875\u9762<br \/>\n8. \u5b50\u7ebf\u7a0bclose\u6587\u4ef6\u3001close\u5ba2\u6237\u7aef\u5957\u63a5\u5b57 \u2192 \u7ebf\u7a0b\u9000\u51fa&#xff0c;\u7cfb\u7edf\u81ea\u52a8\u56de\u6536\u8d44\u6e90<br \/>\n9. \u4e3b\u7ebf\u7a0b\u4e00\u76f4\u5361\u5728accept()&#xff0c;\u7b49\u5f85\u4e0b\u4e00\u4e2a\u6d4f\u89c8\u5668\u8bbf\u95ee&#xff0c;\u5faa\u73af\u5f80\u590d&#xff0c;\u6c38\u4e45\u8fd0\u884c<\/p>\n<hr \/>\n<h4>\u516d\u3001\u6838\u5fc3\u77e5\u8bc6\u70b9\u603b\u7ed3 &#043; \u9762\u8bd5\u5fc5\u80cc\u8003\u70b9&#xff08;\u5168\u90e8\u662f\u4f60\u4ee3\u7801\u91cc\u7684\u91cd\u70b9&#xff09;<\/h4>\n<h4>\u2705 1. \u591a\u7ebf\u7a0b\u7684\u6838\u5fc3\u4f18\u52bf<\/h4>\n<p>\u4e00\u4e2a\u7ebf\u7a0b\u5904\u7406\u4e00\u4e2a\u5ba2\u6237\u7aef&#xff0c;\u652f\u6301\u5e76\u53d1\u8bbf\u95ee&#xff1a;\u591a\u4e2a\u6d4f\u89c8\u5668\u53ef\u4ee5\u540c\u65f6\u8bbf\u95ee\u4f60\u7684\u670d\u52a1\u5668&#xff0c;\u4e92\u4e0d\u5f71\u54cd&#xff0c;\u6548\u7387\u8fdc\u9ad8\u4e8e\u5355\u7ebf\u7a0b\u3002<\/p>\n<h4>\u2705 2. \u4e24\u4e2a\u5185\u5b58\u6cc4\u6f0f\u7684\u89e3\u51b3\u65b9\u6848&#xff08;\u4ee3\u7801\u91cc\u90fd\u505a\u4e86&#xff0c;\u5fc5\u8bb0&#xff09;<\/h4>\n<ul>\n<li>\u5806\u5185\u5b58\u6cc4\u6f0f&#xff1a;\u7528malloc\u4f20\u53c2&#xff0c;\u5b50\u7ebf\u7a0b\u91ccfree\u91ca\u653e&#xff1b;<\/li>\n<li>\u7ebf\u7a0b\u8d44\u6e90\u6cc4\u6f0f&#xff1a;\u8bbe\u7f6e\u7ebf\u7a0b\u5206\u79bb\u5c5e\u6027&#xff0c;\u7ebf\u7a0b\u9000\u51fa\u81ea\u52a8\u56de\u6536\u8d44\u6e90\u3002<\/li>\n<\/ul>\n<h4>\u2705 3. TCP \u548c HTTP \u7684\u5173\u7cfb&#xff08;\u4f60\u8fd9\u4e2a\u4ee3\u7801\u5b8c\u7f8e\u4f53\u73b0&#xff09;<\/h4>\n<ul>\n<li>TCP \u662f\u4f20\u8f93\u5c42\u534f\u8bae&#xff1a;\u8d1f\u8d23\u5efa\u7acb\u53ef\u9760\u7684\u8fde\u63a5\u3001\u6536\u53d1\u6570\u636e&#xff0c;\u662f\u5e95\u5c42\u901a\u4fe1\u4fdd\u969c&#xff1b;<\/li>\n<li>HTTP \u662f\u5e94\u7528\u5c42\u534f\u8bae&#xff1a;\u662f TCP \u4e4b\u4e0a\u7684\u89c4\u5219&#xff0c;\u89c4\u5b9a\u4e86\u8bf7\u6c42\u548c\u54cd\u5e94\u7684\u683c\u5f0f&#xff1b;<\/li>\n<li>\u4f60\u7684\u4ee3\u7801\u672c\u8d28\u662f&#xff1a;\u57fa\u4e8e TCP \u534f\u8bae\u5b9e\u73b0\u4e86 HTTP \u534f\u8bae\u7684\u6838\u5fc3\u89c4\u5219\u3002<\/li>\n<\/ul>\n<h4>\u2705 4. \u5173\u952e\u51fd\u6570\u5206\u7c7b\u8bb0\u5fc6<\/h4>\n<ul>\n<li>TCP \u670d\u52a1\u5668\u56db\u4ef6\u5957&#xff1a;socket \u2192 bind \u2192 listen \u2192 accept<\/li>\n<li>\u6570\u636e\u6536\u53d1&#xff1a;recv \u6536\u6570\u636e\u3001send \u53d1\u6570\u636e<\/li>\n<li>\u6587\u4ef6\u64cd\u4f5c&#xff1a;open \u6253\u5f00\u3001read \u8bfb\u53d6\u3001close \u5173\u95ed\u3001lseek \u8ba1\u7b97\u5927\u5c0f<\/li>\n<li>\u7ebf\u7a0b\u64cd\u4f5c&#xff1a;pthread_create \u521b\u5efa\u3001pthread_exit \u9000\u51fa<\/li>\n<\/ul>\n<hr \/>\n<h4>\u6700\u540e\u8865\u5145&#xff1a;\u8fd0\u884c\u524d\u7684\u51c6\u5907\u5de5\u4f5c&#xff08;\u4fdd\u8bc1\u4e00\u6b21\u6210\u529f&#xff09;<\/h4>\n<p>\u5728\u4f60\u7684\u76ee\u5f55 \/home\/stu\/c2305\/day22 \u4e0b&#xff0c;\u521b\u5efa\u4e00\u4e2a\u6d4b\u8bd5\u7684index.html\u6587\u4ef6&#xff0c;\u968f\u4fbf\u5199\u5185\u5bb9\u5c31\u884c&#xff1a;<\/p>\n<p>cd \/home\/stu\/c2305\/day22<br \/>\ntouch index.html<br \/>\necho &#034;&lt;h1&gt;\u6211\u7684HTTP\u670d\u52a1\u5668\u8fd0\u884c\u6210\u529f&#xff01;&lt;\/h1&gt;&lt;p&gt;\u8fd9\u662f\u6211\u7684\u7b2c\u4e00\u4e2a\u7f51\u9875&lt;\/p&gt;&#034; &gt; index.html<\/p>\n<p>\u5199\u5b8c\u540e\u6d4f\u89c8\u5668\u8bbf\u95ee http:\/\/127.0.0.1&#xff0c;\u5c31\u80fd\u770b\u5230\u9875\u9762\u4e86&#xff01;<\/p>\n<p>\u6d4b\u8bd5:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"489\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114094138-696764d29affb.png\" width=\"924\" \/><\/p>\n<h4>\u4e00\u3001\u6d4b\u8bd5\u8fc7\u7a0b&#xff08;\u6b65\u9aa4\u6e05\u6670&#xff09;<\/h4>\n<li>\u51c6\u5907\u5de5\u4f5c&#xff1a;\u5728\u670d\u52a1\u5668\u6839\u76ee\u5f55 \/home\/stu\/c2305\/day22 \u4e0b&#xff0c;\u653e\u5165\u4e86\u56fe\u7247\u6587\u4ef6 dog2.jpeg&#xff08;\u548c index.html \u540c\u76ee\u5f55&#xff09;&#xff1b;<\/li>\n<li>\u542f\u52a8\u670d\u52a1\u5668&#xff1a;\u7528 sudo .\/myhttp \u542f\u52a8\u670d\u52a1\u5668&#xff08;\u7ed1\u5b9a 80 \u7aef\u53e3&#xff09;&#xff1b;<\/li>\n<li>\u6d4f\u89c8\u5668\u8bbf\u95ee&#xff1a;\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165 http:\/\/127.0.0.1\/dog2.jpeg&#xff0c;\u8bf7\u6c42\u8bbf\u95ee\u8be5\u56fe\u7247&#xff1b;<\/li>\n<li>\u670d\u52a1\u5668\u5904\u7406&#xff1a;\n<ul>\n<li>\u63a5\u6536\u6d4f\u89c8\u5668\u7684 HTTP \u8bf7\u6c42&#xff08;GET \/dog2.jpeg HTTP\/1.1&#xff09;&#xff1b;<\/li>\n<li>\u89e3\u6790\u51fa\u6587\u4ef6\u8def\u5f84 \/dog2.jpeg&#xff0c;\u62fc\u63a5\u4e3a\u5b8c\u6574\u8def\u5f84 \/home\/stu\/c2305\/day22\/dog2.jpeg&#xff1b;<\/li>\n<li>\u6253\u5f00\u56fe\u7247\u6587\u4ef6&#xff0c;\u8ba1\u7b97\u5927\u5c0f\u3001\u6784\u9020 HTTP \u54cd\u5e94\u5934\u3001\u53d1\u9001\u6587\u4ef6\u5185\u5bb9&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u6d4f\u89c8\u5668\u6e32\u67d3&#xff1a;\u6210\u529f\u63a5\u6536\u56fe\u7247\u6570\u636e&#xff0c;\u5728\u9875\u9762\u4e0a\u663e\u793a dog2.jpeg\u3002<\/li>\n<h4>\u4e8c\u3001\u7ec8\u7aef\u65e5\u5fd7\u89e3\u6790&#xff08;\u6bcf\u4e00\u884c\u7684\u542b\u4e49&#xff09;<\/h4>\n<p>plaintext<\/p>\n<p>buff&#061;GET \/dog2.jpeg HTTP\/1.1&#8230;  # \u670d\u52a1\u5668\u6536\u5230\u6d4f\u89c8\u5668\u7684HTTP\u8bf7\u6c42&#xff0c;\u8bf7\u6c42\u65b9\u6cd5\u662fGET&#xff0c;\u76ee\u6807\u8d44\u6e90\u662f\/dog2.jpeg<br \/>\n\u8bf7\u6c42\u65b9\u6cd5: GET                    # \u89e3\u6790\u51fa\u8bf7\u6c42\u65b9\u6cd5\u4e3aGET<br \/>\npath:\/home\/stu\/c2305\/day22\/dog2.jpeg  # \u62fc\u63a5\u540e\u7684\u5b8c\u6574\u56fe\u7247\u8def\u5f84<\/p>\n<p>\u8fd9\u4e9b\u65e5\u5fd7\u5bf9\u5e94\u4ee3\u7801\u4e2d\u7684 printf \u8f93\u51fa&#xff0c;\u8bc1\u660e\u670d\u52a1\u5668\u6210\u529f\u89e3\u6790\u4e86\u8bf7\u6c42\u5e76\u5b9a\u4f4d\u5230\u4e86\u56fe\u7247\u6587\u4ef6\u3002<\/p>\n<h4>\u4e09\u3001\u6838\u5fc3\u9a8c\u8bc1\u70b9&#xff08;\u670d\u52a1\u5668\u7684\u80fd\u529b\u5347\u7ea7&#xff09;<\/h4>\n<ul>\n<li>\u539f\u4ee3\u7801\u53ea\u80fd\u8fd4\u56de HTML \u6587\u4ef6&#xff0c;\u8fd9\u6b21\u6d4b\u8bd5\u9a8c\u8bc1\u4e86&#xff1a;\u670d\u52a1\u5668\u652f\u6301\u8fd4\u56de\u4efb\u610f\u9759\u6001\u8d44\u6e90&#xff08;\u56fe\u7247\u3001CSS\u3001JS \u7b49&#xff09;&#xff0c;\u53ea\u8981\u8d44\u6e90\u5728\u6839\u76ee\u5f55\u4e0b&#xff0c;\u6d4f\u89c8\u5668\u5c31\u80fd\u6b63\u786e\u8bf7\u6c42\u5e76\u6e32\u67d3&#xff1b;<\/li>\n<li>\u672c\u8d28\u539f\u56e0&#xff1a;HTTP \u54cd\u5e94\u5934\u4e2d\u7684 Content-Length \u53ea\u8d1f\u8d23\u544a\u8bc9\u6d4f\u89c8\u5668\u300c\u8d44\u6e90\u5927\u5c0f\u300d&#xff0c;\u4e0d\u9650\u5236\u8d44\u6e90\u7c7b\u578b&#xff0c;\u6d4f\u89c8\u5668\u4f1a\u6839\u636e\u6587\u4ef6\u540e\u7f00\u81ea\u52a8\u8bc6\u522b\u5e76\u6e32\u67d3&#xff08;\u5982.jpeg\u4f1a\u8bc6\u522b\u4e3a\u56fe\u7247&#xff09;\u3002<\/li>\n<\/ul>\n<h4>\u56db\u3001\u4e3a\u4ec0\u4e48\u80fd\u6210\u529f\u663e\u793a\u56fe\u7247&#xff1f;<\/h4>\n<p>\u670d\u52a1\u5668\u7684\u5904\u7406\u903b\u8f91\u5bf9\u6240\u6709\u9759\u6001\u8d44\u6e90\u901a\u7528&#xff1a;<\/p>\n<li>\u4e0d\u7ba1\u662f.html\u8fd8\u662f.jpeg&#xff0c;\u670d\u52a1\u5668\u90fd\u4f1a\u6309\u300c\u6253\u5f00\u6587\u4ef6\u2192\u8ba1\u7b97\u5927\u5c0f\u2192\u53d1\u54cd\u5e94\u5934\u2192\u53d1\u6587\u4ef6\u5185\u5bb9\u300d\u7684\u6d41\u7a0b\u5904\u7406&#xff1b;<\/li>\n<li>\u6d4f\u89c8\u5668\u6536\u5230\u6570\u636e\u540e&#xff0c;\u4f1a\u6839\u636e\u8bf7\u6c42\u7684\u8d44\u6e90\u540e\u7f00&#xff08;.jpeg&#xff09;&#xff0c;\u81ea\u52a8\u4ee5\u56fe\u7247\u683c\u5f0f\u89e3\u6790\u5e76\u6e32\u67d3&#xff0c;\u65e0\u9700\u4fee\u6539\u670d\u52a1\u5668\u4ee3\u7801\u3002<\/li><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6d4f\u89c8\u5668\u8bbf\u95ee Web \u670d\u52a1\u5668\u7684 HTTP \u901a\u4fe1\u5168\u6d41\u7a0b<br \/>\n\u5305\u542b DNS \u89e3\u6790\u3001TCP \u8fde\u63a5\u3001HTTP \u6536\u53d1\u3001\u8fde\u63a5\u7ba1\u7406 4 \u4e2a\u6838\u5fc3\u73af\u8282&#xff0c;\u6211\u5206\u6b65\u9aa4\u62c6\u89e3&#xff08;\u6e05\u6670\u5230\u6bcf\u4e00\u6b65&#xff09;&#xff1a; \u6b65\u9aa4 1&#xff1a;DNS \u89e3\u6790&#xff08;\u6d4f\u89c8\u5668\u5148\u627e\u5230\u670d\u52a1\u5668\u7684 IP&#xff09;<br \/>\n\u6d4f\u89c8\u5668\u8f93\u5165www.baidu.com&#xff08;\u57df\u540d&#xff09;&#xff0c;\u4f46\u7f51\u7edc\u901a\u4fe1\u9700\u8981IP \u5730\u5740&#xff1b;\u6d4f\u89c8\u5668\u5411DNS \u670d\u52a1\u5668\u53d1\u8d77\u8bf7\u6c42&amp;<\/p>\n","protected":false},"author":2,"featured_media":59901,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[151,78,191],"topic":[],"class_list":["post-59907","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-http","tag-78","tag-191"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/59907.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001\u6d4f\u89c8\u5668\u8bbf\u95ee Web \u670d\u52a1\u5668\u7684 HTTP \u901a\u4fe1\u5168\u6d41\u7a0b \u5305\u542b DNS \u89e3\u6790\u3001TCP \u8fde\u63a5\u3001HTTP \u6536\u53d1\u3001\u8fde\u63a5\u7ba1\u7406 4 \u4e2a\u6838\u5fc3\u73af\u8282&#xff0c;\u6211\u5206\u6b65\u9aa4\u62c6\u89e3&#xff08;\u6e05\u6670\u5230\u6bcf\u4e00\u6b65&#xff09;&#xff1a; \u6b65\u9aa4 1&#xff1a;DNS \u89e3\u6790&#xff08;\u6d4f\u89c8\u5668\u5148\u627e\u5230\u670d\u52a1\u5668\u7684 IP&#xff09; \u6d4f\u89c8\u5668\u8f93\u5165www.baidu.com&#xff08;\u57df\u540d&#xff09;&#xff0c;\u4f46\u7f51\u7edc\u901a\u4fe1\u9700\u8981IP \u5730\u5740&#xff1b;\u6d4f\u89c8\u5668\u5411DNS \u670d\u52a1\u5668\u53d1\u8d77\u8bf7\u6c42&amp;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/59907.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-14T09:41:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114094137-696764d1b35a0.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/59907.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/59907.html\",\"name\":\"http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-14T09:41:39+00:00\",\"dateModified\":\"2026-01-14T09:41:39+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/59907.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/59907.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/59907.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/59907.html","og_locale":"zh_CN","og_type":"article","og_title":"http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001\u6d4f\u89c8\u5668\u8bbf\u95ee Web \u670d\u52a1\u5668\u7684 HTTP \u901a\u4fe1\u5168\u6d41\u7a0b \u5305\u542b DNS \u89e3\u6790\u3001TCP \u8fde\u63a5\u3001HTTP \u6536\u53d1\u3001\u8fde\u63a5\u7ba1\u7406 4 \u4e2a\u6838\u5fc3\u73af\u8282&#xff0c;\u6211\u5206\u6b65\u9aa4\u62c6\u89e3&#xff08;\u6e05\u6670\u5230\u6bcf\u4e00\u6b65&#xff09;&#xff1a; \u6b65\u9aa4 1&#xff1a;DNS \u89e3\u6790&#xff08;\u6d4f\u89c8\u5668\u5148\u627e\u5230\u670d\u52a1\u5668\u7684 IP&#xff09; \u6d4f\u89c8\u5668\u8f93\u5165www.baidu.com&#xff08;\u57df\u540d&#xff09;&#xff0c;\u4f46\u7f51\u7edc\u901a\u4fe1\u9700\u8981IP \u5730\u5740&#xff1b;\u6d4f\u89c8\u5668\u5411DNS \u670d\u52a1\u5668\u53d1\u8d77\u8bf7\u6c42&amp;","og_url":"https:\/\/www.wsisp.com\/helps\/59907.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-14T09:41:39+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260114094137-696764d1b35a0.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"14 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/59907.html","url":"https:\/\/www.wsisp.com\/helps\/59907.html","name":"http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-14T09:41:39+00:00","dateModified":"2026-01-14T09:41:39+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/59907.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/59907.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/59907.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"http\u5b9e\u73b0\u670d\u52a1\u5668\u4e0e\u6d4f\u89c8\u5668\u901a\u4fe1"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/59907","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=59907"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/59907\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/59901"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=59907"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=59907"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=59907"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=59907"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}