From 90ba9418042ed56ebd0d58c8ce227218b6afdf7e Mon Sep 17 00:00:00 2001 From: Jonathan Williamson Date: Fri, 5 Jun 2015 09:10:03 -0700 Subject: [PATCH] Fix "bind: Address already in use" error if restart before socket is free --- main.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index adb3827..65a33c8 100644 --- a/main.c +++ b/main.c @@ -892,13 +892,17 @@ int main (int argc, char **argv) { if (port > 0) { struct sockaddr_in serv_addr; - + int yes = 1; sfd = socket (AF_INET, SOCK_STREAM, 0); if (sfd < 0) { perror ("socket"); exit(1); } + if(setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) { + perror("setsockopt"); + exit(1); + } memset (&serv_addr, 0, sizeof (serv_addr)); serv_addr.sin_family = AF_INET;