{"id":3197,"date":"2011-08-10T11:11:09","date_gmt":"2011-08-10T03:11:09","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3197"},"modified":"2011-08-11T11:16:02","modified_gmt":"2011-08-11T03:16:02","slug":"%e5%9f%ba%e4%ba%8esocket-api%e7%9a%84cs%e9%80%9a%e4%bf%a1%ef%bc%9a%e4%bb%a5%e6%a0%91%e5%bd%a2%e7%bb%93%e6%9e%84%e6%98%be%e7%a4%ba%e6%9c%8d%e5%8a%a1%e5%99%a8%e7%ab%af%e7%9b%ae%e5%bd%95","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3197","title":{"rendered":"\u57fa\u4e8eSocket API\u7684C\/S\u901a\u4fe1\uff1a\u4ee5\u6811\u5f62\u7ed3\u6784\u663e\u793a\u670d\u52a1\u5668\u7aef\u76ee\u5f55(1)"},"content":{"rendered":"<p>\u5728\u57fa\u4e8esocket API\u7684C\/S\u6a21\u578b\u4e2d\uff0c\u6709\u65f6\u5019\u9700\u8981\u8fd9\u6837\u7684\u9700\u6c42\uff1a<\/p>\n<p><strong>1.\u5ba2\u6237\u7aef\u53ef\u4ee5\u5373\u65f6\u83b7\u53d6\u670d\u52a1\u5668\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u5217\u8868\u53ca\u5c5e\u6027\uff1b<\/strong><br \/>\n<strong> 2.\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u70b9\u51fb\u67d0\u4e2a\u5b50\u76ee\u5f55\uff0c\u53ef\u4ee5\u8fdb\u5165\u5e76\u83b7\u53d6\u8be5\u5b50\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u5217\u8868\u53ca\u5176\u5c5e\u6027\uff1b<\/strong><br \/>\n<strong> 3.\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u53ef\u4ee5\u8fd4\u56de\u5230\u4e0a\u4e00\u7ea7\u76ee\u5f55\uff1b<\/strong><\/p>\n<p>\u5982\u679cC\/S\u6a21\u578b\u57fa\u4e8eFTP\u534f\u8bae\uff0c\u90a3\u4e48\u8fd9\u4e2a\u9700\u6c42\u5c31\u5f88\u5bb9\u6613\u5b9e\u73b0\uff0c\u4e0d\u8fc7\u9700\u8981\u4e8b\u5148\u5c06\u670d\u52a1\u5668\u7aef\u7684\u4e3b\u673a\u8bbe\u7f6e\u4e3aFTP\u670d\u52a1\u5668\u3002\u5982\u679cC\/S\u6a21\u578b\u662f\u57fa\u4e8eTCP\/IP\u534f\u8bae\uff0c\u90a3\u4e48\u5b9e\u73b0\u8d77\u6765\u5c31\u6709\u4e00\u4e9b\u9ebb\u70e6\u3002<\/p>\n<p>\u672c\u6587\u6240\u63cf\u8ff0\u7684C\/S\u6a21\u578b\u5373\u57fa\u4e8eTCP\/IP\u534f\u8bae\uff0c\u670d\u52a1\u5668\u7a0b\u5e8f\u91c7\u7528Linux C\u7f16\u5199\uff0c\u8fd0\u884c\u5728Linux\u4e3b\u673a\u4e0a\uff1b\u800c\u5ba2\u6237\u7aef\u91c7\u7528Qt\u4ee5\u53ca\u539f\u751f\u7684Socket API\u7f16\u5199\uff0c\u4ee5\u53cb\u597d\u7684\u4ea4\u4e92\u754c\u9762\u8fd0\u884c\u5728Windows\u6216LInux\u4e3b\u673a\u4e0a\u3002<\/p>\n<h4>1.\u670d\u52a1\u5668\u7a0b\u5e8f\u5b9e\u73b0\u6982\u8ff0<\/h4>\n<p>\u6982\u62ec\u7684\u8bf4\uff0c\u670d\u52a1\u5668\u548c\u5ba2\u6237\u7aef\u4e4b\u95f4\u7684\u901a\u4fe1\u603b\u662f\u201c\u5ba2\u6237\u7aef\u53d1\u9001\u8bf7\u6c42&#8211;\u670d\u52a1\u5668\u63a5\u53d7\u8bf7\u6c42\u5e76\u53d1\u9001\u6570\u636e&#8211;\u5ba2\u6237\u7aef\u63a5\u53d7\u6570\u636e\u5e76\u89e3\u6790\u201d\u8fd9\u6837\u7684\u5faa\u73af\u8fc7\u7a0b\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u670d\u52a1\u5668\u9700\u8981\u4e00\u76f4\u76d1\u542c\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\uff0c\u6839\u636e\u53d1\u9001\u6570\u636e\u7684\u4e0d\u540c\u670d\u52a1\u5668\u4f5c\u51fa\u76f8\u5e94\u7684\u64cd\u4f5c\u3002\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u5747\u4e3a\u5b57\u8282\u6d41\uff0c\u4f46\u662f\u6839\u636e\u5176\u529f\u80fd\u4e0d\u540c\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u5b57\u8282\u6d41\u5206\u4e3a\u4e24\u79cd\uff1a\u7b2c\u4e00\u79cd\u4e3a\u6307\u4ee4\u578b\u6570\u636e\uff0c\u8fd9\u4e9b\u6570\u636e\u5e76\u4e0d\u662f\u5ba2\u6237\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u771f\u6b63\u4ea4\u4e92\u7684\u6570\u636e\uff0c\u800c\u662f\u5ba2\u6237\u7aef\u6307\u5b9a\u670d\u52a1\u5668\u505a\u51fa\u76f8\u5e94\u64cd\u4f5c\u7684\u547d\u4ee4\uff1b\u7b2c\u4e8c\u79cd\u4e3a\u8bf7\u6c42\u6570\u636e\uff0c\u8fd9\u7c7b\u6570\u636e\u624d\u662f\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u7528\u6765\u8fdb\u884c\u6570\u636e\u4ea4\u4e92\u7684\u3002<\/p>\n<p>\u6839\u636e\u4e0a\u9762\u7684\u7406\u89e3\uff0c\u670d\u52a1\u5668\u548c\u5ba2\u6237\u4e4b\u95f4\u7684\u4ea4\u4e92\u6a21\u578b\u5927\u81f4\u662f\u8fd9\u6837\u7684\uff1a\u670d\u52a1\u5668\u548c\u5ba2\u6237\u8fde\u63a5\u6210\u529f\u540e\uff0c\u670d\u52a1\u5668\u9996\u5148\u5c06\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u540d\u53ca\u6587\u4ef6\u5c5e\u6027\u53d1\u9001\u81f3\u5ba2\u6237\u7aef\uff0c\u5ba2\u6237\u7aef\u8fdb\u884c\u89e3\u6790\u751f\u6210\u6587\u4ef6\u5217\u8868\uff1b\u5f53\u7528\u6237\u5728\u5ba2\u6237\u7aef\u70b9\u51fb\u76f8\u5e94\u5b50\u76ee\u5f55\uff08\u6216\u4e0a\u7ea7\u76ee\u5f55\u540d\uff09\u540e\uff0c\u5ba2\u6237\u7aef\u7ed9\u670d\u52a1\u5668\u53d1\u9001\u8bfb\u53d6\u5b50\u76ee\u5f55\uff08\u6216\u4e0a\u7ea7\u76ee\u5f55\uff09\u7684\u6307\u4ee4\uff1b\u670d\u52a1\u5668\u6536\u5230\u8be5\u547d\u4ee4\u540e\uff0c\u8bfb\u53d6\u76f8\u5e94\u5b50\u76ee\u5f55\uff08\u6216\u4e0a\u7ea7\u76ee\u5f55\uff09\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5e76\u53d1\u9001\u81f3\u5ba2\u6237\u7aef\u3002<\/p>\n<p>\u5728\u4e0a\u8ff0\u8fc7\u7a0b\u4e2d\uff0c\u670d\u52a1\u5668\u5bf9\u5e94\u7684\u4ee3\u7801\u6a21\u578b\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">int server_dir(int client_fd)\r\n{\r\n\tsendDir(client_fd, \"\/home\/edsionte\/\");\r\n\r\n\twhile (1) {\r\n\t\tmemset(recvbuf, 0, CONFIGSTRUCT_LENGTH);\r\n          \tif (recv(client_fd, recvbuf, CONFIGSTRUCT_LENGTH, 0) &lt; 0) {\r\n\t          \tmy_error(\"recv\", errno, __LINE__);\r\n           \t}\r\n\r\n\t\tif (strcmp(recvbuf, \"command1\") == 0) {\r\n\t\t\tdo_command1();\r\n\t\t} else if (strcmp(recvbuf, \"cd\") == 0) {\r\n\t\t\tsendNextDir(client_fd);\r\n\t\t} else if (strcmp(recvbuf, \"command2\") == 0) {\r\n\t\t\tdo_command2();\r\n\t\t}\r\n                \u2026\u2026\u2026\u2026\r\n                else {\r\n\t\t\tdo_anotherthing();\r\n\t\t}\r\n\r\n\treturn 1;\r\n}<\/pre>\n<p>\u6bd4\u5982\u5ba2\u6237\u7aef\u8bf7\u6c42\u83b7\u5f97\/home\/edsionte\/\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5217\u8868\uff0c\u90a3\u4e48\u4e00\u5f00\u59cb\u5148\u901a\u8fc7sendDir\u51fd\u6570\u83b7\u5f97\u8be5\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u3002\u63a5\u7740\u670d\u52a1\u5668\u901a\u8fc7\u8fdb\u5165\u4e00\u4e2a\u6b7b\u5faa\u73af\u800c\u5b9e\u73b0\u65e0\u9650\u76d1\u542c\u5ba2\u6237\u7aef\u5230\u529f\u80fd\uff0c\u670d\u52a1\u5668\u6839\u636e\u4ece\u5ba2\u6237\u7aef\u63a5\u53d7\u7684\u4e0d\u540c\u201c\u6307\u4ee4\u201d\u4ece\u800c\u505a\u51fa\u76f8\u5e94\u5904\u7406\u3002\u6bd4\u5982\u5f53\u670d\u52a1\u5668\u68c0\u6d4b\u5230\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u4e3a\u201ccd\u201d\u65f6\uff0c\u5219\u8fdb\u5165sendNextDir\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u5373\u83b7\u5f97\/home\/edsionte\/\u4e0b\u67d0\u4e2a\u5b50\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u3002<\/p>\n<h4>2.\u670d\u52a1\u5668\u7684\u5177\u4f53\u5b9e\u73b0<\/h4>\n<p>\u5728\u5b9e\u73b0\u670d\u52a1\u5668\u7684\u8fc7\u7a0b\u4e2d\u4e3b\u8981\u6d89\u53ca\u4e09\u4e2a\u51fd\u6570\uff0c\u4e0b\u9762\u5c06\u4f9d\u6b21\u5bf9\u6240\u6d89\u53ca\u7684\u51fd\u6570\u8fdb\u884c\u8bf4\u660e\u3002<\/p>\n<p>readDir()\u8bfb\u53d6pathname\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\uff0c\u5c06\u6587\u4ef6\u4ee5\u5176\u76f8\u5173\u5c5e\u6027\u4fdd\u5b58\u5230dirbuf\u4e2d\uff0c\u5c06\u8be5\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u6570\u4fdd\u5b58\u5230num\u4e2d\u3002\u8be5\u51fd\u6570\u5177\u4f53\u7684\u5b9e\u73b0\u8fc7\u7a0b\u5bb9\u6613\u7406\u89e3\uff1a\u5148\u6253\u5f00pathname\u76ee\u5f55\uff0c\u518d\u5229\u7528\u5faa\u73af\u4f9d\u6b21\u8bfb\u53d6\u8be5\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u53ca\u76f8\u5e94\u7684\u6587\u4ef6\u5c5e\u6027\u3002\u5e76\u4e14\u5728\u8bfb\u53d6\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f9d\u6b21\u5c06\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u5c5e\u6027\u6309\u7167\u4e00\u5b9a\u7684\u683c\u5f0f\u62fc\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u6d41\u3002<\/p>\n<pre class=\"brush:c\">\r\nint readDir(char *pathname, char *dirbuf)\r\n{\r\n\tDIR *mydir;\r\n\tstruct dirent *myentry;\r\n\tstruct stat mystat;\r\n\tchar filepath[DATA_MAX];\r\n\tint num = 1;\r\n\r\n\tif ((mydir = opendir(pathname)) == NULL) {\r\n\t\tmy_error(\"opendir\", errno, __LINE__);\r\n\t}\r\n\r\n\tif (pathname[strlen(pathname) - 1] != '\/')\r\n\t\tpathname[strlen(pathname)] = '\/';\r\n\tstrcpy(dirbuf, pathname);\r\n\tstrcat(dirbuf, \"\\n\");\r\n\tstrcat(dirbuf, \"..:1\\n\");\r\n\r\n\twhile ((myentry = readdir(mydir)) != NULL) {\t\r\n\t\tif ((strcmp(myentry->d_name, \".\") == 0) || (strcmp(myentry->d_name, \"..\") == 0))\r\n\t\t\tcontinue;\r\n\t\tmemset(filepath, 0, DATA_MAX);\r\n        \tstrcpy(filepath, pathname);\r\n\t\tstrcat(dirbuf, myentry->d_name);\r\n\r\n\t\tstrcat(filepath, myentry->d_name);\r\n\t\tif (lstat(filepath, &mystat) == -1) {\r\n\t\t\tmy_error(\"stat\", errno, __LINE__);\r\n\t\t}\r\n\t\r\n\t\tif (S_ISDIR(mystat.st_mode)) {\r\n\t\t\tstrcat(dirbuf, \":1\");\r\n\t\t} else {\r\n\t\t\tstrcat(dirbuf, \":0\");\r\n\t\t}\r\n\r\n\t\tstrcat(dirbuf, \"\\n\");\r\n\t\tnum++;\r\n\t}\/\/while\r\n\r\n\treturn num;\r\n}\r\n<\/pre>\n<p>sendDir()\u5148\u5f00\u8f9f\u4e00\u5b9a\u5927\u5c0f\u7684\u5185\u5b58\u7a7a\u95f4dirbuf\uff0c\u518d\u901a\u8fc7readDir()\u83b7\u5f97\u76f8\u5e94\u5b57\u8282\u6d41\uff1b\u518d\u901a\u8fc7\u5ba2\u6237\u7aef\u5957\u63a5\u5b57client_fd\uff0c\u5c06pathname\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6\u6570\u548c\u8be5\u76ee\u5f55\u4e0b\u7684\u6570\u636e\u5b57\u8282\u6d41\u53d1\u9001\u81f3\u5ba2\u6237\u7aef\u3002<\/p>\n<pre class=\"brush:c\">void sendDir(int client_fd, char *pathname)\r\n{\r\n\tint num;\r\n\r\n\tchar *dirbuf = NULL;\r\n\tif ((dirbuf = (char *)malloc(DATA_MAX * 10)) == NULL) {\r\n\t\tmy_error(\"malloc\", errno, __LINE__);\r\n\t}\r\n\tmemset(dirbuf, 0, DATA_MAX * 10);\r\n\tnum = readDir(pathname, dirbuf);\r\n\tif (send(client_fd, &amp;num, sizeof(num), 0) == -1) {\r\n\t\tmy_error(\"send\", errno, __LINE__);\r\n\t}\r\n\r\n\tif (send(client_fd, dirbuf, strlen(dirbuf), 0) == -1) {\r\n\t\tmy_error(\"send\", errno, __LINE__);\r\n\t}\r\n\tfree(dirbuf);\r\n}<\/pre>\n<p>\u5f53\u670d\u52a1\u5668\u63a5\u6536\u5230\u5ba2\u6237\u7aef\u201c\u8fdb\u5165\u4e0b\u7ea7\u76ee\u5f55\u201d\u7684\u547d\u4ee4\uff08\u7a0b\u5e8f\u4e2d\u7528\u5b57\u8282\u6d41\u201ccd\u201d\u8868\u793a\uff09\u65f6\u5c31\u6389\u7528sendNextDir()\u3002\u8be5\u51fd\u6570\u5148\u63a5\u6536\u5ba2\u6237\u7aef\u53d1\u9001\u6765\u7684\u4e0b\u7ea7\u76ee\u5f55\u7684\u5b8c\u6574\u8def\u5f84\uff0c\u518d\u8c03\u7528sendDir()\u3002<\/p>\n<pre class=\"brush:c\">void sendNextDir(int client_fd)\r\n{\r\n\tchar pathname[DATA_MAX];\r\n\tmemset(pathname, 0, DATA_MAX);\r\n\tif (recv(client_fd, pathname, DATA_MAX, 0) == -1) {\r\n\t\tmy_error(\"recv\", errno, __LINE__);\r\n\t}\r\n\r\n\tsendDir(client_fd, pathname);\r\n}<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u56db\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u57fa\u672c\u5b9e\u73b0\u672c\u6587\u4e00\u5f00\u59cb\u63d0\u51fa\u7684\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u57fa\u4e8esocket API\u7684C\/S\u6a21\u578b\u4e2d\uff0c\u6709\u65f6\u5019\u9700\u8981\u8fd9\u6837\u7684\u9700\u6c42\uff1a 1.\u5ba2\u6237\u7aef\u53ef\u4ee5\u5373\u65f6\u83b7\u53d6\u670d\u52a1\u5668\u6307\u5b9a\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[208],"tags":[241,220,200,209],"class_list":["post-3197","post","type-post","status-publish","format-standard","hentry","category-208","tag-api","tag-cs","tag-qt","tag-socket"],"views":6376,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=3197"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3197\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3197"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}