# Struggling to make a TCP server based on new socket module using use the “reuseaddr” and “reuseport” options

**URL:** <https://erlangforums.com/t/struggling-to-make-a-tcp-server-based-on-new-socket-module-using-use-the-reuseaddr-and-reuseport-options/1789>\
**Category:** Questions / Help\
**Tags:** tcp\
**Created:** [August 28, 2022, 12:14am UTC](https://erlangforums.com/t/struggling-to-make-a-tcp-server-based-on-new-socket-module-using-use-the-reuseaddr-and-reuseport-options/1789 "2022-08-28T00:14:37Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![saleyn](https://erlangforums.com/user_avatar/erlangforums.com/saleyn/32/1188_2.png) [@saleyn](https://erlangforums.com/u/saleyn)\
**Post date:** [August 28, 2022, 6:55pm UTC](https://erlangforums.com/t/struggling-to-make-a-tcp-server-based-on-new-socket-module-using-use-the-reuseaddr-and-reuseport-options/1789/3 "2022-08-28T18:55:18Z")

</div>

> [@zabrane](#):
>
> `reuseaddr => true,`

The main issue with your use of socket options is that `reuseaddr` and `reuseport` options have to be set prior to calling `socket:bind/2`. Try this:

```erlang
{ok, LSock} = socket:open(inet, stream, tcp),
ok = socket:setopt(LSock, socket, reuseaddr, true),
ok = socket:setopt(LSock, socket, reuseport, true),
ok = socket:bind(LSock, #{family => inet, port => Port, addr => Addr}),
...

```

Secondly, if you need to have multiple worker processes doing the accept, you can pass the `LSock` to a pool of worker processes so that they are calling the `socket:accept/1` on the **same** instance of `LSock`. If your idea was that the `reuseaddr` and `reuseport` will help you have multiple listening sockets open on the same address/port pair, that is not what those two options are for. These options are meant to allow your OS process to reconnect to the same address/port right after the process crashed that was previously bound to that address/port pair, so that the OS would release the socket and make it available for new binding immediately.

---

_[View the full topic](https://erlangforums.com/t/struggling-to-make-a-tcp-server-based-on-new-socket-module-using-use-the-reuseaddr-and-reuseport-options/1789)._
