{"id":127048,"date":"2020-05-29T16:59:07","date_gmt":"2020-05-29T08:59:07","guid":{"rendered":"http:\/\/4563.org\/?p=127048"},"modified":"2020-05-29T16:59:07","modified_gmt":"2020-05-29T08:59:07","slug":"%e5%88%9a%e5%a5%bd%e5%b7%a5%e4%bd%9c%e4%b8%ad%e9%81%87%e5%88%b0%e7%9a%84-socket-%e9%97%ae%e9%a2%98%e6%83%b3%e9%97%ae%e4%b8%80%e4%b8%8b%e5%a4%a7%e5%ae%b6","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=127048","title":{"rendered":"\u521a\u597d\u5de5\u4f5c\u4e2d\u9047\u5230\u7684 socket \u95ee\u9898\u60f3\u95ee\u4e00\u4e0b\u5927\u5bb6"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u521a\u597d\u5de5\u4f5c\u4e2d\u9047\u5230\u7684 socket \u95ee\u9898\u60f3\u95ee\u4e00\u4e0b\u5927\u5bb6               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <span><i><\/i> 15<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<h1>server \u7aef\u7684\u4ee3\u7801<\/h1>\n<p> <\/p>\n<pre><code>import socket  server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) addr = (\"0.0.0.0\", 9000) server.bind(addr) server.listen(5)   while 1:     print(\"waiting for connection\")     client, address = server.accept()     print(\"connection from {}:{}\".format(address[0], address[1]))      while 1:         data = client.recv(1024)         if data == b\"\":             break         print(data)     client.close()     print(\"close connection from {}:{}\".format(address[0], address[1])) <\/code><\/pre>\n<h1>client \u7684\u4ee3\u7801<\/h1>\n<p> <\/p>\n<pre><code>import socket  addr = (\"localhost\", 9000) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.connect(addr) client.close() client.connect(addr)  <\/code><\/pre>\n<h3>\u8fd0\u884c\u7684\u65f6\u5019\u670d\u52a1\u7aef\u6ca1\u6709\u5565\u95ee\u9898\uff0c \u4f46\u662f\u5728\u5ba2\u6237\u7aef\u7684\u65f6\u5019\uff0c \u6211\u5148\u5173\u95ed\u518d\u8fde\u63a5\u4e5f\u5c31\u662f client \u4e2d\u7684\u6700\u540e\u4e00\u884c\u4f1a\u62a5\u4e00\u4e2a\u9519\uff1aOSError: [WinError 10038] \u5728\u4e00\u4e2a\u975e\u5957\u63a5\u5b57\u4e0a\u5c1d\u8bd5\u4e86\u4e00\u4e2a\u64cd\u4f5c\uff0c \u6211\u4e00\u76f4\u4e0d\u61c2\u4e3a\u4ec0\u4e48\u4f1a\u62a5\u8fd9\u4e2a\u9519\uff0c \u6709\u670b\u53cb\u5e2e\u6211\u89e3\u91ca\u4e00\u4e0b\u4e48<\/h3>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"2056490\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : opengps <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u4e0d\u61c2 py\uff0c\u4f46\u6211\u7406\u89e3 socket \u8fde\u63a5\u4e0d\u80fd\u8fd9\u4e48\u590d\u7528\uff08\u76f4\u63a5\u65ad\u76f4\u63a5\u91cd\u5f00\uff09\u3002<br \/>\u5728 c#\u4e0b\u5f97\u5148=null \u7136\u540e\u91cd\u65b0 new \u624d\u80fd\u4f7f\u7528\u65b0\u7684\u5bf9\u8c61\u5efa\u7acb\u8fde\u63a5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056491\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : limboMu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             TCP \u4e3b\u52a8\u5173\u95ed\u4e00\u65b9\uff0c\u662f\u8981\u7b49\u5f85 2msl \u624d\u80fd\u590d\u7528\u7aef\u53e3\u7684\uff0c\u91cd\u65b0\u8fde\u63a5\u591a\u534a\u662f\u8981\u6362\u4e00\u4e2a\u7aef\u53e3\uff0cso \u539f\u6765\u7684 socket \u5b9e\u4f8b\u662f\u4e0d\u80fd\u4f7f\u7528\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056492\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ysc3839 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u8981\u91cd\u65b0\u8c03\u7528 socket.socket \u521b\u5efa\u4e2a\u65b0\u7684\u5427\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056493\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @limboMu <br \/>\u7b49 2msl \u662f\u4ec0\u4e48\u610f\u601d\uff0c \u7b49 2 \u6beb\u79d2\u4e48<br \/>client.connect(addr)\u4e4b\u524d\u518d\u91cd\u65b0\u8d4b\u503c\u4e0b client<br \/>client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>\u8fd9\u6837\u5c31\u4e0d\u4f1a\u62a5\u9519                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056494\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ysc3839 \u7684\u786e\u662f\u9700\u8981\u5efa\u7acb\u4e00\u4e2a\u65b0\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056495\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : est <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             client.connect(addr)<\/p>\n<p>\u8fd9\u4e2a\u4e0d\u80fd\u91cd\u590d\u5199\u3002\u5f97\u518d\u5f04\u4e00\u4e2a client2                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056496\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : limboMu <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @18870715400 2msl \u662f\u4e2a\u865a\u6570\uff0c\u5f88\u957f\u53ef\u80fd\u534a\u5206\u949f\u4e5f\u53ef\u80fd\u4e00\u5206\u949f\uff0c\u8fd9\u4e2a\u53d6\u51b3\u4e8e\u64cd\u4f5c\u7cfb\u7edf\u7684\u7f51\u7edc\u534f\u8bae\u6808\u7684\u5b9e\u73b0\uff0c\u5efa\u8bae\u4e86\u89e3\u4e00\u4e0b TCP \u7684\u72b6\u6001\u673a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056497\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lackywind <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6700\u540e\u4e00\u4e2a client.connect(addr) \u5e94\u8be5\u662f\u628a\u5957\u63a5\u5b57\u7684\u53e5\u67c4\u7ed9\u5173\u95ed\u4e86\uff0c\u9700\u8981\u518d\u91cd\u65b0\u521b\u5efa\u4e0b<\/p>\n<p>import socket<\/p>\n<p>addr = (&#8220;localhost&#8221;, 9000)<br \/>client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>client.connect(addr)<br \/>client.close()<br \/>client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>client.connect(addr)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056498\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sujin190 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             socket \u521b\u5efa\u7684\u4e5f\u662f\u6587\u4ef6\u53e5\u67c4\uff0c\u4f60\u90fd\u5173\u95ed\u4e86\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6587\u4ef6\u53e5\u67c4\u5728\u5185\u6838\u91cc\u5c31\u76f4\u63a5\u88ab\u91ca\u653e\u4e86\u554a\uff0c\u4f60\u5728\u64cd\u4f5c\u4ed6\u5f53\u7136\u4e0d\u884c\u4e86\uff0c\u5c31\u597d\u6bd4\u4f60\u4e00\u4e2a\u6587\u4ef6\u6253\u5f00\u7136\u540e\u5173\u95ed\u518d\u8bfb\u5199\u5f53\u7136\u4e0d\u884c\u4e86<\/p>\n<p>\u7b2c\u4e8c\u4e2a\u4e5f\u662f\u4e0a\u9762\u8bf4\u7684\uff0ctcp \u4e3a\u4e86\u8fc7\u6ee4\u5f02\u5e38\u5305\u6240\u4ee5\u5173\u95ed\u540e\u4f1a\u7ee7\u7eed\u4fdd\u6301\u5360\u7528\u4e00\u6bb5\u65f6\u95f4\u7aef\u53e3\uff0c\u7acb\u523b\u91cd\u7528\u8fd9\u4e2a\u7aef\u53e3\u4e5f\u662f\u4e0d\u884c\u7684                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056499\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : HanMeiM <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             2msl \u662f\u6307\u4e24\u500d\u7684\u62a5\u6587\u6700\u957f\u751f\u5b58\u65f6\u95f4\uff0c\u4fdd\u8bc1\u88ab\u5173\u95ed\u7684\u4e00\u65b9\u786e\u5b9a\u6536\u5230 ack \u548c\u65e7\u6570\u636e\u7684\u6e05\u9664                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056500\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : youngce <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u521a\u597d\u4e4b\u524d\u505a\u8fc7\u7c7b\u4f3c tcp \u65ad\u7ebf\uff08\u5b9e\u9645\u4e0a\u53ef\u80fd\u662f\u670d\u52a1\u7aef\u6682\u65f6\u4e0d\u53ef\u7528\uff09\u91cd\u8fde\u7684\u673a\u5236\uff0c\u4e00\u822c\u662f client.close()\u4e3b\u52a8 sleep \u4e00\u6bb5\u65f6\u95f4\uff0c\u7136\u540e\u518d\u91cd\u65b0\u53d1\u8d77 client.connect(addr)\uff0c\u8fd9\u6837\u5c31\u907f\u514d\u4e86\u4f60\u8fd9\u91cc\u7684\u62a5\u9519                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056501\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yannxia <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Socket \u88ab close \u5c31\u7ea6\u7b49\u4e8e FD \u88ab lose \u7ea6\u7b49\u4e8e \u6b64\u6b21\u7684 TCP \u94fe\u8def\u7ed3\u675f\u4e86\uff0c\u518d Connect \u9700\u8981\u4e00\u4e2a \u65b0\u7684 FD\uff0c\u4e5f\u5c31\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Socket                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"2056502\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : 18870715400 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @sujin190 \u770b\u5230\u4f60\u7684\u4f8b\u5b50\u7a81\u7136\u61c2\u4e86\uff0c \u8c22\u8c22                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u521a\u597d\u5de5\u4f5c\u4e2d\u9047\u5230\u7684 socket \u95ee&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/127048"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127048"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/127048\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127048"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127048"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127048"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}