title: Can one reliably connect a TCP socket to itself?
body: <p>I am wondering whether it is possible to reliably connect a TCP socket to itself -- that is, to get just one socket where whatever you send() on you receive back through recv(). I saw that this can happen (e.g., <a href="https://stackoverflow.com/questions/17584383/why-can-a-socket-connect-to-its-own-ephemeral-port">here</a>, <a href="https://stackoverflow.com/questions/5139808/tcp-simultaneous-open-and-self-connect-prevention">here</a>, and <a href="http://sgros.blogspot.co.uk/2013/08/tcp-client-self-connect.html" rel="nofollow noreferrer">here</a>), but none of these posts explain how to do this programmatically and reliably (i.e., this is usually touted as a curiosity, rather than a feature one would use deliberately). I'm interested in a solution for Windows, Mac, and Linux.</p>
  
  <p>Just for completeness, please let me explain why I'm interested in this. I'm writing a cloud-based application where servers can send messages to other servers, including themselves. Each server uses one socket to talk to other servers, and so, to unify the code and make certain threading issues simpler, it would be good if the server could talk to itself using just one socket as well.</p>
  
  <p><strong>EDIT:</strong> @Rufflewind suggested connecting to the loopback adapter. I tried that using the code below, both with and without the call to listen. In all cases, however, I got an error (either "Invalid argument" or "Operation not supported"). What am I doing wrong?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;netinet/tcp.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  void die(const char* const message) {
      perror(message);
      exit(0);
  }
  
  #define CHECK(operation,message) \
      if ((operation) != 0) \
          die(message)
  
  int main(int argc, char* argv[]) {
      sockaddr_in local;
      local.sin_family = AF_INET;
      local.sin_port = htons(40000);
      inet_aton("127.0.0.1", &amp;local.sin_addr);
      int sck = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      CHECK(bind(sck, (struct sockaddr *)&amp;local, sizeof(local)), "Error while binding the socket to an address");
  //    CHECK(listen(sck, 1), "Error while listening.");
      CHECK(connect(sck, (struct sockaddr *)&amp;local, sizeof(local)), "Error while connecting the socket to self");
      std::cout &lt;&lt; "Succeeded in connecting sockets!" &lt;&lt; std::endl;
      const char* message = "ABCDEFGH";
      const size_t messageLength = ::strlen(message) + 1;
      const char* currentSend = message;
      ssize_t leftToSend = messageLength;
      while (leftToSend != 0) {
          const ssize_t sent = send(sck, currentSend, leftToSend, 0);
          if (sent == -1)
              die("Can't send.");
          currentSend += sent;
          leftToSend -= sent;
      }
      std::cout &lt;&lt; "Sent the message!" &lt;&lt; std::endl;
      char buffer[256];
      char* currentRead = buffer;
      size_t leftToRead = messageLength;
      while (leftToRead != 0) {
          const ssize_t read = recv(sck, currentRead, leftToRead, 0);
          if (read == -1)
              die("Can't read.");
          currentRead += read;
          leftToRead -= read;
      }
      std::cout &lt;&lt; "Received message: " &lt;&lt; buffer &lt;&lt; std::endl;
      return 0;
  }
  </code></pre>