title: Specific server IP using winsock
body: <p>I need some help</p>
  
  <p>Last week I wrote my first server application in C++ using winsock2 by following a tutorial.
  This tutorial does not connect to a specific IP, but using a tool called "SocketTest" I can connect to the server via IP 127.0.0.1 and port 21.</p>
  
  <p>I would like to use a specific IP address (192.168.1.10) and port (17013).
  I know how to do the port but I son't know how to connect to a specific IP address.
  Could anyone help me and explain it to me?
  This is the code I use:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;WS2tcpip.h&gt;
  
  #pragma comment (lib, "ws2_32.lib")
  
  using namespace std;
  
  void main()
  {
  
      //initialize winsock (sockets)
      WSADATA wsData;
      WORD ver = MAKEWORD(2, 2);
  
      int wsOk = WSAStartup(ver, &amp;wsData);
      if (wsOk != 0)
      {
          cerr &lt;&lt; "Can't initialize winsock! quitting" &lt;&lt; endl;
          return;
      }
  
      //create a socket
      SOCKET listening = socket(AF_INET, SOCK_STREAM, 0);
      if (listening == INVALID_SOCKET)
      {
          cerr &lt;&lt; "Can't create a socket! quitting" &lt;&lt; endl;
          return;
      }
  
      //bind the ip to a socket address and port
      sockaddr_in hint;
      hint.sin_family = AF_INET;
      hint.sin_port = htons(21);
      hint.sin_addr.S_un.S_addr = INADDR_ANY; //could also use inet_pton
  
      bind(listening, (sockaddr*)&amp;hint, sizeof(hint));
  
      //tell winsock the socket is for listening
      listen(listening, SOMAXCONN);
  
      //wait for a connection
      sockaddr_in client;
      int clientSize = sizeof(client);
  
      SOCKET clientSocket = accept(listening, (sockaddr*)&amp;client, &amp;clientSize);
      if (clientSocket == INVALID_SOCKET)
      {
          cerr &lt;&lt; "Can't make connection! quitting" &lt;&lt; endl;
          return;
      }
  
      char host[NI_MAXHOST];      //client's remote name
      char service[NI_MAXSERV];   //service (i.e. port) the client is connect on
  
      ZeroMemory(host, NI_MAXHOST);
      ZeroMemory(service, NI_MAXSERV);
  
      if (getnameinfo((sockaddr*)&amp;client, sizeof(client), host, NI_MAXHOST, service, NI_MAXSERV, 0) == 0)
      {
          cout &lt;&lt; host &lt;&lt; " connected on port " &lt;&lt; service &lt;&lt; endl;
      }
      else
      {
          inet_ntop(AF_INET, &amp;client.sin_addr, host, NI_MAXHOST);
          cout &lt;&lt; host &lt;&lt; " connected on port " &lt;&lt;
              ntohs(client.sin_port) &lt;&lt; endl;
      }
  
      //close listening socket
      closesocket(listening);
  
      char buf[4096];
  
      while (true) //this part of the code will be used to send and receive data
      {
          ZeroMemory(buf, 4096);
          float X = 12.4;
          float Y = 456;
          float Z = 900.87;
  
          sprintf_s(buf, "(%lf,%lf,%lf)",X,Y,Z);
          size_t buf_len = strlen(buf);
  
          send(clientSocket, buf, buf_len, 0);
  
  
  
  
          int bytesReceived = recv(clientSocket, buf, 4096, 0);
      }
  
      //close the socket(s)
      closesocket(clientSocket);
  
      //shutdown/cleanup winsock (sockets)
      WSACleanup();
  
  }
  </code></pre>