title: how to work with c++ sockets over the internet?
body: <p>So I've been learning about socket programming in c++ and I can successfully create a basic client-server socket application that works on a local machine but I don't know how to get it to work over the internet like creating a chat application or something similar, I saw people talking about UDP wall punching to use internet sockets but I can't seem to find any information or tutorial on how to do such thing.  </p>
  
  <p>Server Script</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;string&gt;
  
  #pragma comment(lib, "Ws2_32.lib")
  
  using namespace std;
  
  int main(){
      cout &lt;&lt; "&gt;&gt;&gt;WINSOCK SEREVER\n";
  
      WSADATA wsaData;
      WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
  
      addrinfo hints;
      memset(&amp;hints, 0, sizeof(hints));
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
      hints.ai_flags = AI_PASSIVE;
  
  
      addrinfo *Results = NULL;
      if (!getaddrinfo("41.143.215.7", "255", &amp;hints, &amp;Results)){
          cout &lt;&lt; "-&gt;Ready To Create SOCKET and bind Address\n";
          SOCKET listenSocket = INVALID_SOCKET;
          listenSocket = socket(Results-&gt;ai_family, Results-&gt;ai_socktype, Results-&gt;ai_protocol);
          if (listenSocket != INVALID_SOCKET){
              cout &lt;&lt; "-&gt;Socket is created...";
              if (bind(listenSocket, Results-&gt;ai_addr, Results-&gt;ai_addrlen) != SOCKET_ERROR){
                  cout &lt;&lt; "and bind successfuly\n [" &lt;&lt; getsockname(listenSocket, Results-&gt;ai_addr, (int *)Results-&gt;ai_addrlen) &lt;&lt; "]\n";
                  if (!listen(listenSocket, SOMAXCONN)){
                      cout &lt;&lt; "-&gt;waiting for Client to connect";
                      SOCKET clientSocket = INVALID_SOCKET;
                      clientSocket = accept(listenSocket, NULL, NULL);
                      if (clientSocket != INVALID_SOCKET){
                          cout &lt;&lt; "-&gt;Client Has Connected\nWaiting for Messages...";
                          int iResult = 1;
                          while(true){
                              char BufferRcv[9000];
                              iResult = recv(clientSocket, BufferRcv, 9000, 0);
                              if (iResult &gt; 0){
                                  cout &lt;&lt; "\nBuffer Received : \n" &lt;&lt; BufferRcv;
                              }   
                          }
                      }
  
                  }
              }
              else { cout &lt;&lt; "Error Binding Socket!!!"; }
  
          }
  
      }
  
  
  
      cin.get();
      return 0;
  }
  </code></pre>
  
  <p>Client Script </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;winsock2.h&gt;
  #include &lt;ws2tcpip.h&gt;
  #include &lt;string&gt;
  
  #pragma comment(lib, "Ws2_32.lib")
  
  using namespace std;
  
  int main(){
      cout &lt;&lt; "&gt;&gt;&gt;WINSOCK CLIENT\n";
      WSADATA wsaData;
      WSAStartup(MAKEWORD(2, 2), &amp;wsaData);
  
      addrinfo hints;
      memset(&amp;hints, 0, sizeof(hints));
      hints.ai_family = AF_UNSPEC;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
  
      addrinfo *Results = NULL;
  
      if (!getaddrinfo("41.143.215.79", "255", &amp;hints, &amp;Results)){
          cout &lt;&lt; "-&gt;Ready To Create SOCKET and connect to SERVER\n";
          SOCKET connectSocket = INVALID_SOCKET;
          connectSocket = socket(Results-&gt;ai_family, Results-&gt;ai_socktype, Results-&gt;ai_protocol);
          if (connectSocket != INVALID_SOCKET){
              cout &lt;&lt; "-&gt;Created Socket Successfuly...connecting to server\n";
              if (connect(connectSocket, Results-&gt;ai_addr, Results-&gt;ai_addrlen) != SOCKET_ERROR){
                  cout &lt;&lt; "-&gt;Connected To Server Successfuly\n";
                  string Message;
                  while (true){
                      cout &lt;&lt; "\n-&gt;Enter A Message :";
                      getline(cin, Message);          
                      if (send(connectSocket, Message.c_str(), sizeof(Message), 0) != SOCKET_ERROR){
                          cout &lt;&lt; "MESSAGE IS SENT\n";
                      }
                  }
              }
          }
      }
  
  
  
      cin.get();
      return 0;
  }
  </code></pre>