Coder Social home page Coder Social logo

golang-tcp-port-reuse's Introduction

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

golang-tcp-port-reuse's People

Contributors

nordicdyno avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.