title: "MFC/C++ Socket programming.." How to connect server and client?
body: <p>I used a code for MFC/C++  socket programming but it's only working when I make the server and client on the same PC , but when I use the client  on different PC ,it fails to find the server and the connection is failed . 
  I don't know if the problem with the local  IP  of the server I use or with the code any help please :) ! </p>
  
  <p>the following code is server side :-  </p>
  
  <pre><code>#include &lt;afx.h&gt;
  #include &lt;afxext.h&gt;
  #include &lt;afxsock.h&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main() 
  {
      AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
      AfxSocketInit();
      CSocket serverSocket;
      serverSocket.Create(3333);
      serverSocket.Listen();
      CSocket clientSocket;
  
      while(serverSocket.Accept(clientSocket))
      {
          CString s;
  
          while(s!="bye")
          {
              char msg[128];
  
              if(clientSocket.Receive(msg, 128)&lt;0)break;
  
              s = msg;
              cout&lt;&lt;"Client: "&lt;&lt;msg&lt;&lt;endl;
              sprintf_s(msg, 128, "Your msg (%d letter) arrived successfully.",
              strlen(msg));
              clientSocket.Send(msg, 128);
  
              if(s=="shutdown")exit(0);
          }
  
          clientSocket.Close();
      }
  
      return 0;
  }
  </code></pre>
  
  <p>the following code for client side :-</p>
  
  <pre><code> #include &lt;afx.h&gt;
   #include &lt;afxext.h&gt;
   #include &lt;afxsock.h&gt;
   #include &lt;iostream&gt;
  
   using namespace std;
  
  int main()
  {
      AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0);
      AfxSocketInit();
      CSocket clientSocket;
      clientSocket.Create();
  
      if(clientSocket.Connect("192.168.1.2", 3333))
      {
          cout&lt;&lt;"Connected to server."&lt;&lt;endl;
          CString s;
  
          while(s!="bye" &amp;&amp; s!="shutdown")
          {
              char msg[128];
              cin.getline(msg, 128);
              s = msg;
              clientSocket.Send(msg, 128);
  
              if(clientSocket.Receive(msg, 128)&lt;0)break;
  
              cout&lt;&lt;msg&lt;&lt;endl;
          }
      }
      else
      {
         cout&lt;&lt;"Cannot find server."&lt;&lt;endl;
      }
  
      return 0;
  }
  </code></pre>