winsock select fdset issues

This is a discussion on winsock select fdset issues within the C++ Programming forums, part of the General Programming Boards category; i'm getting error -1 for select, and i can't find out what i'm doing wrong. PHP Code: // <-- start get sockIndex          if ( ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    winsock select fdset issues

    i'm getting error -1 for select, and i can't find out what i'm doing wrong.

    PHP Code:
    // <-- start get sockIndex
            
    if (engine::playerVector.size() == 0)
            {
                
    sockIndex engine::sock_accept 1;
            }
            else
            {
                
    sockIndex engine::playerVector.back()->get_sock() + 1;
            }
            
    // end get sockIndex -->

            
    engine::sr select(sockIndex, &engine::playerSocketsNULLNULL, &engine::selectTime);

            if (
    engine::sr == -1)
            {
                
    cout << "error" << endl;
            }
            else if (
    engine::sr == 0)
            {
                
    cout << "nothing to select" << endl;
            }
            else
            {
                if (
    FD_ISSET(engine::sock_accept, &engine::playerSockets))
                {
                    
    engine::sock_accept accept(engine::sock_listen, (sockaddr *)&engine::addrRemote, &engine::length_addrRemote);
                    
    greet();
                }
                
                for (
    int i 0engine::playerVector.size(); i++)
                {
                    if (
    FD_ISSET(engine::playerVector[i], &engine::playerSockets))
                    {
                        
    cout << "ok" << endl;
                    }
                }
            } 
    its printing error. i can't find out what i did wrong, or why it won't work. i'll post the whole code below, but the error is in that code.

    PHP Code:
    namespace engine
    {
        
    ofstream fout;
        
    ifstream fin;

        
    vector <item*>itemVector;
        
    vector <player*>playerVector;

        
    SOCKET      sock_acceptsock_listen;
        
    sockaddr_in addrServeraddrRemote;

        
    int length_addrRemote sizeof(engine::addrRemote);
        
    int sr;
        
        
    fd_set  playerSockets;
        
    timeval selectTime;
    }

    void sock_init()
    {
        
    WSADATA w;
        
    int error WSAStartup(0x0202, &w);

        if (
    error)
        {
            return;
        }

        if (
    w.wVersion != 0x0202)
        {
            
    WSACleanup();
            return;
        }
    }

    int engine_init()
    {
        
    sock_init();

        
    engine::sock_listen socket(PF_INETSOCK_STREAM0);

        
    // <-- start set fdset
        
    FD_ZERO(&engine::playerSockets);

        
    //FD_SET(engine::sock_listen, &engine::playerSockets);
        
    FD_SET(engine::sock_accept, &engine::playerSockets);

        
    engine::selectTime.tv_sec  0;
        
    engine::selectTime.tv_usec 0;
        
    // end set fdset -->

        // <-- start set server address
        
    engine::addrServer.sin_family      AF_INET;
        
    engine::addrServer.sin_port        htons(7869);
        
    engine::addrServer.sin_addr.s_addr htonl(INADDR_ANY);
        
    // end set server address -->

        // <-- start load item database
        
    engine::fin.open("idb.dat"ios::in ios::binary);
        
    load_items(&engine::fin, &engine::itemVector);
        
    engine::fin.close();
        
    // end load item database -->

        
    if (bind(engine::sock_listen, (sockaddr *)&engine::addrServersizeof(engine::addrServer)) == SOCKET_ERROR)
        {
            
    WSACleanup();
            return (
    0);
        }

        if (
    listen(engine::sock_listen1) == SOCKET_ERROR)
        {
            
    WSACleanup();
            return (
    0);
        }

        return (
    1);
    }

    int main()
    {
        if (
    engine_init() == 0)
        {
            return (
    0);
        }

        
    int sockIndex;
        
        while (
    1)
        {
            
    // code from above
        
    }


  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > i'm getting error -1 for select, and i can't find out what i'm doing wrong.
    Well how about calling getLastError to find out which error you're getting then.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
       FD_SET(engine::sock_accept, &engine::playerSockets);
    sock_accept has not been created at this point, so you add an invalid socket to the playerSockets list (seemingly the only socket you add to this list).

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    it returns error 10022. i looked it up and it said its caused from trying to accept from a socket thats not created or something like that. but its getting returned from select, so i don't know if thats much help or not. anyways i added
    PHP Code:
    engine::sock_accept socket(PF_INETSOCK_STREAM0); 
    to the engine_init() function before I added sock_accept to the list, and that didn't fix anything. i then realized i probably want to use the listen socket instead because when they try to connect i'll get something from the listen socket not from sock_accept, right? anyways i am kind of confused why it doesn't work now. i'm still getting the same error. can anyone help. thank you for your time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock issues
    By tjpanda in forum Windows Programming
    Replies: 3
    Last Post: 12-04-2008, 07:32 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21