\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u8be6\u7ec6\u4ecb\u7ecd\u4e86Golang\u5b9e\u73b0\u6587\u4ef6\u4f20\u8f93\u529f\u80fd\uff0c\u6587\u4e2d\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u611f\u5174\u8da3\u7684\u5c0f\u4f19\u4f34\u4eec\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86Golang\u5b9e\u73b0\u6587\u4ef6\u4f20\u8f93\u7684\u5177\u4f53\u4ee3\u7801\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b<\/p>\n
\u501f\u52a9TCP\u5b8c\u6210\u6587\u4ef6\u7684\u4f20\u8f93\uff0c\u57fa\u672c\u601d\u8def\u5982\u4e0b\uff1a<\/p>\n
1\u3001\u53d1\u9001\u65b9\uff08\u5ba2\u6237\u7aef\uff09\u5411\u670d\u52a1\u7aef\u53d1\u9001\u6587\u4ef6\u540d\uff0c\u670d\u52a1\u7aef\u4fdd\u5b58\u8be5\u6587\u4ef6\u540d\u3002<\/p>\n
2\u3001\u63a5\u6536\u65b9\uff08\u670d\u52a1\u7aef\uff09\u5411\u5ba2\u6237\u7aef\u8fd4\u56de\u4e00\u4e2a\u6d88\u606fok\uff0c\u786e\u8ba4\u6587\u4ef6\u540d\u4fdd\u5b58\u6210\u529f\u3002<\/p>\n
3\u3001\u53d1\u9001\u65b9\uff08\u5ba2\u6237\u7aef\uff09\u6536\u5230\u6d88\u606f\u540e\uff0c\u5f00\u59cb\u5411\u670d\u52a1\u7aef\u53d1\u9001\u6587\u4ef6\u6570\u636e\u3002<\/p>\n
4\u3001\u63a5\u6536\u65b9\uff08\u670d\u52a1\u7aef\uff09\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5199\u5165\u5230\u4e4b\u524d\u4fdd\u5b58\u597d\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n
<\/p>\n
\u9996\u5148\u83b7\u53d6\u6587\u4ef6\u540d\u3002\u501f\u52a9os\u5305\u4e2d\u7684stat()\u51fd\u6570\u6765\u83b7\u53d6\u6587\u4ef6\u5c5e\u6027\u4fe1\u606f\u3002\u5728\u51fd\u6570\u8fd4\u56de\u7684\u6587\u4ef6\u5c5e\u6027\u4e2d\u5305\u542b\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u5927\u5c0f\u3002Stat\u53c2\u6570name\u4f20\u5165\u7684\u662f\u6587\u4ef6\u8bbf\u95ee\u7684\u7edd\u5bf9\u8def\u5f84\u3002FileInfo\u4e2d\u7684Name()\u51fd\u6570\u53ef\u4ee5\u5c06\u6587\u4ef6\u540d\u5355\u72ec\u63d0\u53d6\u51fa\u6765\u3002<\/p>\n
func Stat(name string) (FileInfo, error) \r\ntype FileInfo interface {\r\n Name() string \r\n Size() int64 \r\n Mode() FileMode \r\n ModTime() time.Time \r\n IsDir() bool \r\n Sys() interface{} \r\n}<\/pre>\n\u53d1\u9001\u7aef\uff1a<\/p>\n
package main\r\n \r\nimport (\r\n \"fmt\"\r\n \"io\"\r\n \"net\"\r\n \"os\"\r\n)\r\n \r\nfunc sendFile(conn net.Conn, filePath string) {\r\n \/\/ \u53ea\u8bfb\u6253\u5f00\u6587\u4ef6\r\n f, err := os.Open(filePath)\r\n if err != nil {\r\n fmt.Println(\"os.Open err:\", err)\r\n return\r\n }\r\n defer f.Close()\r\n \r\n \/\/ \u4ece\u672c\u6587\u4ef6\u4e2d\uff0c\u8bfb\u6570\u636e\uff0c\u5199\u7ed9\u7f51\u7edc\u63a5\u6536\u7aef\u3002 \u8bfb\u591a\u5c11\uff0c\u5199\u591a\u5c11\u3002\u539f\u5c01\u4e0d\u52a8\u3002\r\n buf := make([]byte, 1024)\r\n for {\r\n n, err := f.Read(buf)\r\n if err != nil {\r\n if err == io.EOF {\r\n fmt.Println(\"\u53d1\u9001\u6587\u4ef6\u5b8c\u6210\u3002\")\r\n } else {\r\n fmt.Println(\"os.Open err:\", err)\r\n }\r\n return\r\n }\r\n \/\/ \u5199\u5230\u7f51\u7edcsocket\u4e2d\r\n _, err = conn.Write(buf[:n])\r\n if err != nil {\r\n fmt.Println(\"conn.Write err:\", err)\r\n return\r\n }\r\n }\r\n}\r\n \r\nfunc main() {\r\n list := os.Args \/\/ \u83b7\u53d6\u547d\u4ee4\u884c\u53c2\u6570\r\n \r\n if len(list) != 2 {\r\n fmt.Println(\"\u683c\u5f0f\u4e3a\uff1ago run xxx.go \u6587\u4ef6\u7edd\u5bf9\u8def\u5f84\")\r\n return\r\n }\r\n \/\/ \u63d0\u53d6 \u6587\u4ef6\u7684\u7edd\u5bf9\u8def\u5f84\r\n filePath := list[1]\r\n \r\n \/\/\u63d0\u53d6\u6587\u4ef6\u540d\r\n fileInfo, err := os.Stat(filePath)\r\n if err != nil {\r\n fmt.Println(\"os.Stat err:\", err)\r\n return\r\n }\r\n fileName := fileInfo.Name()\r\n \r\n \/\/ \u4e3b\u52a8\u53d1\u8d77\u8fde\u63a5\u8bf7\u6c42\r\n conn, err := net.Dial(\"tcp\", \"127.0.0.1:8000\")\r\n if err != nil {\r\n fmt.Println(\"net.Dial err:\", err)\r\n return\r\n }\r\n defer conn.Close()\r\n \r\n \/\/ \u53d1\u9001\u6587\u4ef6\u540d\u7ed9 \u63a5\u6536\u7aef\r\n _, err = conn.Write([]byte(fileName))\r\n if err != nil {\r\n fmt.Println(\"conn.Write err:\", err)\r\n return\r\n }\r\n \/\/ \u8bfb\u53d6\u670d\u52a1\u5668\u56de\u53d1\u7684 OK\r\n buf := make([]byte, 1024)\r\n n, err := conn.Read(buf)\r\n if err != nil {\r\n fmt.Println(\"conn.Read err:\", err)\r\n return\r\n }\r\n \r\n if \"ok\" == string(buf[:n]) {\r\n \/\/ \u5199\u6587\u4ef6\u5185\u5bb9\u7ed9\u670d\u52a1\u5668\u2014\u2014\u501f\u52a9conn\r\n sendFile(conn, filePath)\r\n }\r\n}<\/pre>\n\u63a5\u6536\u7aef\uff1a<\/p>\n
\r\npackage main\r\n \r\nimport (\r\n \"fmt\"\r\n \"net\"\r\n \"os\"\r\n)\r\n \r\nfunc recvFile(conn net.Conn, fileName string) {\r\n \/\/ \u6309\u7167\u6587\u4ef6\u540d\u521b\u5efa\u65b0\u6587\u4ef6\r\n f, err := os.Create(fileName)\r\n if err != nil {\r\n fmt.Println(\"os.Create err:\", err)\r\n return\r\n }\r\n defer f.Close()\r\n \r\n \/\/ \u4ece \u7f51\u7edc\u4e2d\u8bfb\u6570\u636e\uff0c\u5199\u5165\u672c\u5730\u6587\u4ef6\r\n buf := make([]byte, 1024)\r\n for {\r\n n, _ := conn.Read(buf)\r\n if n == 0 {\r\n fmt.Println(\"\u63a5\u6536\u6587\u4ef6\u5b8c\u6210\u3002\")\r\n return\r\n }\r\n \/\/ \u5199\u5165\u672c\u5730\u6587\u4ef6\uff0c\u8bfb\u591a\u5c11\uff0c\u5199\u591a\u5c11\u3002\r\n f.Write(buf[:n])\r\n }\r\n}\r\n \r\nfunc main() {\r\n \/\/ \u521b\u5efa\u7528\u4e8e\u76d1\u542c\u7684socket\r\n listener, err := net.Listen(\"tcp\", \"127.0.0.1:8000\")\r\n if err != nil {\r\n fmt.Println(\" net.Listen err:\", err)\r\n return\r\n }\r\n defer listener.Close()\r\n \r\n fmt.Println(\"\u63a5\u6536\u7aef\u542f\u52a8\u6210\u529f\uff0c\u7b49\u5f85\u53d1\u9001\u7aef\u53d1\u9001\u6587\u4ef6\uff01\")\r\n \r\n \/\/ \u963b\u585e\u76d1\u542c\r\n conn, err := listener.Accept()\r\n if err != nil {\r\n fmt.Println(\" listener.Accept() err:\", err)\r\n return\r\n }\r\n defer conn.Close()\r\n \r\n \/\/ \u83b7\u53d6\u6587\u4ef6\u540d\uff0c\u4fdd\u5b58\r\n buf := make([]byte, 1024)\r\n n, err := conn.Read(buf)\r\n if err != nil {\r\n fmt.Println(\" conn.Read err:\", err)\r\n return\r\n }\r\n fileName := string(buf[:n])\r\n \r\n \/\/ \u56de\u5199 ok \u7ed9\u53d1\u9001\u7aef\r\n conn.Write([]byte(\"ok\"))\r\n \r\n \/\/ \u83b7\u53d6\u6587\u4ef6\u5185\u5bb9\r\n recvFile(conn, fileName)\r\n}<\/pre>\n\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9<\/p>\n","protected":false},"excerpt":{"rendered":"
\u672c\u6587\u5b9e\u4f8b\u4e3a\u5927\u5bb6\u5206\u4eab\u4e86Golang\u5b9e\u73b0\u6587\u4ef6\u4f20\u8f93\u7684\u5177\u4f53\u4ee3\u7801\uff0c\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5185\u5bb9\u5982\u4e0b \u501f\u52a9TCP\u5b8c\u6210\u6587\u4ef6\u7684\u4f20\u8f93\uff0c\u57fa […]<\/p>\n","protected":false},"author":668,"featured_media":232819,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-248886","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248886","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=248886"}],"version-history":[{"count":4,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248886\/revisions"}],"predecessor-version":[{"id":248891,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/248886\/revisions\/248891"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/232819"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=248886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=248886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=248886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}