title: send() to specific client C WInSock
body: <p>How would I send to a specific client when I have multiple clients connected. I've thought of <code>sendto();</code> But I run all of them on the same socket and i am unsure of a way to store their addr for <code>sendto()</code>. SO maybe my way of accepting multiple clients isn't very good? </p>
  
  <p>My Server Code:</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;stdio.h&gt;
  #include &lt;winsock.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;conio.h&gt;
  #pragma comment(lib,"ws2_32.lib") //Winsock Library
  #define _WIN32_WINNIT 0x0500
  #include &lt;windows.h&gt;
  WSADATA wsa;
  SOCKET sock, newsock;
  int c;
  int clientnum;
  struct sockaddr_in server, client;
  
  DWORD WINAPI ProcessClient(LPVOID lpParam) {
      SOCKET newSock = (SOCKET)lpParam;
      // Send and receive data.
      char buf[256];
      char newbuf[256];
      char cnumchar[5];
      strcpy(buf, "Hello Client #: ");
      itoa(clientnum, cnumchar, 10);
      strcat(buf, cnumchar);
      sendto(newSock, buf, sizeof(buf), 0, NULL, NULL);
      char sendbuf[256];
      strcpy(sendbuf, "a");
      while (1)
      {
          if (recv(newSock, newbuf, sizeof(newbuf), 0) == 0 || recv(newSock, newbuf, sizeof(newbuf), 0) == -1)  {
              printf("\nClient disconnected");
              clientnum--;
          }
          else if (send(newSock, sendbuf, sizeof(sendbuf), 0) == 0 || send(newSock, sendbuf, sizeof(sendbuf), 0) == -1) {
              printf("\nClient Disconnected!");
              clientnum--;
          }
  
  
  
      }
  
  }
  
  int main()
  {
  
      printf("Initializing Winsock...\n");
      int ret = WSAStartup(MAKEWORD(2, 2), &amp;wsa);
      if (ret != 0)
      {
          printf("Initialization Failed. Error: %d", ret);
          return 1;
      }
      printf("Initialized.\n");
  
      sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      if (sock == INVALID_SOCKET) {
          printf("Could not create socket! Error: %d\n", WSAGetLastError());
          return 1;
      }
      //textcolor(2);
      printf("Socket Created!\n");
  
      memset(&amp;server, 0, sizeof(server));
      server.sin_family = AF_INET;
      server.sin_addr.s_addr = INADDR_ANY;
      server.sin_port = htons(3939);
  
      //bind
      if (bind(sock, (struct sockaddr *)&amp;server, sizeof(server)) == SOCKET_ERROR) {
          printf("Bind failed! Error: %d\n", WSAGetLastError());
          closesocket(sock);
          getch();
          return 1;
      }
      printf("Binded!\n");
  
      // listen
      if (listen(sock, 1) == SOCKET_ERROR) {
          printf("Listen failed! Error: %d\n", WSAGetLastError());
          closesocket(sock);
          return 1;
      }
      printf("Now Listening...\n");
      do {
          newsock = SOCKET_ERROR;
          do {
              newsock = accept(sock, NULL, NULL);
          } while (newsock == SOCKET_ERROR);
          printf("Client Connected!");
          DWORD dwThreadId;
          CreateThread(NULL, 0, ProcessClient, (LPVOID)newsock, 0, &amp;dwThreadId);
          clientnum++;
      } while (true);
  </code></pre>
  
  <p>return 0;
      }</p>