[C++] Winsock + Select

11/30/2009 19:33 Flyff_Service#1
I solved this Problem adding a "continue;" at
PHP Code:
    if(FD_ISSET(aSocket, &fdSet))
    {
        
AddSocket(accept(aSocketNULLNULL));
        continue;
    } 
12/02/2009 08:49 InvincibleNoOB#2
is recv executed? Where does it stop - FD_ISSET(m_clients[i], &fdSet)? Give more info.
12/02/2009 13:18 Flyff_Service#3
Code:
while(1)
{
    char buf[256];
    long rc;
    FD_ZERO(&fdSet);
    FD_SET(aSocket, &fdSet);

    FOR_EACH(m_clients, i)
    {
        FD_SET(m_clients[i], &fdSet);
        FD_SET(m_servers[i], &fdSet);
    }

    select(0, &fdSet, NULL, NULL, NULL);

    if(FD_ISSET(aSocket, &fdSet))
    {
        AddSocket(accept(aSocket, NULL, NULL));
    }

    FOR_EACH(m_clients, i)
    {
        if(FD_ISSET(m_clients[i], &fdSet))
        {
[COLOR="Red"]            rc = recv(m_clients[i], buf, 256, 0);
            if(rc == 0 || rc == SOCKET_ERROR)
            {
                Close(i);
            }
            else
            {
                send(m_servers[i], buf, rc, 0);
            }[/COLOR]
        }

        if(FD_ISSET(m_servers[i], &fdSet))
        {
[COLOR="Red"]            rc = recv(m_servers[i], buf, 256, 0);
            if(rc == 0 || rc == SOCKET_ERROR)
            {
                Close(i);
            }
            else
            {
                send(m_clients[i], buf, rc, 0);
            }[/COLOR]
        }
    }
}
The red highlighted Code is never executed. So the if statement returns always false, but why?
12/02/2009 17:51 MrSm!th#4
is a client connected and accepted?
is wsastartup initialized?
12/02/2009 18:11 InvincibleNoOB#5
1)Why are you using select?
2)Why are you not checking return val of select()?
3)Problem(first execution):
Code:
    FD_ZERO(&fdSet);    //fdSet.fd_count = 0;
    FD_SET(aSocket, &fdSet);  //fdSet.fd_count = 1;(listening socket)

    FOR_EACH(m_clients, i) //this isn't executed. --> fdSet.fd_count = 1
    {
        FD_SET(m_clients[i], &fdSet);
        FD_SET(m_servers[i], &fdSet);
    }

    select(0, &fdSet, NULL, NULL, NULL); //fdSet contains only one socket,the Lsock
    if(FD_ISSET(aSocket, &fdSet))
    {
        AddSocket(accept(aSocket, NULL, NULL)); //fdSet contains Lsock and Connected sock
    }

    FOR_EACH(m_clients, i) //executed once
    {
        if(FD_ISSET(m_clients[i], &fdSet)) //false.Did you select the m_clients[0] socket? No,so it won't be executed.
        {
           ...
        }
12/02/2009 18:23 Flyff_Service#6
@MrSm!th:
WSAStartup -> Yes
Client connected and accepted -> yes

1) Because i want to handle many clients
2) I'll check it now (wait please :P)
3) Should i add another select? Or how could i resolve the problem?
12/02/2009 18:25 MrSm!th#7
Quote:
Originally Posted by InvincibleNoOB View Post
1)Why are you using select?
2)Why are you not checking return val of select()?
3)Problem(first execution):
Code:
    FD_ZERO(&fdSet);    //fdSet.fd_count = 0;
    FD_SET(aSocket, &fdSet);  //fdSet.fd_count = 1;(listening socket)

    FOR_EACH(m_clients, i) //this isn't executed. --> fdSet.fd_count = 1
    {
        FD_SET(m_clients[i], &fdSet);
        FD_SET(m_servers[i], &fdSet);
    }

    select(0, &fdSet, NULL, NULL, NULL); //fdSet contains only one socket,the Lsock
    if(FD_ISSET(aSocket, &fdSet))
    {
        AddSocket(accept(aSocket, NULL, NULL)); //fdSet contains Lsock and Connected sock
    }

    FOR_EACH(m_clients, i) //executed once
    {
        if(FD_ISSET(m_clients[i], &fdSet)) //false.Did you select the m_clients[0] socket? No,so it won't be executed.
        {
           ...
        }
cause select is a goog way to do that ;)
12/02/2009 18:36 Flyff_Service#8
Edit:
The recv Function is blocking the whole while loop. How to resolve this problem?

Thanks for your Help :)
12/02/2009 20:01 InvincibleNoOB#9
Perhaps use WSAAsyncSelect() or WSAEventSelect.You wouldn't even need a while loop.
12/02/2009 20:05 Flyff_Service#10
Okay thanks!
I will try it. :)
12/02/2009 20:12 MrSm!th#11
Quote:
Originally Posted by InvincibleNoOB View Post
Perhaps use WSAAsyncSelect() or WSAEventSelect.You wouldn't even need a while loop.
but a message loop dont you?
i dont like the message loop^^
12/02/2009 20:17 Flyff_Service#12
Edited:
mhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
12/02/2009 22:00 InvincibleNoOB#13
Quote:
Originally Posted by MrSm!th View Post
but a message loop dont you?
i dont like the message loop^^
There is always a message loop,using that technique only requires a few conditional instructions to be written inside the message loop.
12/03/2009 17:43 Flyff_Service#14
I solved this Problem adding a "continue;" at
PHP Code:
    if(FD_ISSET(aSocket, &fdSet))
    {
        
AddSocket(accept(aSocketNULLNULL));
        continue;
    }