summaryrefslogtreecommitdiffstats
path: root/socket.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-02-27 18:37:55 -0800
committerKaz Kylheku <kaz@kylheku.com>2016-02-27 18:37:55 -0800
commitf589076e26aa831e3e7effed243860debd6f7f6a (patch)
tree924db4ae1e72d40ac52a5c770601561fce83699a /socket.c
parentc9d657e320eb40e71695b587dc2e47a9230d05bf (diff)
downloadtxr-f589076e26aa831e3e7effed243860debd6f7f6a.tar.gz
txr-f589076e26aa831e3e7effed243860debd6f7f6a.tar.bz2
txr-f589076e26aa831e3e7effed243860debd6f7f6a.zip
Do the SO_REUSEADDR thing when binding socket.
* socket.c (sock_bind): Set the SO_REUSEADDR option before binding the socket, to thwart the EADDRINUSE nuisance.
Diffstat (limited to 'socket.c')
-rw-r--r--socket.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/socket.c b/socket.c
index 6a637edf..b0c90465 100644
--- a/socket.c
+++ b/socket.c
@@ -237,14 +237,17 @@ static void sockaddr_in(val sockaddr, val family,
static val sock_bind(val sock, val sockaddr)
{
- val sfd = stream_fd(sock);
+ int sfd = c_num(stream_fd(sock));
val family = sock_family(sock);
struct sockaddr_storage sa;
socklen_t salen;
+ int reuse = 1;
+
+ (void) setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
sockaddr_in(sockaddr, family, &sa, &salen);
- if (bind(c_num(sfd), coerce(struct sockaddr *, &sa), salen) != 0)
+ if (bind(sfd, coerce(struct sockaddr *, &sa), salen) != 0)
uw_throwf(socket_error_s, lit("bind failed: ~d/~s"),
num(errno), string_utf8(strerror(errno)), nao);