c unix- Select() send and receive with same socket descriptor

Posted by RileyVanZeeland on Stack Overflow See other posts from Stack Overflow or by RileyVanZeeland
Published on 2012-09-19T03:32:31Z Indexed on 2012/09/19 3:37 UTC
Read the original article Hit count: 167

Filed under:
|
|
|

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 knew to c and also 'select()'. Because select has the three fd_set options (read, write, execute) I thought maybe I could do this.

Thank you.

© Stack Overflow or respective owner

Related posts about select

Related posts about client