[ENet-discuss] Broadcast Messages through the LAN - Trying an implementation
Lee Salzman
lsalzman1 at cox.net
Wed Mar 18 11:41:57 PDT 2009
The following line is wrong:
mySocket = socket(AF_INET, SOCK_RAW, VT_UDP_SOCK);
This should be:
mySocket = socket(PF_INET, SOCK_DGRAM, 0);
Also, the host ip field of the IPv4 address "sin_addr.s_addr" is already
in network byte order, so you shouldn't be using htonl() for that. Only
for the port "sin_port" do you need to use htons(), since the port
number you are providing is in host byte-order.
Note that you can really just reuse the enet_socket_*() and
enet_address_*() functions for this, which should make things go much
easier. They are not terribly documented yet, but what they do should be
self-explanatory reading through unix.c. You would have to get the CVS
source code of ENet (which is stable, not to worry), however, as the
actual release package doesn't have some of the various functionality -
like REUSEADDR - which I had recently added for this purpose.
If you use the CVS version of ENet, you can just do something like the
following:
To setup client sending socket:
ENetSocket pingsock = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM); //
create UDP socket
enet_socket_set_option(pingsock, ENET_SOCKOPT_NONBLOCK, 1); // prevent
send from blocking (through must be careful to check enet_socket_send()
and enet_socket_receive() for return value of 0, meaning socket is busy
enet_socket_set_option(pingsock, ENET_SOCKOPT_BROADCAST, 1); // allow
the socket to send to the broadcast address
To setup the server receiving socket:
ENetAddress address = /* something with fixed port value */;
ENetSocket pongock = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM);
enet_socket_set_option(pongsock, ENET_SOCKOPT_REUSEADDR, 1); // allow
multiple servers on the same computer to listen to same port
enet_socket_bind(pongsock, &address); // bind to the address to listen
on after REUSEADDR has been set
enet_socket_set_option(pongsock, ENET_SOCKOPT_NONBLOCK, 1); // no
blocking again
To send data on either socket:
ENetAddress destinationAddress = /* something */;
ENetBuffer buf;
buf.data = /* pointer to data */;
buf.dataLength = /* size of data, in bytes */;
int err = enet_socket_send(socket, destinationAddress, &buf, 1);
if(err==0) { /* would have blocked, need to resend later */ }
else if(err < 0) { /* something went wrong, error! */ }
To receive data on either socket:
ENetAddress senderAddress;
ENetBuffer buf;
buf.data = /* pointer to receive buffer */;
buf.dataLength = /* size of receive buffer, in bytes */;
int received = enet_socket_receive(socket, destinationAddress, &buf, 1);
if(receive==0) { /* would have blocked, need to resend later */ }
else if(receive < 0) { /* something went wrong, error! */ }
else { /* receive says the number of bytes written into buf.data */ }
Lee
David Orejuela wrote:
>
> I have tried to implement the solution you provided me, but there are
> some problems sending data and I have some questions about it:
>
> --------
>
> SERVER:
>
> --------
>
> Server socket creation doesn't return errors, but I'm not sure if I'm
> doing things right
>
> --- Socket creation---
>
> mySocket = socket(AF_INET, SOCK_RAW, VT_UDP_SOCK);
>
> //SOCK_RAW for raw UDP traffic?
>
> ---SetSockOpt---
>
> int reuseAddrOp = 1;
>
> setsockopt(mySocket,SOL_SOCKET/*IPPROTO_UDP*/,SO_REUSEADDR,&reuseAddrOp,sizeof(reuseAddrOp))
>
> //must I change option for IPPROTO_UDP or SOL_SOCKET?
>
> ---Bind---
>
> sockaddr_in inAddress;
>
> inAddress.sin_family = AF_INET;
>
> inAddress.sin_port = htons(DEFAULT_PORT); //#define DEFAULT_PORT 1001
>
> inAddress.sin_addr.s_addr = htonl(INADDR_ANY);
>
> //is INADDR_ANY address right?
>
> bind(mySocket,&inAddress,sizeof(inAddress));
>
> ---Listening thread---
>
> ...
>
> do
>
> {
>
> FindServerRequestMessage fsr_msg;
>
> //the sockaddr_in parameter is right? I want to listen any address...
>
> int result = recvfrom(mySocket,&fsr_msg, sizeof(fsr_msg), 0,
> &inAddress, sizeof(inAddress));
>
> if(result > 0)
>
> {
>
> //Get fsr_msg data and answer
>
> }
>
> }
>
> while(1);
>
> --------
>
> CLIENT
>
> --------
>
> The client sends a FindServersRequest message through the socket.
>
> When I try to send data with the "sendto" function returns me a -1,
> but I don't know why...
>
> ---Socket creation---
>
> Like the server
>
> ---SetSockOpt---
>
> Like the server
>
> ---Bind---
>
> Like the server
>
> ---Delivery---
>
> sockaddr_in outAddress;
>
> outAddress.sin_family = AF_INET;
>
> outAddress.sin_port = htons(1001);
>
> outAddress.sin_addr.s_addr = htonl(ENET_HOST_BROADCAST); // BROADCAST
> sending, is it ok?
>
> sendto(mySocket,&fsr_msg,
> sizeof(fsr_msg),0,&outAddress,sizeof(outAddress));
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>
More information about the ENet-discuss
mailing list