title: How to write to a socket file descriptor without using ``connect()``?
body: <p>I'm trying to write through a socket using its descriptor. I have used <code>bind()</code> to bind the socket to the address I want. Whenever I'm using <code>sendto()</code> everything is OK. But whenever I'm using <code>write()</code> (or <code>send()</code>) I'll face <code>Destination address required</code>(errno 89).</p>
  <p>What I'm trying to do is to write through the socket using <code>iovec</code>. In this case we don't have any alternative for <code>writev()</code> to get destination address. As I don't want to connect every single time, I was wondering whether I can do it without having to use <code>connect()</code> or <code>accept()</code> syscalls.</p>
  <p>Here is the code I wrote:</p>
  <pre><code>
  int sock = 0;
  struct sockaddr_in serv_addr; 
  char *hello = &quot;chetori to&quot;; 
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) &lt; 0) 
  { 
      printf(&quot;\n Socket creation error \n&quot;); 
      return -1; 
  } 
  int opt = 1; 
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &amp;opt, sizeof(opt))) 
  { 
      perror(&quot;setsockopt&quot;); 
      exit(EXIT_FAILURE); 
  }
  
  string interface_name = &quot;lo&quot;;
  ifreq ifr;
  memset(&amp;ifr, 0, sizeof(ifr));
  snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), &quot;%s&quot;, interface_name.c_str());
  if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, reinterpret_cast&lt;void*&gt;(&amp;ifr), sizeof(ifr)) &lt; 0)
      throw exception();  // Imaginary exception
  
  memset(&amp;serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  
  string interface_address = &quot;127.0.0.1&quot;;
  if (inet_aton(interface_address.c_str(), &amp;serv_addr.sin_addr) == 0)
      throw exception();  // Imaginary exception
  
  serv_addr.sin_port = htons(PORT);
  
  if (bind(sock, reinterpret_cast&lt;sockaddr*&gt;(&amp;serv_addr), sizeof(serv_addr)) &lt; 0)
      throw exception();  // Imaginary exception
      
  char f[1024] = &quot;Hello there&quot;;
  char sec[1024] = &quot;How are you&quot;;
  char th[1024] = &quot;I'm fine&quot;;
  char fh[1024] = &quot;Bye bye&quot;;
  struct iovec bufs[] = {
      { .iov_base = f, .iov_len = sizeof(f) },
      { .iov_base = sec, .iov_len = sizeof(sec) },
      { .iov_base = th, .iov_len = sizeof(th) },
      { .iov_base = fh, .iov_len = sizeof(fh) },
  };
  int iovcnt = sizeof(bufs) / sizeof(struct iovec);
  writev(sock, bufs, iovcnt);
  
  </code></pre>
  <p>Note that Interface name and IP address have no problem and are correct.</p>