C, socket programming: Connecting multiple clients to server using select()

后端 未结 5 2004
有刺的猬
有刺的猬 2021-02-02 00:23

I\'m trying to make a server that can be connected to by multiple clients. Here\'s my code so far:

Client:

int main(int argc, char **argv) {

  struct so         


        
5条回答
  •  轮回少年
    2021-02-02 01:13

    I replaced the else with the below and it works

     } else {
    /*                  int messageLength = 5;
                        char message[messageLength+1];
                        int in, index = 0, limit = messageLength+1;
    
                        memset ( &message[index] , 0, sizeof ( message [index] ) );
    
                        while ((in = recv(i, &message[index], limit, 0)) > 0) {
                            index += in;
                            limit -= in;
                        }
    
                        printf("%d\n", index);
                        printf("%s\n", message);
    */
                        bzero(buf, sizeof(buf));
                        if ((rval = read(i, buf, 1024)) < 0)
                            perror("reading stream message");
                        else if (rval == 0)
                            printf("Ending connection\n");
                        else
                            printf("-->%s\n", buf);
    
                    }
    

提交回复
热议问题