title: C, Bind faild: `Address Already in Use`
body: <p>I am writing a client-server program in C. I am getting an error in Bind function says: <code>Address Already in Use</code>. So I tired to use memset() and I got error says: <code>Invalid Argument</code>. Finally, I looked for similar questions on here, and some of them suggesting the use of setsocketopt() function. I used it and I am getting error says:</p>
  <blockquote>
  <p>/tmp/ccBNsJtU.o: In function <code>main</code>:</p>
  <p>socket.c:(.text+0xd0): undefined reference to <code>setsocket</code></p>
  <p>collect2: error: ld returned 1 exit status</p>
  </blockquote>
  <p>I looked at almost-if-not all the similar questions even in different programing languages. Now I am stuck. Do I have to use setsocket() and if I do, is it causing a problem in my code? Or I don't have to use it and the problem is somewhere else? Could the problem be in the client or the server functions where I listen and send messages? This is the part of the code where the bind() and setsocket() functions are:</p>
  <pre><code>int main (void) {
  int sl, sa, bn, erro, lis;
  int status;
  
  //server log socket
  struct sockaddr_un server = {AF_UNIX, &quot;log_server&quot;};
  sl = socket (AF_UNIX, SOCK_STREAM, 0);
  if(sl &lt; 0) {
  perror(&quot;Faild to create socket - in main&quot;);
      exit(1);
  }
  
  //I added this part
  if (setsocket(sl, SOL_SOCKET, SO_REUSEADDR, &amp;server, sizeof(server)) == -1) {
      perror(&quot;setsocket - in main&quot;);
      exit(1);
  }
  bn = bind (sl, (struct sockaddr *)&amp;server, sizeof(server));
  if(bn &lt; 0){
      perror(&quot;Faild to bind - in main&quot;);
      exit(1);
  }
  lis = listen (sl, 1); //to be changed to 4
  if (lis &lt; 0) {
      perror(&quot;Faild to listen - in main&quot;);
  }
  </code></pre>