Files
core/reuseport/README.md
2021-10-11 01:38:13 +02:00

43 lines
1.1 KiB
Markdown

# reuseport
This is a fork from `https://github.com/libp2p/go-reuseport`. Last sync of changes: 11.10.2021
**NOTE:** This package REQUIRES go >= 1.11.
This package enables listening and dialing from _the same_ TCP or UDP port.
This means that the following sockopts may be set:
```
SO_REUSEADDR
SO_REUSEPORT
```
This is a simple package to help with address reuse. This is particularly
important when attempting to do TCP NAT holepunching, which requires a process
to both Listen and Dial on the same TCP port. This package provides some
utilities around enabling this behaviour on various OS.
## Examples
```Go
// listen on the same port. oh yeah.
l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
l2, _ := reuse.Listen("tcp", "127.0.0.1:1234")
```
```Go
// dial from the same port. oh yeah.
l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
l2, _ := reuse.Listen("tcp", "127.0.0.1:1235")
c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235")
```
**Note: cant dial self because tcp/ip stacks use 4-tuples to identify connections, and doing so would clash.**
## Tested
Tested on `darwin`, `linux`, and `windows`.