{"id":29183,"date":"2023-09-17T04:55:44","date_gmt":"2023-09-16T20:55:44","guid":{"rendered":"https:\/\/www.techphant.cn\/?p=29183"},"modified":"2023-10-14T16:54:25","modified_gmt":"2023-10-14T08:54:25","slug":"cjthuds","status":"publish","type":"post","link":"https:\/\/www.techphant.cn\/blog\/29183.html","title":{"rendered":"\u521b\u5efaTCP\u548cUDP\u7684Socket"},"content":{"rendered":"
\u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0cSocket\u662f\u4e00\u79cd\u901a\u4fe1\u63a5\u53e3\uff0c\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002<\/strong>Socket\u53ef\u4ee5\u5206\u4e3aTCP Socket\u548cUDP Socket\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u4f20\u8f93\u6570\u636e\u7684\u65b9\u5f0f\u4e0d\u540c\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u521b\u5efaTCP Socket\u548cUDP Socket\uff0c\u4ee5\u53ca\u5b83\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u3002<\/p>\n TCP Socket\u662f\u4e00\u79cd\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528TCP\u534f\u8bae\u6765\u4f20\u8f93\u6570\u636e\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u53ef\u9760\u6027\u3002TCP Socket\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5efa\u7acb\u53ef\u9760\u7684\u8fde\u63a5\uff0c\u5e76\u4e14\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u53ef\u9760\u6027\u3002<\/p>\n \u8981\u521b\u5efaTCP Socket\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2aSocket\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528Socket\u5bf9\u8c61\u7684connect()\u65b9\u6cd5\uff0c\u4f20\u5165\u670d\u52a1\u5668\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\uff0c\u5373\u53ef\u5efa\u7acbTCP\u8fde\u63a5\u3002<\/p>\n s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<\/p> s.connect(('127.0.0.1', 80))<\/p><\/code><\/pre>\n \u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aTCP Socket\uff0c\u5e76\u4e14\u8fde\u63a5\u5230\u4e86\u672c\u673a\u7684IP\u5730\u5740127.0.0.1\uff0c\u7aef\u53e3\u53f7\u4e3a80\u3002<\/p>\n UDP Socket\u662f\u4e00\u79cd\u4e0d\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528UDP\u534f\u8bae\u6765\u4f20\u8f93\u6570\u636e\uff0c\u4e0d\u80fd\u786e\u4fdd\u6570\u636e\u7684\u53ef\u9760\u6027\u3002UDP Socket\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u5efa\u7acb\u4e0d\u53ef\u9760\u7684\u8fde\u63a5\uff0c\u4f46\u662f\u4e0d\u80fd\u4fdd\u8bc1\u6570\u636e\u7684\u53ef\u9760\u6027\u3002<\/p>\n \u8981\u521b\u5efaUDP Socket\uff0c\u9996\u5148\u9700\u8981\u521b\u5efa\u4e00\u4e2aSocket\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528Socket\u5bf9\u8c61\u7684bind()\u65b9\u6cd5\uff0c\u4f20\u5165\u672c\u5730\u7684IP\u5730\u5740\u548c\u7aef\u53e3\u53f7\uff0c\u5373\u53ef\u5efa\u7acbUDP\u8fde\u63a5\u3002<\/p>\n s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)<\/p> s.bind(('127.0.0.1', 80))<\/p><\/code><\/pre>\n \u4e0a\u9762\u7684\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2aUDP Socket\uff0c\u5e76\u4e14\u7ed1\u5b9a\u5230\u4e86\u672c\u673a\u7684IP\u5730\u5740127.0.0.1\uff0c\u7aef\u53e3\u53f7\u4e3a80\u3002<\/p>\n \u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0cSocket\u662f\u4e00\u79cd\u901a\u4fe1\u63a5\u53e3\uff0c\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002<\/strong>Socket\u53ef\u4ee5\u5206\u4e3aTCP Socket\u548cUDP Socket\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\u4f20\u8f93\u6570\u636e\u7684\u65b9\u5f0f\u4e0d\u540c\u3002TCP Socket\u662f\u4e00\u79cd\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528TCP\u534f\u8bae\u6765\u4f20\u8f93\u6570\u636e\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u53ef\u9760\u6027\uff1bUDP Socket\u662f\u4e00\u79cd\u4e0d\u53ef\u9760\u7684\u6570\u636e\u4f20\u8f93\u65b9\u5f0f\uff0c\u5b83\u4f7f\u7528UDP\u534f\u8bae\u6765\u4f20\u8f93\u6570\u636e\uff0c\u4e0d\u80fd\u786e\u4fdd\u6570\u636e\u7684\u53ef\u9760\u6027\u3002\u8981\u521b\u5efaTCP Socket\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aSocket\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528Socket\u5bf9\u8c61\u7684connect()\u65b9\u6cd5\uff1b\u8981\u521b\u5efaUDP Socket\uff0c\u9700\u8981\u521b\u5efa\u4e00\u4e2aSocket\u5bf9\u8c61\uff0c\u7136\u540e\u8c03\u7528Socket\u5bf9\u8c61\u7684bind()\u65b9\u6cd5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":" \u5728\u7f51\u7edc\u901a\u4fe1\u4e2d\uff0cSocket\u662f\u4e00\u79cd\u901a\u4fe1\u63a5\u53e3\uff0c\u53ef\u4ee5\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002Socket\u53ef\u4ee5\u5206\u4e3aTCP Socket\u548cUDP Socke<\/p>\n","protected":false},"author":1,"featured_media":3671,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[36],"tags":[323,52],"class_list":["post-29183","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-udp","tag-wxtx"],"_links":{"self":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/29183","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/comments?post=29183"}],"version-history":[{"count":0,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/posts\/29183\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media\/3671"}],"wp:attachment":[{"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/media?parent=29183"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/categories?post=29183"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techphant.cn\/wp-json\/wp\/v2\/tags?post=29183"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}TCP Socket<\/h2>\n
\u521b\u5efaTCP Socket<\/h3>\n
\u793a\u4f8b\u4ee3\u7801<\/h4>\n
import socket
UDP Socket<\/h2>\n
\u521b\u5efaUDP Socket<\/h3>\n
\u793a\u4f8b\u4ee3\u7801<\/h4>\n
import socket
\u603b\u7ed3<\/h2>\n