title: error in binding port "Address already in use" TCP socket programming in unix
body: <p>I've gone through many posts and forums and I'm new to socket programming. Major parts of my code are similar to 
  <a href="https://stackoverflow.com/questions/2147592/bind-error-address-already-in-use/9206435#9206435">BIND ERROR : Address already in use</a></p>
  
  <p>but then i changed my code so that i include "setsockopt" function like so:</p>
  
  <pre><code>const char* port="5555";
  int opt=1;
  portno=atoi(port);
  //parameters for server address
  serv_addr.sin_family=AF_INET;
  serv_addr.sin_port=htons(portno);
  serv_addr.sin_addr.s_addr=INADDR_ANY;
  //bind the socket to the address
  setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(const char *)&amp;opt,sizeof(int));
  
  
   if(bind(sockfd,(struct sockaddr*)&amp;serv_addr,sizeof(serv_addr))&lt;0)
  {close(sockfd);
  error("error in binding port!");
  }
  </code></pre>
  
  <p>But still i get the error. I have to close the terminal and restart it in order to use the port again. I want to use a hardcoded port (like i mentioned in the code above) </p>
  
  <pre><code>                                                            Thanks a lot in advance
  </code></pre>