cunixclientrecvposix-select

Select() send and receive with same socket descriptor


I am wanting to use select to receive and send on a client/server on the same socket descriptor (serverside).

timestruct* myTime;
sockfd = accept(listeningFd, 0, 0);

while(1)
    FD_ZERO(&my_fd_set)
    maxFd = sockfd
    FD_ZERO(&my_fd_set);
    FD_SET(sockfd, &my_fd_set);
    select(maxFd+1, &my_fd_set, &my_fd_set, NULL, myTime);
    
    for (j=0; j<=maxFd; j++)

    if(FD_ISSET(j, &temp_fd_set))
        if(j==sockfd)
            send()
        if(j==sockfd)
            recv()

This is essentially what I want to do. Obviously this won't work because sockfd is going to be the same value for sending and receiving. Is there a way I can do this without using fork()?? Currently I have a blocking recv and send but the server could be required to recv multiple commands while another command is being processed to send back to the client. I am very new to c and also 'select()'. Because select has the three fd_set options (read, write, execute) I thought maybe I could do this.


Solution

  • Use different sets for the rfds and wfds parameters to select, so you can distinguish when sockfd is in one set but not the other.

    fd_set rfds;
    fd_set wfds;
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);
    
    FD_SET(sockfd, &rfds);
    FD_SET(sockfd, &wfds);
    if(select(sockfd + 1, &rfds, &wfds, NULL, myTime) < 0) {
        perror("select");
        return -1;
    }
    
    if(FD_ISSET(sockfd, &rfds))
        recv();
    if(FD_ISSET(sockfd, &wfds))
        send();