2019-06-09 07:24:20 +00:00
|
|
|
# go-reuseport
|
|
|
|
|
|
|
|
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai)
|
2023-06-30 13:41:32 +00:00
|
|
|
[![GoDoc](https://godoc.org/github.com/libp2p/go-reuseport?status.svg)](https://godoc.org/github.com/libp2p/go-reuseport)
|
2021-06-16 20:19:45 +00:00
|
|
|
[![](https://img.shields.io/badge/project-libp2p-yellow.svg?style=flat-square)](https://libp2p.io/)
|
|
|
|
[![](https://img.shields.io/badge/freenode-%23libp2p-yellow.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23libp2p)
|
2019-06-09 07:24:20 +00:00
|
|
|
[![codecov](https://codecov.io/gh/libp2p/go-reuseport/branch/master/graph/badge.svg)](https://codecov.io/gh/libp2p/go-reuseport)
|
|
|
|
[![Travis CI](https://travis-ci.org/libp2p/go-reuseport.svg?branch=master)](https://travis-ci.org/libp2p/go-reuseport)
|
2021-06-16 20:19:45 +00:00
|
|
|
[![Discourse posts](https://img.shields.io/discourse/https/discuss.libp2p.io/posts.svg)](https://discuss.libp2p.io)
|
2019-06-09 07:24:20 +00:00
|
|
|
|
|
|
|
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`.
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
The last gx published version of this module was: 0.2.2: Qme8kdM7thoCqLqd7GYCRqipoZJS64rhJo5MBcTpyWfsL9
|