[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.6.6 SDLNet_SocketReady

int SDLNet_SocketReady(sock)

sock
The socket to check for activity.
Both UDPsocket and TCPsocket can be used with this function.

Check whether a socket has been marked as active. This function should only be used on a socket in a socket set, and that set has to have had SDLNet_CheckSockets (see section 3.6.5 SDLNet_CheckSockets) called upon it.

Returns: non-zero for activity. zero is returned for no activity.

 
// Wait forever for a connection attempt
//SDLNet_SocketSet set;
//TCPsocket serversock, client;
int numready;

numready=SDLNet_CheckSockets(set, 1000);
if(numready==-1) {
    printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
    //most of the time this is a system error, where perror might help you.
    perror("SDLNet_CheckSockets");
}
else if(numready) {
    printf("There are %d sockets with activity!\n",numready);
    // check all sockets with SDLNet_SocketReady and handle the active ones.
    if(SDLNet_SocketReady(serversock)) {
        client=SDLNet_TCP_Accept(serversock);
        if(client) {
            // play with the client.
        }
    }
}

To just quickly do network handling with no waiting, we do this.

 
// Check for, and handle UDP data
//SDLNet_SocketSet set;
//UDPsocket udpsock;
//UDPpacket *packet;
int numready, numpkts;

numready=SDLNet_CheckSockets(set, 0);
if(numready==-1) {
    printf("SDLNet_CheckSockets: %s\n", SDLNet_GetError());
    //most of the time this is a system error, where perror might help you.
    perror("SDLNet_CheckSockets");
}
else if(numready) {
    printf("There are %d sockets with activity!\n",numready);
    // check all sockets with SDLNet_SocketReady and handle the active ones.
    if(SDLNet_SocketReady(udpsock)) {
        numpkts=SDLNet_UDP_Recv(udpsock,&packet);
        if(numpkts) {
            // process the packet.
        }
    }
}

See Also:
3.6.5 SDLNet_CheckSockets, 3.6.3 SDLNet_AddSocket, 3.6.4 SDLNet_DelSocket, 3.6.1 SDLNet_AllocSocketSet, 4.5 SDLNet_SocketSet, 4.3 UDPsocket, 4.2 TCPsocket



This document was generated on November, 3 2009 using texi2html