title: can not connect to socket inside vmware vm
body: <p>I created a server socket in C. This is the most basic stuff like what you would fine in a simple TCP server example. Server code is below. I also created a client socket that runs on the host machine. Code also below. However, for some reason the client is not able to connect to the server. The IP address I used is the same as the one under the entry eth0 from the "ip addr" command.  The network adapter of the VM is a bridged connection.</p>
  
  <p>The exact same code works when both client and server run on the same machine (the host). </p>
  
  <p>Thank you!</p>
  
  <p>Server code:</p>
  
  <pre><code>int sockfd;
  int clientfd;
  struct sockaddr_in self;
  struct sockaddr_in client_addr;
  int addrlen = sizeof (client_addr);
  
  
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) {
      perror("Socket");
      return EXIT_FAILURE;
  }
  
  printf("Socket descriptor is: %d\n", sockfd);
  
  memset(&amp;self, 0, sizeof (self));
  self.sin_family = AF_INET;
  self.sin_port = htons(MY_PORT);
  self.sin_addr.s_addr = INADDR_ANY;
  
  if (bind(sockfd, (struct sockaddr*) &amp;self, sizeof (self)) != 0) {
      perror("socket--bind");
      return EXIT_FAILURE;
  }
  
  if (listen(sockfd, 20) != 0) {
      perror("socket--listen");
      return EXIT_FAILURE;
  }
  
  clientfd = accept(sockfd, (struct sockaddr*) &amp;client_addr, &amp;addrlen);
  printf("%s:%d connected\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));
  </code></pre>
  
  <p>Client Code:</p>
  
  <pre><code>int sock;
  struct sockaddr_in server;
  //struct sockaddr_in client; 
  struct hostent *hp;
  //char buf[BUFFER_SIZE];
  
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0)
      die(6, "Opening stream socket");
  
  printf("Client socket file descriptor is: %d\n", sock);
  
  memset(&amp;server, (char) 0, sizeof (server));
  server.sin_family = AF_INET;
  hp = gethostbyname(host_name);
  if (!hp) {
      //sprintf(buf, "%s: unknown host\n", host_name);
      die(8,  "%s: unknown host\n", host_name);
  }
  memcpy(hp-&gt;h_addr, &amp;server.sin_addr, hp-&gt;h_length);
  server.sin_port = htons((u_short) SERVER_PORT);
  
  /* Try to connect */
  if ((connect(sock, (struct sockaddr *) &amp;server, sizeof (server))) &lt; 0)
      die(7, "%s", "Failed to connect stream socket\n");
  </code></pre>