Todo:golang Language TCP/UDP protocol reuse address port

Source: Internet
Author: User
Tags set socket

Todo:golang Language TCP/UDP protocol reuse address port

650) this.width=650; "class=" Alignnone size-full wp-image-977 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/11/11fa00044d7450dffd2a-2.jpg "width=" "height=" 337 "alt=" Todo:golang language TCP/UDP protocol reuse address port "style=" border:0px; vertical-align:middle;margin:0px auto;height:auto; "/>

650) this.width=650; "class=" Alignnone size-full wp-image-978 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/11/11f60003079bb72000ca-2.jpg "width=" 382 "height=" + "alt=" Todo:golang language TCP/UDP protocol reuse address port "style=" border:0px; vertical-align:middle;margin:0px auto;height:auto; "/>

This is a simple package to solve the problem of reusing addresses. The Go net package (as far as I know) is not allowed to set socket options. This is particularly problematic when attempting TCP NAT, which requires listening and dialing on the same TCP port. This package makes it possible for me to implement this function. This is a very small use case, but perhaps this package can become more common over time.

This package allows listening and dialing from the same TCP port, not listening with the same UDP port, but listening to the same UDP port. This means that the following sockopts are set:

Syscall. So_reuseaddr

Syscall. So_reuseport

Example:

Listen on the same port.

L1, _: = Reuse. Listen ("TCP", "127.0.0.1:1234")

L2, _: = Reuse. Listen ("TCP", "127.0.0.1:1234")

Dial from the same port.

L1, _: = Reuse. Listen ("TCP", "127.0.0.1:1234")

L2, _: = Reuse. Listen ("TCP", "127.0.0.1:1235")

C1, _: = Reuse. Dial ("TCP", "127.0.0.1:1234", "127.0.0.1:1235")

C2, _: = Reuse. Dial ("TCP", "127.0.0.1:1234", "127.0.0.1:1235")

The following is a complete TCP dialing example, tested in Ubuntu 14.04 Sever

Server code:

650) this.width=650; "class=" Alignnone size-full wp-image-979 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/11/11ff00024e98e6753d35-2.jpg "width=" 640 "height=" 659 "alt=" Todo:golang language TCP/UDP protocol reuse address port "style=" border:0px; vertical-align:middle;margin:0px auto;height:auto; "/>

Client code:

650) this.width=650; "class=" Alignnone size-full wp-image-980 "src=" http://www.todosomeone.com/wp-content/uploads/ 2016/11/11fe0003d2907d942511-2.jpg "width=" 640 "height=" 410 "alt=" Todo:golang language TCP/UDP protocol reuse address port "style=" border:0px; vertical-align:middle;margin:0px auto;height:auto; "/>

If you have a scenario where you need to reuse TCP/UDP's address and port, try this package and hopefully you'll get the most out of it.

Wxgzh:ludong86

650) this.width=650; "class=" AlignCenter size-full wp-image-845 "src=" http://www.todosomeone.com/wp-content/uploads /2016/11/qrcode_for_gh_6bb1f39ae99c_258-1.jpg "alt=" qrcode_for_gh_6bb1f39ae99c_258-1 "width=" 258 "height=" 258 " style= "border:0px;vertical-align:middle;margin:0px auto;height:auto;text-align:center;"/>


This article is from the computer language driven data blog, so be sure to keep this source http://onetodo.blog.51cto.com/12321875/1878041

Todo:golang Language TCP/UDP protocol reuse address port

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.