[ENet-discuss] How to send and recevice boradcast packets using enet

Ruud van Gaal ruud at racer.nl
Mon Jan 7 00:51:57 PST 2013


I don't see p2udp_socket_create in the ENet library, so are you trying to
mix 2 network libraries?

Ruud


On Mon, Jan 7, 2013 at 7:33 AM, enetcub <enet_club at 126.com> wrote:

>  enet-discuss,
>
>   i just be in a spot  with send and receiveing BROADCAST packets in enet,
> i don't know what is the right way to playing with function
> enet_socket_receive
>
> //sender_code
>
>     P2UdpSocket sck=p2udp_socket_create(P2UDP_SOCKET_TYPE_DATAGRAM);
>     if(INVALID_SOCKET == sck){
>         fprintf(stderr,"error while create sccket\n");
>         return(EXIT_FAILURE);
>     }
>     //set as broadcast option
>     p2udp_socket_set_option(sck,P2UDP_SOCKOPT_BROADCAST,1);
>     //set broadcast addrs
>     P2UdpAddress p2u_addr;
>     p2u_addr.host=P2UDP_HOST_BROADCAST;
>     p2u_addr.port=P2UDP_HOST_TO_NET_16(P2U_PORT);
>     printf("starting send broadcast packages...\n");
>     //gererate notify package
>     char p2u_act_msg[16];
>     strcpy(p2u_act_msg,P2U_ASK_ALIVE_FLAG_MSG);
>     int p2u_alive_host_count=0;            //queue  length
>     while(1){
>         p2udp_socket_send(sck,&p2u_addr,&p2u_act_msg,sizeof(p2u_act_msg));
>         printf(".");
>         Sleep(1000);
>     }
>
>
> //recv_code
>
>    P2UdpSocket sck=p2udp_socket_create(P2UDP_SOCKET_TYPE_DATAGRAM);
>     if(INVALID_SOCKET == sck){
>         fprintf(stderr,"error while create a cscoket\n");
>         return(EXIT_FAILURE);
>     }
>     p2udp_socket_set_option(sck,P2UDP_SOCKOPT_BROADCAST,1);
>     P2UdpAddress serv_addr;
>     serv_addr.host=P2UDP_HOST_ANY;
>     serv_addr.port=P2UDP_HOST_TO_NET_16(P2U_PORT);
>     int iret=p2udp_socket_bind(sck,&serv_addr);
>     if(SOCKET_ERROR == iret){
>         fprintf(stderr,"bind socket with error\n");
>         p2udp_socket_shutdown(sck,0);
>         return(EXIT_FAILURE);
>     }
>     //host who send broadcast msg
>     P2UdpAddress host_addr;
>     char msg_buf[1024];
>     int recv_len=0;
>     while(1){printf("data length:%d\n",recv_len);   ------------> this
> alway got 0 or -1 ,why?
>
> recv_len=p2udp_socket_receive(sck,&host_addr,&msg_buf,sizeof(msg_buf));
>         if(recv_len <= 0){
>             fprintf(stderr,"error receiving data..\n");
>             //p2udp_socket_shutdown(sck,0);
>             //return(EXIT_FAILURE);
>             //break;          //no msg or blcked,just wait
>         }else{
>             static int seq_count=0;
>             memset(msg_buf+recv_len,'/0',1);
>             printf("get broadcast message from %u:%d with length:%d has
> content:%s seq_id=%d\n",
>                     host_addr.host,
>                     host_addr.port,
>                     recv_len,
>                     msg_buf,
>                     seq_count++);
>             //check if send is alive flag
>             if(strcmp(msg_buf,P2U_ASK_ALIVE_FLAG_MSG)){
>                 printf("i am alive\n");
>             }else{
>                 printf("i am dead\n");
>             }
>             memset(msg_buf,0,sizeof(msg_buf));
>         }
>         Sleep(1000);
>     }
>     p2udp_socket_shutdown(sck,0);
>
>
>
>  enetcub,enet_club at 126.com
> 2013-01-07
>
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20130107/4a73152b/attachment.html>


More information about the ENet-discuss mailing list