title: How to access my laptop IP over internet using windows sockets?
body: <p>Currently, I am working on a project in which I have to send data from one computer to another computer using windows sockets and the internet. Currently, I am able to send and receive data using LAN.</p>
  <p>Now how can I receive and send data over the internet from 1 computer to another computer?
  Here is my server and client code.</p>
  <p>Server.cpp</p>
  <pre><code>#define _WINSOCK_DEPRECATED_NO_WARNINGS
  #include &lt;iostream&gt;
  #include &lt;WS2tcpip.h&gt;
  //#pragma warning(disable:4996) 
  
  // Include the Winsock library (lib) file
  #pragma comment (lib, &quot;ws2_32.lib&quot;)
  
  // Saves us from typing std::cout &lt;&lt; etc. etc. etc.
  using namespace std;
  
  // Main entry point into the server
  void main()
  {
      ////////////////////////////////////////////////////////////
      // INITIALIZE WINSOCK
      ////////////////////////////////////////////////////////////
  
      // Structure to store the WinSock version. This is filled in
      // on the call to WSAStartup()
      WSADATA data;
  
      // To start WinSock, the required version must be passed to
      // WSAStartup(). This server is going to use WinSock version
      // 2 so I create a word that will store 2 and 2 in hex i.e.
      // 0x0202
      WORD version = MAKEWORD(2, 2);
  
      // Start WinSock
      int wsOk = WSAStartup(version, &amp;data);
      if (wsOk != 0)
      {
          // Not ok! Get out quickly
          cout &lt;&lt; &quot;Can't start Winsock! &quot; &lt;&lt; wsOk;
          return;
      }
  
      ////////////////////////////////////////////////////////////
      // SOCKET CREATION AND BINDING
      ////////////////////////////////////////////////////////////
  
      // Create a socket, notice that it is a user datagram socket (UDP)
      SOCKET in = socket(AF_INET, SOCK_DGRAM, 0);
  
      // Create a server hint structure for the server
      sockaddr_in serverHint;
      serverHint.sin_addr.s_addr = inet_addr(&quot;192.168.220.234&quot;); // Us any IP address available on the machine
      serverHint.sin_family = AF_INET; // Address format is IPv4
      serverHint.sin_port = htons(8001); // Convert from little to big endian
  
      // Try and bind the socket to the IP and port
      if (bind(in, (sockaddr*)&amp;serverHint, sizeof(serverHint)) == SOCKET_ERROR)
      {
          cout &lt;&lt; &quot;Can't bind socket! &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          return;
      }
  
      ////////////////////////////////////////////////////////////
      // MAIN LOOP SETUP AND ENTRY
      ////////////////////////////////////////////////////////////
  
      sockaddr_in client; // Use to hold the client information (port / ip address)
      int clientLength = sizeof(client); // The size of the client information
  
      char buf[1024];
  
      // Enter a loop
      while (true)
      {
          ZeroMemory(&amp;client, clientLength); // Clear the client structure
          ZeroMemory(buf, 1024); // Clear the receive buffer
  
          // Wait for message
          int bytesIn = recvfrom(in, buf, 1024, 0, (sockaddr*)&amp;client, &amp;clientLength);
          if (bytesIn == SOCKET_ERROR)
          {
              cout &lt;&lt; &quot;Error receiving from client &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
              continue;
          }
  
          // Display message and client info
          char clientIp[256]; // Create enough space to convert the address byte array
          ZeroMemory(clientIp, 256); // to string of characters
  
          // Convert from byte array to chars
          inet_ntop(AF_INET, &amp;client.sin_addr, clientIp, 256);
  
          // Display the message / who sent it
          cout &lt;&lt; &quot;Message recv from &quot; &lt;&lt; clientIp &lt;&lt; &quot; : &quot; &lt;&lt; buf &lt;&lt; endl;
      }
  
      // Close socket
      closesocket(in);
  
      // Shutdown winsock
      WSACleanup();
  }
  </code></pre>
  <p>and this is client code...</p>
  <p>Client.CPP</p>
  <pre><code>/*All right reserved to awinsyspro.com 2019*/
  /* UDP Server Sample program*/
  
  #include &lt;Windows.h&gt;
  #include &lt;winsock.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;cstring&gt;
  using namespace std;
  int main()
  {
      cout &lt;&lt; &quot;\t\t------UDP Client------&quot; &lt;&lt; endl;
      cout &lt;&lt; endl;
      // Local Variable
      WSADATA   WinSockData;
      int       iWsaStartup;
      int       iWsaCleanup;
  
      SOCKET               UDPSocketClient;
      struct  sockaddr_in  UDPServer;
  
  
      // STEP-1 Initialization of Winsock
  
      iWsaStartup = WSAStartup(MAKEWORD(2, 2), &amp;WinSockData);
      if (iWsaStartup != 0)
      {
          cout &lt;&lt; &quot;WSAStartup Failed = &quot; &lt;&lt; iWsaStartup &lt;&lt; endl;
      }
      cout &lt;&lt; &quot;WSAStartup Success&quot; &lt;&lt; endl;
  
      // STEP-2 Fill the UDPServer Structure
      UDPServer.sin_family = AF_INET;
      UDPServer.sin_addr.s_addr = inet_addr(&quot;192.168.220.234&quot;);
      UDPServer.sin_port = htons(8001);
  
      // STEP-3 Socket Creation
  
      UDPSocketClient = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
      if (UDPSocketClient == INVALID_SOCKET)
      {
          cout &lt;&lt; &quot;Socket Creation Failed &quot; &lt;&lt; endl;
  
          cout &lt;&lt; &quot;Error No-&gt; &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
      }
  
      cout &lt;&lt; &quot;UDP Socket Creation Success&quot; &lt;&lt; endl;
  
      int integ = 2656;
      //char Buffer[512] = strint;
      int  iCloseSocket;
      while (1)
      {
          //STEP-4 Sendto Fun.
          string strint = to_string(integ);
          const char* Buffer = strint.c_str();
          int iSendto;
  
          int  iBufferLen = strlen(Buffer) + 1;
          int  iUDPServerLen = sizeof(UDPServer);
         
          cout &lt;&lt; integ &lt;&lt; endl;
          iSendto = sendto(
              UDPSocketClient,
              Buffer,
              iBufferLen,
              MSG_DONTROUTE,
              (SOCKADDR*)&amp;UDPServer,
              sizeof(UDPServer));
  
          if (iSendto == SOCKET_ERROR)
          {
              cout &lt;&lt; &quot;Sending Data Failed &quot; &lt;&lt; endl;
              cout &lt;&lt; &quot;Error No-&gt;&quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
          }
          cout &lt;&lt; &quot;Sending Data Success&quot; &lt;&lt; endl;
          integ = integ + 1;
  
          // STEP-5 CloseSocket Function
      }
      iCloseSocket = closesocket(UDPSocketClient);
      if (iCloseSocket == SOCKET_ERROR)
      {
          cout &lt;&lt; &quot;Socket Closing failed &quot; &lt;&lt; endl;
  
          cout &lt;&lt; &quot;Error No-&gt; &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
      }
  
      cout &lt;&lt; &quot;Close Socket Success&quot; &lt;&lt; endl;
  
      // STEP-6 WSACleanUp fun for Terminating the Winsock DLL
  
      iWsaCleanup = WSACleanup();
      if (iWsaCleanup == SOCKET_ERROR)
      {
          cout &lt;&lt; &quot;WSA CleanUp failed &quot; &lt;&lt; endl;
  
          cout &lt;&lt; &quot;Error No-&gt; &quot; &lt;&lt; WSAGetLastError() &lt;&lt; endl;
      }
      cout &lt;&lt; &quot;Cleanup Success&quot; &lt;&lt; endl;
  
      system(&quot;PAUSE&quot;);
      return 0;
  }
  </code></pre>