title: sent a message to all clients with c programming
body: <p>İ am trying make a chat program.Server will get a msg and then sent all other clients.</p>
  
  <p>There are no problem to get message from client.But when it came <em>SendToAll</em> function doesnt work.</p>
  
  <p>thanks your help</p>
  
  <p><strong>SERVER</strong></p>
  
  <pre><code>    #include &lt;stdio.h&gt;
      #include &lt;string.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;sys/socket.h&gt;
      #include &lt;sys/types.h&gt;
      #include &lt;netinet/in.h&gt;
      #include &lt;errno.h&gt;
      #include &lt;unistd.h&gt;
      #include &lt;arpa/inet.h&gt;
  
  
      #define PORT 7777
      #define SIZE 1024
  
  
    struct clients{
       struct sockaddr_in clientAddr;
       struct clients *next;
   };
  
  
  
  
   struct clients *AddClient(struct sockaddr_in new_ClientAddr,struct clients *head)
   {
  
       struct clients *temp = head;
       puts("burda2");
       int a = 1;
       if(head == NULL)
       {
           temp = (struct clients *)malloc(sizeof(struct clients));
           temp-&gt;clientAddr = new_ClientAddr;
           temp-&gt;next = NULL;
       }
       else
       {
           while(temp != NULL)
           {
           if(temp-&gt;clientAddr.sin_addr.s_addr == new_ClientAddr.sin_addr.s_addr)
           {
          a = 0;
          break;
          }   
               temp = temp-&gt;next;
           }
           if(a == 1)
           {
               temp-&gt;next = (struct clients *)malloc(sizeof(struct clients));
              temp-&gt;next-&gt;clientAddr = new_ClientAddr;
              temp = temp-&gt;next;
              temp-&gt;next = NULL;
           }
           else{
               puts("Its already save");
           }
       }
       return head;
   }
     // --- End of Function AddClient() ---
  
  void SendToAll(char msg[1023], struct sockaddr_in repliedClient, struct clients *head)
   {   
      struct clients *temp = head;
      int clients_socket;
      int byte;
      clients_socket  = socket(AF_INET, SOCK_STREAM, 0);
          if(clients_socket == -1)
              perror("Error On Socket(SendToAll)");
      while(temp != NULL)
      {
  
         if(repliedClient.sin_addr.s_addr != temp-&gt;clientAddr.sin_addr.s_addr)//Dont send msg who to replied
          {
              if(connect(clients_socket, (struct sockaddr *)&amp;temp-&gt;clientAddr,
                                                  sizeof(struct sockaddr)) == -1)
              {
                  perror("Error on Connect(SendToAll)");
  
  
              byte = send(clients_socket, msg, strlen(msg), 0);
              printf("%s message send",msg);
  
              if(byte == -1)
                  perror("Error on Send(SendToAll");
              else if(byte == 0)
                  printf("Connection've been closed");
  
              temp = temp-&gt;next;
              }
          }
      }
  
   } 
   // --- End of Function SendToAll() ---
  
  
  
  int main(int argc, char *argv[])
  {
      struct clients *head = NULL;
      int socket_fd, temp_fd;
      struct sockaddr_in serverAddr,new_clientAddr;
      int structSize,byte;
      char text[1023];
  
      // Creating socket
      socket_fd = socket(AF_INET, SOCK_STREAM, 0);
      if(socket_fd == -1)
          perror("Error on Soket");
  
      // Editting Server socket
      serverAddr.sin_family = AF_INET;
      serverAddr.sin_port = htons(PORT);
      serverAddr.sin_addr.s_addr = INADDR_ANY;
      memset(&amp;(serverAddr.sin_zero), '\0', 8);
  
      // Bind
      if(bind(socket_fd, (struct sockaddr *)&amp;serverAddr, sizeof(struct sockaddr)) == -1)
          perror("Error on Bind");
  
     // Start listen the port
     if(listen(socket_fd, 20) == -1  )
          perror("Error on Listen");
  
      structSize = sizeof(new_clientAddr);
  
      while(1)
      {
          puts(" ");
          // Accept
          temp_fd = accept(socket_fd , (struct sockaddr *)&amp;new_clientAddr, &amp;structSize);
          if(temp_fd == -1)
              perror("Error on Accept");
          // Recv
          byte = recv(temp_fd, &amp;text, SIZE-1, 0);
          if(byte == -1)
              perror("Error on Recv");
          else if(byte == 0)
              printf("Connection is close\n");
          printf("%s", text);
          //Add to list 
          head = AddClient(new_clientAddr, head);
          //Send message to other clients
          SendToAll(text, new_clientAddr, head);
  
          close(temp_fd);    
      }
  
  
      close(socket_fd);
  
  
      return (EXIT_SUCCESS);
  }
  </code></pre>
  
  <p><strong>CLİENT</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sys/socket.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;netinet/in.h&gt;
  #include &lt;errno.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;arpa/inet.h&gt;
  
  //#define ServerIP "35.162.226.229"
  #define ServerIP "127.0.0.1"
  #define ServerPort 7777
  #define SIZE 1024
  
  
  
  
  
  int main(int argc, char** argv) {
  
      int socket_fd;
      struct sockaddr_in serverAddr;
      char text[SIZE],msg[SIZE-20], name[20]text temizlendi,get_msg[1024];
      int byte;
      // Nick
      printf("Please enter your nick"); 
      scanf("%s",name);
      //Create Socket
      socket_fd = socket(AF_INET, SOCK_STREAM, 0);
      if(socket_fd == -1)
          perror("Error on Socket");
  
      serverAddr.sin_family = AF_INET;
      serverAddr.sin_port = htons(ServerPort);
      serverAddr.sin_addr.s_addr =inet_addr(ServerIP);
      memset(&amp;(serverAddr.sin_zero), '\0', 8);
  
      // Connect to server
      if(connect(socket_fd, (struct sockaddr *)&amp;serverAddr, sizeof(struct sockaddr)) == -1)
          perror("Error on Connect");
      puts("Connected");
      while(1)
      {
          //Get message
          printf("&gt;&gt;&gt;"); scanf("%s",msg);
          //Edit message
          strcpy(text, name);
          strcat(text, " : ");
          strcat(text, msg);
          //Send message
          byte = send(socket_fd, text, strlen(text), 0);
          if(byte == -1)
              perror("Error on Send");
          else if(byte == 0)
              printf("Connection've been closed");
          //Get reply to other users
  
          recv(socket_fd, &amp;get_msg, SIZE-1, 0);
          printf("Getting message %s", get_msg);
      }
      //Close socket
      close(socket_fd);
  
  
  
      return (EXIT_SUCCESS);
  }
  </code></pre>