title: How to send a file to a server
body: <p>I want to make a program such that the user runs the client program and then writes the name of a file to be transferred to the server, the user should be able to set the server address. Then the server accepts the file and saves it to its working directory with the same name.</p>
  
  <p>I don't know how to do this.</p>
  
  <p>This is my code:</p>
  
  <p>Server.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;winsock2.h&gt;
  #pragma comment (lib, "ws2_32.lib")
  
  using namespace std;
  
  int main(int argc, _TCHAR* argv[])
  {
      WSAData wsaData;
      WORD DLLVersion = MAKEWORD(2,1);
      if (WSAStartup(DLLVersion, &amp;wsaData)) {
          cout &lt;&lt; "Error\n";
          exit(1);
      }
  
      SOCKADDR_IN addr;
      int sizeofaddr = sizeof(addr);
      addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      addr.sin_port        = htons(1111);
      addr.sin_family      = AF_INET;
  
      SOCKET sListen = socket(AF_INET, SOCK_STREAM, NULL);
      bind(sListen, (SOCKADDR*)&amp;addr, sizeof(addr));
      listen(sListen, SOMAXCONN);
  
      SOCKET newConnection;
      newConnection = accept(sListen, (SOCKADDR*)&amp;addr, &amp;sizeofaddr);
  
      if (newConnection == 0) cout &lt;&lt; "Error #2\n";
      else                cout &lt;&lt; "Client connected!\n";
  
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>Client.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;winsock2.h&gt;
  #pragma comment (lib, "ws2_32.lib")
  
  using namespace std;
  
  int main(int argc, _TCHAR* argv[])
  {
      WSAData wsaData;
      WORD DLLVersion = MAKEWORD(2,1);
      if (WSAStartup(DLLVersion, &amp;wsaData)) {
          cout &lt;&lt; "Error\n";
          exit(1);
      }
  
      SOCKADDR_IN addr;
      addr.sin_addr.s_addr = inet_addr("127.0.0.1");
      addr.sin_port        = htons(1111);
      addr.sin_family      = AF_INET;
  
      SOCKET Connection = socket(AF_INET, SOCK_STREAM, NULL);
      if (connect(Connection, (SOCKADDR*)&amp;addr, sizeof(addr)) != 0) {
          cout &lt;&lt; "Error: failed connect to server\n";
          return 1;
      }
  
      cout &lt;&lt; "Connected!\n";
  
      system("pause");
      return 0;
  }
  </code></pre>