title: error C2664: 'errno_t strcpy_s(char *,rsize_t,const char *)' : cannot convert parameter 2 from 'char [512]' to 'rsize_t'
body: <p>Code:</p>
  
  <pre><code> #define WIN32_LEAN_AND_MEAN
  
  #include &lt;WinSock2.h&gt;
  #include &lt;WS2tcpip.h&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  // link with Ws2_32.lib
  #pragma comment(lib, "Ws2_32.lib")
  
  #define DEFAULT_PORT "27015" 
  #define DEFAULT_BUFFER_LENGTH   512
  
  class Client {
  public:
      Client(char* servername)
      {
          szServerName = servername;
          ConnectSocket = INVALID_SOCKET;
      }
  
      bool Start() {
          WSADATA wsaData;
  
          // Initialize Winsock
          int iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
          if(iResult != 0)
          {
              printf("WSAStartup failed: %d\n", iResult);
              return false;
          }
  
          struct addrinfo *result = NULL,
                          *ptr = NULL,
                          hints;
  
          ZeroMemory(&amp;hints, sizeof(hints));
          hints.ai_family = AF_UNSPEC;        
          hints.ai_socktype = SOCK_STREAM;    
          hints.ai_protocol = IPPROTO_TCP;
  
          // Resolve the server address and port
          iResult = getaddrinfo(szServerName, DEFAULT_PORT, &amp;hints, &amp;result);
          if (iResult != 0)
          {
              printf("getaddrinfo failed: %d\n", iResult);
              WSACleanup();
              return false;
          }
  
          ptr = result;
  
          // Create a SOCKET for connecting to server
          ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol);
  
          if (ConnectSocket == INVALID_SOCKET)
          {
              printf("Error at socket(): %d\n", WSAGetLastError());
              freeaddrinfo(result);
              WSACleanup();
              return false;
          }
  
          // Connect to server
          iResult = connect(ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen);
  
          if (iResult == SOCKET_ERROR)
          {
              closesocket(ConnectSocket);
              ConnectSocket = INVALID_SOCKET;
          }
  
          freeaddrinfo(result);
  
          if (ConnectSocket == INVALID_SOCKET)
          {
              printf("Unable to connect to server!\n");
              WSACleanup();
              return false;
          }
  
          return true;
      };
  
      // Free the resouces
      void Stop() {
          int iResult = shutdown(ConnectSocket, SD_SEND);
  
          if (iResult == SOCKET_ERROR)
          {
              printf("shutdown failed: %d\n", WSAGetLastError());
          }
  
          closesocket(ConnectSocket);
          WSACleanup();
      };
  
      // Send message to server
      bool Send(char* szMsg)
      {
  
          int iResult = send(ConnectSocket, szMsg, strlen(szMsg), 0);
  
          if (iResult == SOCKET_ERROR)
          {
              printf("send failed: %d\n", WSAGetLastError());
              Stop();
              return false;
          }
  
          return true;
      };
  
      // Receive message from server
      bool Recv()
      {
          char recvbuf[DEFAULT_BUFFER_LENGTH];
          int iResult = recv(ConnectSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);
  
          if (iResult &gt; 0)
          {
              char msg[DEFAULT_BUFFER_LENGTH];
              memset(&amp;msg, 0, sizeof(msg));
              strcpy_s(msg, recvbuf, iResult);
  
              printf("Received: %s\n", msg);
  
              return true;
          }
  
  
          return false;
      }
  
  private:
      char* szServerName;
      SOCKET ConnectSocket;
  };
  
  
  int main(int argc, CHAR* argv[]) 
  {
      std::string msg;
  
      Client client("127.0.0.1");
  
      if (!client.Start())
          return 1;
  
      while(true)
      {
          std::cout &lt;&lt; "Send: ";
          std::getline(std::cin, msg);
  
          // Close the connection when user enter "close"
          if (msg.compare("close") == 0)
          {
              break;
          }
  
          client.Send((char*)msg.c_str());
          client.Recv();
      }
  
      client.Stop();
  
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  
  #include &lt;WinSock2.h&gt;
  #include &lt;WS2tcpip.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  // link with Ws2_32.lib
  #pragma comment(lib, "Ws2_32.lib")
  
  #define DEFAULT_PORT "27015"
  #define DEFAULT_BUFFER_LENGTH   512
  
  int main() {
  
      WSADATA wsaData;
  
      // Initialize Winsock
      int iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if(iResult != 0)
      {
          printf("WSAStartup failed: %d\n", iResult);
          return 1;
      }
  
      struct addrinfo *result = NULL,
                      hints;
  
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_INET;      // Internet address family is unspecified so that either an IPv6 or IPv4 address can be returned
      hints.ai_socktype = SOCK_STREAM;    // Requests the socket type to be a stream socket for the TCP protocol
      hints.ai_protocol = IPPROTO_TCP;
      hints.ai_flags = AI_PASSIVE;
  
      // Resolve the local address and port to be used by the server
      iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
      if (iResult != 0)
      {
          printf("getaddrinfo failed: %d\n", iResult);
          WSACleanup();
          return 1;
      }
  
      SOCKET ListenSocket = INVALID_SOCKET;
  
      // Create a SOCKET for the server to listen for client connections
      ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
  
      if (ListenSocket == INVALID_SOCKET)
      {
          printf("Error at socket(): %d\n", WSAGetLastError());
          freeaddrinfo(result);
          WSACleanup();
          return 1;
      }
  
      // Setup the TCP listening socket
      iResult = bind(ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);
  
      if (iResult == SOCKET_ERROR)
      {
          printf("bind failed: %d", WSAGetLastError());
          freeaddrinfo(result);
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      freeaddrinfo(result);
  
      // To listen on a socket
      if ( listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
      {
          printf("listen failed: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      SOCKET ClientSocket;
  
      ClientSocket = INVALID_SOCKET;
  
      // Accept a client socket
      ClientSocket = accept(ListenSocket, NULL, NULL);
  
      if (ClientSocket == INVALID_SOCKET)
      {
          printf("accept failed: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      char recvbuf[DEFAULT_BUFFER_LENGTH];
      int iSendResult;
  
      // reveice until the client shutdown the connection
      do {
          iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);
          if (iResult &gt; 0)
          {
              char msg[DEFAULT_BUFFER_LENGTH];
              memset(&amp;msg, 0, sizeof(msg));
              strcpy_s(msg, recvbuf, iResult);
  
              printf("Received: %s\n", msg);
  
              iSendResult = send(ClientSocket, recvbuf, iResult, 0);
  
              if (iSendResult == SOCKET_ERROR)
              {
                  printf("send failed: %d\n", WSAGetLastError());
                  closesocket(ClientSocket);
                  WSACleanup();
                  return 1;
              }
  
              printf("Bytes sent: %ld\n", iSendResult);
          }
          else if (iResult == 0)
              printf("Connection closed\n");
          else
          {
              printf("recv failed: %d\n", WSAGetLastError());
              closesocket(ClientSocket);
              WSACleanup();
              //return 1;
          }
      } while (iResult &gt; 0);
  
      // Free the resouces
      closesocket(ListenSocket);
      WSACleanup();
  
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>Errors:</p>
  
  <blockquote>
    <p>error C2664: 'errno_t strcpy_s(char *,rsize_t,const char *)' : cannot
    convert parameter 2 from 'char [512]' to 'rsize_t'</p>
    
    <p>error C2664: 'errno_t strcpy_s(char *,rsize_t,const char *)' : cannot
    convert parameter 2 from 'char [512]' to 'rsize_t'</p>
    
    <p>IntelliSense: no instance of overloaded function "strcpy_s" matches
    the argument list
                argument types are: (char [512], char [512], int)</p>
  </blockquote>
  
  <hr>
  
  <h2>I have been trying to fix this problem. Please help.</h2>
  
  <p>New code:</p>
  
  <pre><code>#define WIN32_LEAN_AND_MEAN
  
  #include &lt;WinSock2.h&gt;
  #include &lt;WS2tcpip.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  
  // link with Ws2_32.lib
  #pragma comment(lib, "Ws2_32.lib")
  
  #define DEFAULT_PORT "27015"
  #define DEFAULT_BUFFER_LENGTH   128
  
  int main() {
  
      WSADATA wsaData;
  
      // Initialize Winsock
      int iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if(iResult != 0)
      {
          printf("WSAStartup failed: %d\n", iResult);
          return 1;
      }
  
      struct addrinfo *result = NULL,
                      hints;
  
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_INET;      // Internet address family is unspecified so that either an IPv6 or IPv4 address can be returned
      hints.ai_socktype = SOCK_STREAM;    // Requests the socket type to be a stream socket for the TCP protocol
      hints.ai_protocol = IPPROTO_TCP;
      hints.ai_flags = AI_PASSIVE;
  
      // Resolve the local address and port to be used by the server
      iResult = getaddrinfo(NULL, DEFAULT_PORT, &amp;hints, &amp;result);
      if (iResult != 0)
      {
          printf("getaddrinfo failed: %d\n", iResult);
          WSACleanup();
          return 1;
      }
  
      SOCKET ListenSocket = INVALID_SOCKET;
  
      // Create a SOCKET for the server to listen for client connections
      ListenSocket = socket(result-&gt;ai_family, result-&gt;ai_socktype, result-&gt;ai_protocol);
  
      if (ListenSocket == INVALID_SOCKET)
      {
          printf("Error at socket(): %d\n", WSAGetLastError());
          freeaddrinfo(result);
          WSACleanup();
          return 1;
      }
  
      // Setup the TCP listening socket
      iResult = bind(ListenSocket, result-&gt;ai_addr, (int)result-&gt;ai_addrlen);
  
      if (iResult == SOCKET_ERROR)
      {
          printf("bind failed: %d", WSAGetLastError());
          freeaddrinfo(result);
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      freeaddrinfo(result);
  
      // To listen on a socket
      if ( listen(ListenSocket, SOMAXCONN) == SOCKET_ERROR)
      {
          printf("listen failed: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      SOCKET ClientSocket;
  
      ClientSocket = INVALID_SOCKET;
  
      // Accept a client socket
      ClientSocket = accept(ListenSocket, NULL, NULL);
  
      if (ClientSocket == INVALID_SOCKET)
      {
          printf("accept failed: %d\n", WSAGetLastError());
          closesocket(ListenSocket);
          WSACleanup();
          return 1;
      }
  
      char recvbuf[DEFAULT_BUFFER_LENGTH];
      int iSendResult;
  
      // reveice until the client shutdown the connection
      do {
          iResult = recv(ClientSocket, recvbuf, DEFAULT_BUFFER_LENGTH, 0);
          if (iResult &gt; 0)
          {
              char msg[DEFAULT_BUFFER_LENGTH];
              memset(&amp;msg, 0, sizeof(msg));
              strcpy_s(msg, iResult, recvbuf);
  
              printf("Received: %s\n", msg);
  
              iSendResult = send(ClientSocket, recvbuf, iResult, 0);
  
              if (iSendResult == SOCKET_ERROR)
              {
                  printf("send failed: %d\n", WSAGetLastError());
                  closesocket(ClientSocket);
                  WSACleanup();
                  return 1;
              }
  
              printf("Bytes sent: %ld\n", iSendResult);
          }
          else if (iResult == 0)
              printf("Connection closed\n");
          else
          {
              printf("recv failed: %d\n", WSAGetLastError());
              closesocket(ClientSocket);
              WSACleanup();
              //return 1;
          }
      } while (iResult &gt; 0);
  
      // Free the resouces
      closesocket(ListenSocket);
      WSACleanup();
  
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>New error:</p>
  
  <p><a href="http://i.stack.imgur.com/55IUF.png" rel="nofollow">http://i.stack.imgur.com/55IUF.png</a></p>