Skip to content

Latest commit

 

History

History
24 lines (17 loc) · 693 Bytes

2022-05-03_release-a-port-immediately.md

File metadata and controls

24 lines (17 loc) · 693 Bytes

Release a port immediately

Use the socket.SO_REUSEADDR flag when creating a TCP server so that killing the server immediataly releases the port. Without a quick restart can error out with a "port already in use" error, and this can be SUPER annoying!

Example how to set the flag in python:

server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

And in C:

int enable = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0)
    error("setsockopt(SO_REUSEADDR) failed");

Links: