{"id":4312,"date":"2017-02-27T17:54:11","date_gmt":"2017-02-27T08:54:11","guid":{"rendered":"http:\/\/blog.bitmeister.jp\/?p=4312"},"modified":"2017-02-27T17:54:11","modified_gmt":"2017-02-27T08:54:11","slug":"go%e8%a8%80%e8%aa%9e%e3%81%a7tcp%e5%be%85%e5%8f%97%e3%81%abany%e3%82%a2%e3%83%89%e3%83%ac%e3%82%b9%e3%82%92%e6%8c%87%e5%ae%9a%e3%81%97%e3%81%9f%e5%a0%b4%e5%90%88%e3%81%ae%e6%8c%99%e5%8b%95","status":"publish","type":"post","link":"https:\/\/blog.bitmeister.jp\/?p=4312","title":{"rendered":"Go\u8a00\u8a9e\u3067TCP\u5f85\u53d7\u306bANY\u30a2\u30c9\u30ec\u30b9\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u306e\u6319\u52d5"},"content":{"rendered":"<p>\u6700\u8fd1Go\u8a00\u8a9e\u3067\u306e\u30b7\u30b9\u30c6\u30e0\u958b\u767a\u3092\u884c\u3046\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001ANY\u30a2\u30c9\u30ec\u30b9\u3092\u4f7f\u3063\u305fTCP\u5f85\u53d7\u3067\u30cf\u30de\u3063\u305f\u6319\u52d5\u304c\u3042\u3063\u305f\u306e\u3067\u30e1\u30e2\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4eca\u56de\u3092\u78ba\u8a8d\u3057\u3066\u3044\u308b\u74b0\u5883\u306fLinux\u3067\u3059\u3002Windows\u3084Mac OS\u306f\u6319\u52d5\u304c\u7570\u306a\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002<\/p>\n<h3>\u74b0\u5883<\/h3>\n<table>\n<tbody>\n<tr>\n<td>OS<\/td>\n<td>Ubuntu 16.04.2 LTS<\/td>\n<\/tr>\n<tr>\n<td>Kernel<\/td>\n<td>4.4.0-62-generic<\/td>\n<\/tr>\n<tr>\n<td>Golang<\/td>\n<td>go1.8 linux\/amd64<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u30b5\u30f3\u30d7\u30eb<\/h3>\n<p>\u4eca\u56de\u306fHTTP\u30b5\u30fc\u30d0\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n<pre><code  class=\"Go\">\r\npackage main\r\n\r\nimport (\r\n\t\"flag\"\r\n\t\"fmt\"\r\n\t\"net\"\r\n\t\"net\/http\"\r\n)\r\n\r\nvar proto = flag.String(\"proto\", \"tcp\", \"proto(tcp|tcp4|tcp6)\")\r\nvar addr = flag.String(\"addr\", \"0.0.0.0\", \"listen addres\")\r\nvar port = flag.Int(\"port\", 9999, \"listen port\")\r\n\r\nfunc main() {\r\n\tflag.Parse()\r\n\tfmt.Printf(\"proto[%s]listen[%s:%d]\\n\", *proto, *addr, *port)\r\n\r\n\ttcpAddr, err := net.ResolveTCPAddr(*proto, fmt.Sprintf(\"%s:%d\", *addr, *port))\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\r\n\tlistener, err := net.ListenTCP(*proto, tcpAddr)\r\n\tif err != nil {\r\n\t\tpanic(err)\r\n\t}\r\n\tdefer listener.Close()\r\n\r\n\thttp.HandleFunc(\"\/\", handler)\r\n\ts := http.Server{}\r\n\ts.Serve(listener)\r\n}\r\n\r\nfunc handler(w http.ResponseWriter, r *http.Request) {\r\n\tfmt.Fprintf(w, \"Hello, World\")\r\n}\r\n<\/code><\/pre>\n<p>\u3053\u306e\u30bd\u30fc\u30b9\u3092\u30d3\u30eb\u30c9\u3057\u3066\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<pre><code class=\"Bash\">\r\n$ .\/httptest\r\nproto[tcp]listen[0.0.0.0:9999]\r\n\r\n<\/code><\/pre>\n<pre><code class=\"Bash\">\r\n$ curl http:\/\/localhost:9999\/\r\nHello, World\r\n<\/code><\/pre>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067HTTP\u901a\u4fe1\u81ea\u4f53\u306f\u306a\u3093\u3089\u554f\u984c\u306f\u306a\u3044\u306e\u3067\u3059\u304c\u3001netstat\u3067listen\u30bd\u30b1\u30c3\u30c8\u3092\u8868\u793a\u3059\u308b\u3068<\/p>\n<pre><code class=\"Bash\">\r\n$ netstat -lnt\r\n\u7a3c\u50cd\u4e2d\u306e\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u63a5\u7d9a (\u30b5\u30fc\u30d0\u306e\u307f)\r\nProto \u53d7\u4fe1-Q \u9001\u4fe1-Q \u5185\u90e8\u30a2\u30c9\u30ec\u30b9   \u5916\u90e8\u30a2\u30c9\u30ec\u30b9  \u72b6\u614b\r\ntcp6       0      0 :::9999        :::*       LISTEN\r\n<\/code><\/pre>\n<p>IPv4\u30de\u30c3\u30d7\u30c9\u30a2\u30c9\u30ec\u30b9(IPv4\u5c04\u5f71\u30a2\u30c9\u30ec\u30b9)\u304c\u4f7f\u308f\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u8a66\u3057\u306bIPv4\u3068IPv6\u306e\u30eb\u30fc\u30d7\u30d0\u30c3\u30af\u30a2\u30c9\u30ec\u30b9\u3092\u6307\u5b9a\u3059\u308b\u3068\u4ee5\u4e0b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>IPv4<\/h4>\n<pre><code class=\"Bash\">\r\n$ .\/httptest\u3000-addr 127.0.0.1\r\nproto[tcp]listen[127.0.0.1:9999]\r\n\r\n<\/code><\/pre>\n<pre><code class=\"Bash\">\r\n$ netstat -lnt\r\n\u7a3c\u50cd\u4e2d\u306e\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u63a5\u7d9a (\u30b5\u30fc\u30d0\u306e\u307f)\r\nProto \u53d7\u4fe1-Q \u9001\u4fe1-Q \u5185\u90e8\u30a2\u30c9\u30ec\u30b9   \u5916\u90e8\u30a2\u30c9\u30ec\u30b9  \u72b6\u614b\r\ntcp        0      0 127.0.0.1:9999 0.0.0.0:*  LISTEN\r\n<\/code><\/pre>\n<h4>IPv6<\/h4>\n<pre><code class=\"Bash\">\r\n$ .\/httptest -addr [::1]\r\nproto[tcp]listen[[::1]:9999]\r\n\r\n<\/code><\/pre>\n<pre><code class=\"Bash\">\r\n$ netstat -lnt\r\n\u7a3c\u50cd\u4e2d\u306e\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u63a5\u7d9a (\u30b5\u30fc\u30d0\u306e\u307f)\r\nProto \u53d7\u4fe1-Q \u9001\u4fe1-Q \u5185\u90e8\u30a2\u30c9\u30ec\u30b9   \u5916\u90e8\u30a2\u30c9\u30ec\u30b9  \u72b6\u614b\r\ntcp6       0      0 ::1:9999       :::*       LISTEN\r\n<\/code><\/pre>\n<p>\u3064\u307e\u308a\u300cnet.ListenTCP\u300d\u306b&#8221;tcp&#8221;\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u3001IP\u30a2\u30c9\u30ec\u30b9\u3067IPv4\u304bIPv6\u304b\u3092\u5224\u65ad\u3057\u3066\u3044\u308b\u304c\u3001ANY\u30a2\u30c9\u30ec\u30b9\u306e\u5834\u5408\u306fIPv6\u3067\u30de\u30c3\u30d7\u3059\u308b\u4ed5\u69d8\u3068\u306a\u3063\u3066\u3044\u308b\u3088\u3046\u3067\u3059\u3002<\/p>\n<p>\u901a\u5e38\u3053\u308c\u3067\u56f0\u308b\u30b1\u30fc\u30b9\u306f\u306a\u3044\u306e\u3067\u3059\u304c\u3001\u4eca\u56de\u306fTCP\u63a5\u7d9a\u4e2d\u306e\u30b3\u30cd\u30af\u30b7\u30e7\u30f3\u60c5\u5831\u3092netstat\u3067\u306f\u306a\u304fproc\u304b\u3089\u76f4\u63a5\u691c\u7d22\u3059\u308b\u6642\u306bIPv4\u3068IPv6\u3067\u306f\u53c2\u7167\u5148\u304c\u7570\u306a\u308b\u305f\u3081\u306b\u60c5\u5831\u304c\u898b\u3064\u304b\u3089\u305a\u30de\u30c3\u30c1\u3057\u306a\u3044\u3068\u3044\u3046\u30b1\u30fc\u30b9\u306b\u906d\u9047\u3057\u307e\u3057\u305f\u3002<\/p>\n<table>\n<tbody>\n<tr>\n<td>IPv4<\/td>\n<td>\/proc\/net\/tcp<\/td>\n<\/tr>\n<tr>\n<td>IPv6<\/td>\n<td>\/proc\/net\/tcp6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u306a\u304a\u3001proc\u3092\u76f4\u63a5\u53c2\u7167\u305b\u305a\u3068\u3082netstat\u30b3\u30de\u30f3\u30c9\u306e\u5f15\u6570\u3067IPv4\u9650\u5b9a\u306b\u3057\u305f\u5834\u5408\u3067\u3082\u540c\u3058\u304f\u30de\u30c3\u30c1\u306f\u3057\u307e\u305b\u3093\u3002<\/p>\n<p>\u3053\u308c\u3092\u56de\u907f\u3059\u308b\u306b\u306f\u300cnet.ListenTCP\u300d\u306b&#8221;tcp4&#8243;\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre><code  class=\"Bash\">\r\n$ .\/httptest -proto tcp4\r\nproto[tcp4]listen[0.0.0.0:9999]\r\n\r\n<\/code><\/pre>\n<pre><code  class=\"Bash\">\r\n$ netstat -lnt\r\n\u7a3c\u50cd\u4e2d\u306e\u30a4\u30f3\u30bf\u30fc\u30cd\u30c3\u30c8\u63a5\u7d9a (\u30b5\u30fc\u30d0\u306e\u307f)\r\nProto \u53d7\u4fe1-Q \u9001\u4fe1-Q \u5185\u90e8\u30a2\u30c9\u30ec\u30b9   \u5916\u90e8\u30a2\u30c9\u30ec\u30b9  \u72b6\u614b\r\ntcp        0      0 0.0.0.0:9999   0.0.0.0:*  LISTEN\r\n<\/code><\/pre>\n<p>HTTP\u3092\u4f7f\u3046\u30b1\u30fc\u30b9\u3067\u4eca\u56de\u3068\u540c\u69d8\u306e\u4e8b\u3067\u56f0\u308b\u30b1\u30fc\u30b9\u306f\u6b86\u3069\u306a\u3044\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u305d\u306e\u5834\u5408\u306f\u300chttp.ListenAndServe\u300d\u3060\u3068&#8221;tcp&#8221;\u56fa\u5b9a\u306a\u306e\u3067\u4e0a\u8a18\u30b3\u30fc\u30c9\u306e\u3088\u3046\u306b\u300cnet.TCPListener\u300d\u3092\u81ea\u524d\u3067\u4f5c\u6210\u3059\u308b\u3088\u3046\u306b\u3059\u308c\u3070\u89e3\u6c7a\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1Go\u8a00\u8a9e\u3067\u306e\u30b7\u30b9\u30c6\u30e0\u958b\u767a\u3092\u884c\u3046\u3053\u3068\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001ANY\u30a2\u30c9\u30ec\u30b9\u3092\u4f7f\u3063\u305fTCP\u5f85\u53d7\u3067\u30cf\u30de\u3063\u305f\u6319\u52d5\u304c\u3042\u3063\u305f\u306e\u3067\u30e1\u30e2\u3057\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[147,18],"class_list":["post-4312","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-linux"],"_links":{"self":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/4312","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4312"}],"version-history":[{"count":17,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/4312\/revisions"}],"predecessor-version":[{"id":4330,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=\/wp\/v2\/posts\/4312\/revisions\/4330"}],"wp:attachment":[{"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bitmeister.jp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}