Note that there are some explanatory texts on larger screens.

plurals
  1. POSending data to multiple clients connected on my Server
    text
    copied!<p>I have this server app where clients can connect to. Now i want that when the clients are connected i can send data to all of them. I manage to do it when i connect my two clients.. what i send is received by my two clients. but my problem is when i connect client1 then send data to server, client1 can receive data then i connect client2 and i send data to the server. now when i send data from server to my clients only client1 can receive data but when i disconnect client 1 then client2 can receive data from the server.</p> <p>How can i make them work at the same time?.. Also how can i make my server accept messages at the same time from my client?..</p> <p>Here's the part of the code im having trouble.</p> <pre><code>for(j=0;j&lt;MAX_CLIENTS; j++) Clients[j].connected_sock = -1; do { fduse = fdin; printf("Waiting for Connection\n"); err = select(sMax + 1, &amp;fduse, NULL, NULL, NULL); if (err &lt; 0) { perror(" select() failed"); break; } DescRead = err; for (SockStorage=0; SockStorage &lt;= sMax &amp;&amp; DescRead &gt; 0; ++SockStorage) { if (FD_ISSET(SockStorage, &amp;fduse)) { DescRead -= 1; if (SockStorage == socketFd) { printf(" Listening socket is readable\n"); do { NewSFD = accept(socketFd,(struct sockaddr *) &amp;cli_addr, &amp;clilen); if (NewSFD &lt; 0) { if (errno != EWOULDBLOCK) { perror(" accept() failed"); DCSERVER = TRUE; } break; } if(ClientCount &lt; MAX_CLIENTS){ for(loop = 0; loop &lt;MAX_CLIENTS; loop++){ if(Clients[loop].connected_sock&lt;0){ Clients[loop].connected_sock = NewSFD; break; } } ClientCount++; } else { printf("Maximum Client Reached.\n"); char *sendtoclient = "Server full. "; send(NewSFD, sendtoclient, strlen(sendtoclient),0); close(NewSFD); break; } ip = ntohl(cli_addr.sin_addr.s_addr); printf(" Connection from %d.%d.%d.%d\n", (int)(ip&gt;&gt;24)&amp;0xff, (int)(ip&gt;&gt;16)&amp;0xff, (int)(ip&gt;&gt;8)&amp;0xff, (int)(ip&gt;&gt;0)&amp;0xff); dlogs(ip); FD_SET(NewSFD, &amp;fdin); if (NewSFD &gt; sMax) sMax = NewSFD; } while (NewSFD != -1); } else { int d; for(d=0; d&lt;MAX_CLIENTS; d++){ printf("Descriptor ID: %d\n", Clients[d].connected_sock); } pfds[0].fd = fd; pfds[0].events = POLLIN; pfds[1].fd = SockStorage; pfds[1].events = POLLIN; state = FALSE; do { rc = poll(pfds, 2, -1); if (pfds[0].revents &amp; POLLIN) { while ((nbytes = read(fd, buf, sizeof(buf)-1)) &gt; 0) { buf[nbytes] = '\0'; printf("%s\n", buf); } pfds[0].events = 0; pfds[1].events = POLLIN | POLLOUT; } if (pfds[1].revents &amp; POLLIN) { err = recv(SockStorage, strbuf, sizeof(strbuf), 0); if (err &lt; 0) { if (errno != EWOULDBLOCK) { perror(" recv() failed"); state = TRUE; } break; } if (err == 0) { printf(" Connection closed\n"); state = TRUE; break; } dSize = err; printf(" %d bytes received\n", dSize); } if (pfds[1].revents &amp; POLLOUT) { int s; for(s=0; s&lt;MAX_CLIENTS; s++){ if(Clients[s].connected_sock&gt;0){ err = send(Clients[s].connected_sock, buf, strlen(buf), 0); if (err &lt; 0) { perror(" send() failed"); state = TRUE; break; } } } pfds[0].events = POLLIN; pfds[1].events = POLLIN; } } while (TRUE); </code></pre> <p>Here's how im sending data to my clients.</p> <pre><code> int s; for(s=0; s&lt;MAX_CLIENTS; s++){ if(Clients[s].connected_sock&gt;0){ err = send(Clients[s].connected_sock, buf, strlen(buf), 0); if (err &lt; 0) { perror(" send() failed"); state = TRUE; break; } } } </code></pre> <p>Thanks,</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload