Skip to content

nordicdyno/golang-tcp-port-reuse

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

golang-tcp-port-reuse

This repo contains code for reproducing port binding problems in Go and for understanding how socket stuff works in general.

Problem description

Sometimes your TCP server written in Go fails with error bind: address already in use

It could happen in such cases as:

  1. Other process already listens on this port on the same address. (non interesting case)
  2. Server code explicitly disables SO_REUSEADDR (enabled by default in *nix systems) and tries to bind on same port and address which have unfinished connection in TIME_WAIT state.
  3. Client uses random port for itself and make a lot of reconnects so it exhausted all ephemeral ports (left all potential available port in TIME_WAIT state)
  4. Client uses the same port to itself

Or sometimes your TCP client fails bind to ephemeral port.

TODO: describe

Scenario #1: Can't bind on the same port by the server w/o SO_REUSEADDR option if socket in TIME_WAIT state

  1. Server listens on 1215 port, close connection and exit after 2 seconds: go run server/main.go -l 127.0.0.1:1215 -s 2.
  2. Client connects to 1215 port, exit without closing connection after 4 seconds: go run client/main.go -e 127.0.0.1:1215 -s 5 -noclose. Lefts server side in TIME_WAIT state for 30s (depends on system settings).
  3. Check connections statuses: (netstat -a -n | head -n 2) ; (netstat -a -n | grep 1215).
  4. Try to bind on port w/o SO_REUSEADDR option: go run server/main.go -l 127.0.0.1:1215 -s 2 -no-reuse-addr (should fail if you are fast enough)
  5. Try to bind on port with default mode: go run server/main.go -l 127.0.0.1:1215 -s 2 (should works if no other process listens on the same ip:port pair)

Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages