SDLNet_UDP_Send

Name

SDLNet_UDP_Send -- send a UDP packet.

Synopsis

int SDLNet_UDP_Send(UDPsocket sock, int channel, UDPpacket *packet)

sock A valid UDPsocket.

channel What channel to send packet on.

packet The packet to send.

Description

Send packet using the specified socket sock, using the specified channel or else the packet address.

If channel is not -1 then the packet is sent to all the socket channels bound addresses. If socket sock`s channel is not bound to any destinations, then the packet is not sent at all! If the channel is -1, then the packet's address is used as the destination.

Don't forget to set the length of the packet in the len element of the packet you are sending!

NOTE: the packet->channel will be set to the channel passed in to this function.

Return Value

Returns: The number of destinations sent to that worked. 0 is returned on errors.

Note that since a channel can point to multiple destinations, there should be just as many packets sent, so dont assume it will always return 1 on success. Unfortunately there's no way to get the number of destinations bound to a channel, so either you have to remember the number bound, or just test for the zero return value indicating all channels failed.

Example

// send a packet using a UDPsocket, using the packet's channel as the channel
//UDPsocket udpsock;
//UDPpacket *packet;
int numsent;

numsent=SDLNet_UDP_Send(udpsock, packet->channel, packet);
if(!numsent) {
    printf("SDLNet_UDP_Send: %s\n", SDLNet_GetError());
    // do something because we failed to send
    // this may just be because no addresses are bound to the channel...
}

Here's a way of sending one packet using it's internal channel setting. This is actually what SDLNet_UDP_Send ends up calling for you.

// send a packet using a UDPsocket, using the packet's channel as the channel
//UDPsocket udpsock;
//UDPpacket *packet;
int numsent;

numsent=SDLNet_UDP_SendV(sock, &packet, 1);
if(!numsent) {
    printf("SDLNet_UDP_SendV: %s\n", SDLNet_GetError());
    // do something because we failed to send
    // this may just be because no addresses are bound to the channel...
}

See Also

SDLNet_UDP_Bind, SDLNet_UDP_SendV, SDLNet_UDP_Recv, SDLNet_UDP_RecvV, UDPpacket, UDPsocket

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