总结socket和http的区别 HTTP和Socket连接的区别
http\u548csocket\u901a\u4fe1\u7684\u533a\u522b\u5176\u5b9e\u8fd9\u6837\u6bd4\u662f\u6709\u95ee\u9898\u7684\uff0chttp\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd\u5e94\u7528\u7ea7\u534f\u8bae\uff0c\u5e38\u7528\u7684\u662fhttp1.0\u548chttp 1.1\u534f\u8bae\u3002\u800csocket\u5219\u662f\u4e00\u4e2a\u8fde\u63a5\uff0c\u5b83\u76f4\u63a5\u5728\u94fe\u8def\u5c42\u4e0a\u8fdb\u884c\u8fde\u63a5\u3002\u5728\u8be5\u8fde\u63a5\u7684\u57fa\u7840\u4e4b\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u5e94\u7528\u7ea7\u534f\u8bae\u3002\u5982\u901a\u8fc7socket\u53ef\u4ee5\u5b9e\u73b0ftp\u534f\u8bae\u3001http\u3001https\u3001NetBIOS\u3001Win\u3001Iron\u7b49\u7b49\uff0c\u751a\u81f3\u4e8e\u53ef\u4ee5\u5b9e\u73b0\u84dd\u7259\uff08\u84dd\u7259\u8fde\u63a5\u5728.net\u4e2d\u4e0d\u88ab\u652f\u6301\uff0c\u662f\u56e0\u4e3asocket\u6240\u9700\u53c2\u6570\u7684\u534f\u8bae\u7c07\u4e2d\u679a\u4e3e\u4e2d\u6ca1\u6709\u84dd\u7259\uff0c\u4f46\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u6570\u636e\uff0c\u5b9e\u73b0\u4e0e\u84dd\u7259\u7684\u901a\u4fe1\uff09\u3002\u6240\u4ee5http\u662f\u4e00\u4e2a\u534f\u8bae\u4e0eSocket\u8fde\u63a5\u662f\u6ca1\u6709\u53ef\u6bd4\u6027\u7684\uff0c\u5728.net\u4e2d\uff0c\u53ef\u5b9e\u73b0http\u534f\u8bae\u7684\u8fde\u63a5\u3002
\u6b63\u662f\u56e0\u4e3asocket\u529f\u80fd\u7684\u5f3a\u5927\uff0c\u6240\u4ee5\u5bf9\u4e8esocket\u7f16\u7a0b\u4e00\u822c\u7528\u4e8e\u7279\u6b8a\u4e00\u70b9\u7684\u534f\u8bae\u6216\u9ad8\u6027\u80fd\u7f51\u7edc\u8981\u6c42\u7684\u60c5\u51b5\u4e0b\u624d\u4f7f\u7528\u8fd9\u79cd\u7f16\u7a0b\u3002\u5982\u679c\u662f\u4e00\u822c\u7684tcp/ip\uff0c\u5728.net\u4e2d\u5df2\u7ecf\u5b9e\u73b0\u4e86\u5176\u529f\u80fd\u5c01\u88c5\u3002\u4f8b\u5982WebClient\u5b9e\u73b0http/https,\u518d\u5982.net 4.5\u65b0\u589e\u7684HttpClient\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u540c\u7684\u529f\u80fd\u3002\u4f46\u8fd9\u4e9b\u529f\u80fd\u8f83\u4e3a\u72ed\u7a84\uff0c\u90a3\u4e48\u5728.net\u4e2d\u7684\u53ef\u63d2\u5165\u534f\u8bae\u662f\u4e00\u4e2a\u4e2d\u5eb8\u7684\u9009\u62e9\u3002\u76ee\u524d\u5b9e\u73b0\u7684\u6709HttpWebRequest/FtpWebRequest\u3002\u5e76\u4e14\u53ef\u7ee7\u627fWebRequest\u5b9e\u73b0\u81ea\u5df1\u7684\u53ef\u63d2\u5165\u534f\u8bae\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u4e0ehttpClient\u6216WebClient\u4e0eScoket\u76f8\u6bd4\u7684\u8bdd\uff0cWebClinet/HttpClient\u662f\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684Socket\u5c01\u88c5\uff0c\u5b83\u51cf\u5c11\u5f00\u53d1\u5468\u671f\uff0c\u4f46\u5e95\u5c42\u8fd8\u662fSocket\u3002\u5982\u679cWebRequest\u4e0eSocket\u76f8\u6bd4\u7684\u8bdd\uff0cWebRequest\u662f\u5b9e\u73b0\u7279\u5b9a\u534f\u8bae\u7684Socket\u5c01\u95ed\uff0c\u5e76\u53ef\u5b9e\u73b0\u53ef\u63d2\u5165\u534f\u8bae\u3002\u6362\u53e5\u6765\u8bf4\uff0cWebClient\u662f\u5b8c\u5b8c\u5168\u5168\u7684\u4e00\u4e2a\u901a\u4fe1\u7c7b\uff0c\u800cWebRequest\u5219\u7b97\u662f\u4e00\u4e2a\u534f\u8bae\u5f00\u53d1\u7c7b\uff08\u5b83\u5df2\u7ecf\u5b9e\u73b0\u4e86FtpWebRequest\u53caHttpWebRequest),\u800cSocket\u5219\u662f\u5e95\u5c42\u5f00\u53d1\u3002
\u5176\u5b9e\u8fd9\u6837\u6bd4\u662f\u6709\u95ee\u9898\u7684\uff0chttp\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd\u5e94\u7528\u7ea7\u534f\u8bae\uff0c\u5e38\u7528\u7684\u662fhttp1.0\u548chttp 1.1\u534f\u8bae\u3002\u800csocket\u5219\u662f\u4e00\u4e2a\u8fde\u63a5\uff0c\u5b83\u76f4\u63a5\u5728\u94fe\u8def\u5c42\u4e0a\u8fdb\u884c\u8fde\u63a5\u3002\u5728\u8be5\u8fde\u63a5\u7684\u57fa\u7840\u4e4b\u53ef\u4ee5\u5b9e\u73b0\u5404\u79cd\u5e94\u7528\u7ea7\u534f\u8bae\u3002\u5982\u901a\u8fc7socket\u53ef\u4ee5\u5b9e\u73b0ftp\u534f\u8bae\u3001http\u3001https\u3001NetBIOS\u3001Win\u3001Iron\u7b49\u7b49\uff0c\u751a\u81f3\u4e8e\u53ef\u4ee5\u5b9e\u73b0\u84dd\u7259\uff08\u84dd\u7259\u8fde\u63a5\u5728.net\u4e2d\u4e0d\u88ab\u652f\u6301\uff0c\u662f\u56e0\u4e3asocket\u6240\u9700\u53c2\u6570\u7684\u534f\u8bae\u7c07\u4e2d\u679a\u4e3e\u4e2d\u6ca1\u6709\u84dd\u7259\uff0c\u4f46\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u6570\u636e\uff0c\u5b9e\u73b0\u4e0e\u84dd\u7259\u7684\u901a\u4fe1\uff09\u3002\u6240\u4ee5http\u662f\u4e00\u4e2a\u534f\u8bae\u4e0eSocket\u8fde\u63a5\u662f\u6ca1\u6709\u53ef\u6bd4\u6027\u7684\uff0c\u5728.net\u4e2d\uff0c\u53ef\u5b9e\u73b0http\u534f\u8bae\u7684\u8fde\u63a5\u3002
\u6b63\u662f\u56e0\u4e3asocket\u529f\u80fd\u7684\u5f3a\u5927\uff0c\u6240\u4ee5\u5bf9\u4e8esocket\u7f16\u7a0b\u4e00\u822c\u7528\u4e8e\u7279\u6b8a\u4e00\u70b9\u7684\u534f\u8bae\u6216\u9ad8\u6027\u80fd\u7f51\u7edc\u8981\u6c42\u7684\u60c5\u51b5\u4e0b\u624d\u4f7f\u7528\u8fd9\u79cd\u7f16\u7a0b\u3002\u5982\u679c\u662f\u4e00\u822c\u7684tcp/ip\uff0c\u5728.net\u4e2d\u5df2\u7ecf\u5b9e\u73b0\u4e86\u5176\u529f\u80fd\u5c01\u88c5\u3002\u4f8b\u5982WebClient\u5b9e\u73b0http/https,\u518d\u5982.net 4.5\u65b0\u589e\u7684HttpClient\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u540c\u7684\u529f\u80fd\u3002\u4f46\u8fd9\u4e9b\u529f\u80fd\u8f83\u4e3a\u72ed\u7a84\uff0c\u90a3\u4e48\u5728.net\u4e2d\u7684\u53ef\u63d2\u5165\u534f\u8bae\u662f\u4e00\u4e2a\u4e2d\u5eb8\u7684\u9009\u62e9\u3002\u76ee\u524d\u5b9e\u73b0\u7684\u6709HttpWebRequest/FtpWebRequest\u3002\u5e76\u4e14\u53ef\u7ee7\u627fWebRequest\u5b9e\u73b0\u81ea\u5df1\u7684\u53ef\u63d2\u5165\u534f\u8bae\u3002
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u6211\u4eec\u4e0ehttpClient\u6216WebClient\u4e0eScoket\u76f8\u6bd4\u7684\u8bdd\uff0cWebClinet/HttpClient\u662f\u5b9e\u73b0\u7279\u5b9a\u529f\u80fd\u7684Socket\u5c01\u88c5\uff0c\u5b83\u51cf\u5c11\u5f00\u53d1\u5468\u671f\uff0c\u4f46\u5e95\u5c42\u8fd8\u662fSocket\u3002\u5982\u679cWebRequest\u4e0eSocket\u76f8\u6bd4\u7684\u8bdd\uff0cWebRequest\u662f\u5b9e\u73b0\u7279\u5b9a\u534f\u8bae\u7684Socket\u5c01\u95ed\uff0c\u5e76\u53ef\u5b9e\u73b0\u53ef\u63d2\u5165\u534f\u8bae\u3002\u6362\u53e5\u6765\u8bf4\uff0cWebClient\u662f\u5b8c\u5b8c\u5168\u5168\u7684\u4e00\u4e2a\u901a\u4fe1\u7c7b\uff0c\u800cWebRequest\u5219\u7b97\u662f\u4e00\u4e2a\u534f\u8bae\u5f00\u53d1\u7c7b\uff08\u5b83\u5df2\u7ecf\u5b9e\u73b0\u4e86FtpWebRequest\u53caHttpWebRequest),\u800cSocket\u5219\u662f\u5e95\u5c42\u5f00\u53d1\u3002
HTTP协议:简单对象访问协议,对应于应用层 ,HTTP协议是基于TCP连接的
tcp协议: 对应于传输层
ip协议: 对应于网络层
TCP/IP是传输层协议,主要解决数据如何在网络中传输;而HTTP是应用层协议,主要解决如何包装数据。
Socket是对TCP/IP协议的封装,Socket本身并不是协议,而是一个调用接口(API),通过Socket,才能使用TCP/IP协议。
http连接:http连接就是所谓的短连接,即客户端向服务器端发送一次请求,服务器端响应后连接即会断掉;
socket连接:socket连接就是所谓的长连接,理论上客户端和服务器端一旦建立起连接将不会主动断掉;但是由于各种环境因素可能会是连接断开,比如说:服务器端或客户端主机down了,网络故障,或者两者之间长时间没有数据传输,网络防火墙可能会断开该连接以释放网络资源。
绛旓細socket鍜宧ttp鍖哄埆濡備笅锛1銆乭ttp鏄鎴风鐢╤ttp鍗忚杩涜璇锋眰锛屽彂閫佽姹傜殑鏃跺欓渶瑕佸皝瑁卙ttp璇锋眰澶达紝骞剁粦瀹氳姹傜殑鏁版嵁锛屾湇鍔″櫒涓鑸湁web鏈嶅姟鍣ㄩ厤鍚(褰撶劧涔熼潪缁濆)锛http鐨璇锋眰鏂瑰紡涓哄鎴风涓诲姩鍙戣捣璇锋眰锛屾湇鍔″櫒鎵嶈兘缁欏搷搴旓紝涓娆¤姹傚畬姣曞悗鍒欐柇寮杩炴帴锛屼互鑺傜渷璧勬簮锛屾湇鍔″櫒涓嶈兘涓诲姩缁欏鎴风鍙戣捣鍝嶅簲锛屼富瑕佷娇鐢ㄧ被鏄疦...
绛旓細Socket鍜孒TTP鏄袱绉嶄笉鍚岀殑缃戠粶閫氫俊鍗忚锛屽畠浠湪浠ヤ笅鏂归潰瀛樺湪鏄庢樉鐨勫尯鍒細1. 閫氫俊鏂瑰紡锛歋ocket鏄竴绉嶅熀浜庡鎴风-鏈嶅姟鍣ㄦā寮忕殑搴曞眰閫氫俊鍗忚锛岃孒TTP鍒欐槸涓绉嶅熀浜庝紶杈撳眰鍗忚鐨勪簰鑱旂綉鍗忚銆2. 鏁版嵁浼犺緭锛歋ocket涓昏鐢ㄤ簬鏁版嵁鐨勭洿鎺ヤ紶杈擄紝閫傜敤浜庨珮閫熷疄鏃堕氫俊鍦烘櫙锛岃孒TTP鍒欏熀浜嶵CP/IP鍗忚锛岄氳繃浠g悊鏈嶅姟鍣ㄨ繘琛屾暟鎹...
绛旓細Socket鏄TCP/IP鍗忚鐨勫皝瑁咃紝Socket鏈韩骞朵笉鏄崗璁紝鑰屾槸涓涓皟鐢ㄦ帴鍙o紙API锛夛紝閫氳繃Socket锛屾墠鑳戒娇鐢═CP/IP鍗忚銆http杩炴帴锛歨ttp杩炴帴灏辨槸鎵璋撶殑鐭繛鎺ワ紝鍗冲鎴风鍚戞湇鍔″櫒绔彂閫佷竴娆¤姹傦紝鏈嶅姟鍣ㄧ鍝嶅簲鍚庤繛鎺ュ嵆浼氭柇鎺夛紱socket杩炴帴锛歴ocket杩炴帴灏辨槸鎵璋撶殑闀胯繛鎺ワ紝鐞嗚涓婂鎴风鍜屾湇鍔″櫒绔竴鏃﹀缓绔嬭捣杩炴帴灏嗕笉浼...
绛旓細HTTP鏄竴绉嶅簲鐢ㄥ眰鍗忚锛岃Socket鏄紶杈撳眰鍗忚銆傝繖涓よ呴兘鐢ㄤ簬缃戠粶鏁版嵁鐨勪紶杈擄紝浣嗗湪涓嶅悓灞傞潰涓婃湁鐫涓嶅悓鐨勫姛鑳姐備笅闈㈡槸鍏充簬杩欎袱鑰呭尯鍒殑 涓銆HTTP鍗忚 HTTP锛屽叏绉颁负瓒呮枃鏈紶杈撳崗璁紝瀹冩槸搴旂敤灞傜殑涓绉嶅崗璁傚叾涓昏鍔熻兘鏄湪缃戠粶浼犺緭鏁版嵁鐨勮繃绋嬩腑锛屽畾涔夋暟鎹浣曞憟鐜扮粰娴忚鍣ㄥ拰鏈嶅姟鍣ㄤ箣闂寸殑鏁版嵁浜ゆ崲鏂瑰紡銆備篃灏辨槸...
绛旓細鏈澶у尯鍒socket鏄綉缁滃眰锛http鏄簲鐢ㄦ垚銆傘俿ocket鏄竴涓▼搴忕粍浠讹紝瀹冩敮鎸乀CP,UDP绛夌綉缁滈氳鍗忚锛屼篃灏辨槸閫氳繃socket杩欎釜涓滆タ浣犲彲浠ュ拰浠讳綍浜掕仈缃戞垨灞鍩熺綉涓婄殑璁$畻鏈洪氳銆傘傘俆CP,UDP鏄竴涓綉缁滃眰鍗忚锛岀綉缁滃眰鍗忚涓嶇浣犲彂鐨勫唴瀹规槸鍟ワ紝浠栧彧璐熻矗鎶婁綘鎯虫硶鐨勪笢瑗垮彂鍒板闈紝鍙戠殑鏄暐锛屼粬瀹屽叏涓嶇锛屽洜涓轰粬涓嶆槸...
绛旓細Http璇锋眰涓昏鏈塰ttp鍗忚锛屽熀浜http鍗忚鐨soap鍗忚锛屽父瑙佺殑http鏁版嵁璇锋眰鏂瑰紡鏈塯et鍜宲ost锛寃eb鏈嶅姟銆備紭鐐癸細1.鍩轰簬搴旂敤绾х殑鎺ュ彛浣跨敤鏂逛究 2.瑕佹眰鐨勫紑鍙戞按骞充笉楂橈紝瀹归敊鎬у己 缂虹偣锛 1.浼犺緭閫熷害鎱紝鏁版嵁鍖呭ぇ銆2.濡傚疄鐜板疄鏃朵氦浜掞紝鏈嶅姟鍣ㄦц兘鍘嬪姏澶 3.鏁版嵁浼犺緭瀹夊叏鎬у樊 Socket閫傜敤鍦烘櫙锛氱綉缁滄父鎴忥紝閾惰浜や簰锛屾敮浠...
绛旓細Socket鍜孒TTP鐨涓昏鍖哄埆鍦ㄤ簬瀹冧滑鎵澶勭殑缃戠粶灞傛浠ュ強瀵规暟鎹鐞嗙殑鍏虫敞鐐广係ocket鏄竴绉嶅簳灞傜殑绋嬪簭缁勪欢锛屽畠鏀寔TCP銆乁DP绛夌綉缁滈氫俊鍗忚锛屼娇寰楀簲鐢ㄧ▼搴忚兘澶熶笌缃戠粶涓婄殑鍏朵粬璁惧杩涜閫氫俊銆傚畠涓昏璐熻矗鏁版嵁鐨勪紶杈擄紝涓嶅叧娉ㄥ彂閫佺殑鍏蜂綋鍐呭锛屽叾鍔熻兘灏卞儚涓涓閬擄紝灏嗘暟鎹粠涓绔紶杈撳埌鍙︿竴绔紝鑰屼笉绠℃暟鎹殑鏍煎紡鎴栧唴瀹规槸...
绛旓細鍦═CP/IP鍗忚缁勪腑锛孴CP鍜孶DP鍦ㄤ紶杈撳眰鍙戞尌浣滅敤锛岃HTTP鍗忚鍒欏埄鐢═CP鍦ㄦ祻瑙堝櫒涓庢湇鍔″櫒涔嬮棿浼犺緭瓒呮枃鏈俊鎭侶TTP涓虹煭杩炴帴锛屾瘡娆¤姹傞兘寤虹珛涓娆¤繛鎺ワ紝涓嶈繛缁椂浼氫富鍔ㄦ柇寮銆Socket鍗忚鎻愪緵浜嗕竴绉嶇紪绋嬫帴鍙o紝鐢ㄤ簬瀹炵幇缃戠粶閫氫俊銆傚畠閫氳繃绔彛鍙凤紙socket锛夋潵寤虹珛杩炴帴锛屼负TCP/IP鎻愪緵浜嗕竴灞傛洿鎶借薄鐨勬帴鍙c係ocket鍗忚鏄...
绛旓細WebSocket灏卞儚HTTP涓鏍凤紝鏄竴涓吀鍨嬬殑搴旂敤灞傚崗璁鎬荤粨锛歐ebSocket鏄疕TML5瑙勮寖鎻愬嚭鐨勪竴绉嶅崗璁侶TML5 Web Sockets瑙勮寖瀹氫箟浜哤eb Sockets API锛屾敮鎸侀〉闈娇鐢╓eb Socket鍗忚涓庤繙绋嬩富鏈鸿繘琛屽叏鍙屽伐鐨勯氫俊銆傚畠寮曞叆浜哤ebSocket鎺ュ彛骞朵笖瀹氫箟浜嗕竴涓叏鍙屽伐鐨勯氫俊閫氶亾锛岄氳繃涓涓崟涓鐨勫鎺ュ瓧鍦╓eb涓婅繘琛屾搷浣溿侶TML5 Web ...
绛旓細http銆乀CP/IP鍗忚涓巗ocket涔嬮棿鐨勫尯鍒 缃戠粶鐢变笅寰涓婂垎涓: 鐗╃悊灞-- 鏁版嵁閾捐矾灞-- 缃戠粶灞-- IP鍗忚 浼犺緭灞-- TCP鍗忚 浼氳瘽灞-- 琛ㄧず灞傚拰搴旂敤灞-- HTTP鍗忚 (1)TCP/IP杩炴帴 鎵嬫満鑳藉浣跨敤鑱旂綉鍔熻兘鏄洜涓烘墜鏈哄簳灞傚疄鐜颁簡TCP/IP鍗忚,鍙互浣挎墜鏈虹粓绔氳繃鏃犵嚎缃戠粶寤虹珛TCP杩炴帴銆俆CP鍗忚鍙互瀵逛笂灞傜綉缁滄彁渚涙帴鍙,浣夸笂...