Enable non-blocking socket

三世轮回 提交于 2019-12-01 13:27:24

问题


I have a server written in C/C++. I set the wrapper for the connection as follow:

    //START WRAPPER
    void Server::init_address(int port)
    {
memset(&(this->serv_addr), 0, sizeof(this->serv_addr));
this->serv_addr.sin_family = AF_INET;
this->serv_addr.sin_port = htons(port);
this->serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    }


    int Server::w_socket()
    {
int retv;
retv = socket(PF_INET, SOCK_STREAM, 0);
//FIXME
//fcntl(retv, F_SETFL, O_NONBLOCK);
if(retv == -1)
{
    std::string err_msg(strerror(errno));
    err_msg = "[socket] " + err_msg;
    throw err_msg;
}
else
{
    int reuse_opt = 1;

    if(setsockopt(retv, SOL_SOCKET, SO_REUSEADDR, &reuse_opt, sizeof(int))==-1)
    {
        perror("setsockopt error");
        exit(1);
    }
    return retv;
}
    }

    void Server::w_bind()
    {
int retv;
retv = bind(this->sock_fd,
        (struct sockaddr*) &(this->serv_addr),
        sizeof(this->serv_addr));

if(retv == -1)
{
    std::string err_msg(strerror(errno));
    err_msg = "[bind] " + err_msg;
    throw err_msg;
}
    }

    void Server::w_listen()
    {
int retv;
retv = listen(this->sock_fd, 3);
if (retv == -1)
{
    std::string err_msg(strerror(errno));
    err_msg = "[listen] " + err_msg;
    throw err_msg;
}
    }

    int Server::w_accept(struct sockaddr_in* client_addr)
    {
int retv;
int socklen = sizeof(sockaddr_in);

retv = accept(this->sock_fd, (struct sockaddr*)client_addr, (socklen_t*)&socklen);
if(retv == -1)
{
    std::string err_msg(strerror(errno));
    err_msg = "[accept] " + err_msg;
    throw err_msg;
}
else
{
    return retv;
}
            }

    int Server::recvtimeout(int s, char *buf, int len, int timeout)
    {
fd_set fds;
int n;
struct timeval tv;
// set up the file descriptor set
FD_ZERO(&fds);
FD_SET(s, &fds);
// set up the struct timeval for the timeout
tv.tv_sec = timeout;
tv.tv_usec = 0;
// wait until timeout or data received
n = select(s+1, &fds, NULL, NULL, &tv);
if (n == 0){
    return -2; // timeout!
}
if (n == -1){
    return -1; // error
}
// data must be here, so do a normal recv()
return recv(s, buf, len, 0);
    }
    // END WRAPPER

My goal is to enable the non-blocking socket mode. I've tried to do fcntl(retv, F_SETFL, O_NONBLOCK); like Beej manual said, but I receive the error: [accept] Resource temporarily unavailable A solution to this problem is using the select function, but I already use it in the recvtimeout function, always like Beej guide said.

So, I don't know how to solve this problem to enable the non-blocking socket mode.


回答1:


You get the error because the socket is non-blocking.

Either you do a busy loop while you get that error (check errno when accept returns -1 for either EWOULDBLOCK or EAGAIN). The other and recommended solution is to use select to see when the socket is readable, then you can call accept.

Edit: How to do it with select

You need to have an event-loop at a higher level, that checks if the listening socket, or the connected socket(s), can be read from. If the listening socket is readable then you can accept a new connection, and if the connected sockets are readable then you can read from them.

Something like:

for (;;)
{
    fd_set readset;

    FD_ZERO(&readset); 
    FD_SET(listening_socket, &readset);
    int maxfd = listening_socket;

    if (connected_socket >= 0)
    {
        FD_SET(connected_socket, &readset);
        maxfd = max(maxfd, connected_socket);
    }

    // NULL timeout (5-th argument) means wait until event
    select(maxfd + 1, &readset, NULL, NULL, NULL);

    if (FD_ISSET(listening_socket, &readset))
    {
        accept_new_connection(listening_socket);
    }

    if (connected_socket >= 0 && FD_ISSET(connected_socket, &readset))
    {
        if (!read_from_socket(connected_socket))
        {
            close(connected_socket);
            connected_socket = -1;
        }
    }
}

If you have multiple connected sockets, put them in a simple linked list and add/check them in a loop. Remove from list when they are closed.




回答2:


In your solution, you are using select call for the connection socket, but you are not using the same for listen socket.

If you have changed the listen socket also to non-blocking, then you must use select for listen socket before calling accept.



来源:https://stackoverflow.com/questions/10794999/enable-non-blocking-socket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!