SDLNet_SocketReady

Name

SDLNet_SocketReady -- check whether a socket has been marked as active.

Synopsis

#include "SDL.h"

int SDLNet_SocketReady(sock);

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

Description

SDLNet_SocketReady 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 called upon it.

Return Value

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

Example

Example using TCP and a 1000ms timeout waiting for 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.
        }
    }
}

Example using UDP with no waiting:

// 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

SDLNet_CheckSockets, SDLNet_AddSocket, SDLNet_DelSocket, SDLNet_AllocSocketSet, SDLNet_SocketSet, UDPsocket, TCPsocket

SDLNet_SocketReady (last edited 2008-04-17 08:18:21 by localhost)