recv() with MSG_DONTWAIT flag set doesn't recieve data from TCP socket

3.5k views Asked by At

I am in the process of writing a basic server and client chatting application in FreeBSD c. Basically multiple clients connect to the server on multiple ports (Ex. ports 4, 5, 6, 7), those ports are stored in an array which is constantly iterated through looking for data sent over by the corresponding clients. I am using recv(port#, buffer, sizeof(buffer), MSG_DONTWAIT), in order to support non blocking of the ports. This method works when I use the read() method with blocking, but it is not working when I use the recv method with the non blocking, more specifically it will receive data from the first port in the array but not the other ones. Logically I think my code works, Im just looking for any insight on possible problems with recv, or if anyone knows how to fix this.

void* chat_room(int chatid){
    printf("Entered Chatroom %d\n", chatid);
    int i = 0;
    int n;
    char buffer[150];
    while(1){

    int *array = create_chat_array(chatid);

           while(new_chatter != chatid){
            i = 0;
            while(array[i] != -1){

                n = recv(array[i], buffer, sizeof(buffer), MSG_DONTWAIT);

                    if(n > 0){
                            printf("Recieved %s, from %d\n", buffer, array[i]);
                    }


                i++;
            }
    }

}

}

This is the code for the chatroom, Basically it iterates through the array and tries to read. It always reads from array[0] but not from any of the other ones. Any insight would be helpful

0

There are 0 answers