[SDL] SDL_net and broadcasting

nuage nuage at ysagoon.com
Mon Sep 30 04:58:01 PDT 2002


>
>
>>IIRC, anything ending in .255 is a broadcast address. If your subnet is 
>>128.0.0, sending to 128.0.0.255 should broadcast to the entire network 
>>- assuming the network allows IP broadcast.
>>    
>>
>>However, I don't know if this will work with SDL_net. You may have to 
>>use a UDP packet instead of TCP.
>>
TCP is a connected transmission protocol, one socket to one socket.
Then you can't have any broadasting ability with TCP. (only multicasting 
but this is something totally different we won't talk about)
So you can only broadcast with UDP packets.

yes, any address ending with .255 is a broadcasting address.

>SDL_net provides UDP packets, and yes, I believe Darrell is correct.
>Does Linux require extra work to send to the broadcast addresses?
>
Yes you need extra work, somhow.
You can't simply send packets to a broadcasting address. This will fail.
A SDL_net example:

void f(int size, void *data)
{
    UDPpacket *packet=SDLNet_AllocPacket(size);
    assert(packet);
    memcpy((char *)packet->data, data, size);
   
    packet->address.host=0xFFFFFFFF;
    packet->address.port=0;
    packet->channel=-1;
   
    bool success=SDLNet_UDP_Send(socket, channel, packet)==1;
   
    printf("success=%d", success);
}

success=0

Or a UNIX example:

{
    // servAddr has been set to 255.255.255.255
    int rc=sendto(socketDefinition, data, size, 0, (struct sockaddr *) 
&servAddr, sizeof(servAddr));
    if (rc<0)
        printf("failed");
}

failed

You have to modiffy the "socketDefinition" this way:
("sock->channel" into SDLnetUDP.c)

char ttl=1;
setsockopt(socketDefinition, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 
sizeof(ttl));
int hold=1;
setsockopt(socketDefinition, SOL_SOCKET, SO_BROADCAST, &hold, sizeof(hold));

OK, setting TTL=1 is obviously optional, but cleaner.
But the second modiffication is mandatory for broadcasting.

Once it's accepeted, we have to come back to my fist mail: how to 
implements this ?




Luc-Olivier de Charrière






More information about the SDL mailing list