<div dir="ltr">I don't see p2udp_socket_create in the ENet library, so are you trying to mix 2 network libraries?<div><br></div><div style>Ruud</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jan 7, 2013 at 7:33 AM, enetcub <span dir="ltr"><<a href="mailto:enet_club@126.com" target="_blank">enet_club@126.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div style="FONT-FAMILY:新宋体;FONT-SIZE:9pt">
<div>enet-discuss,</div>
<div> </div>
<div>  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</div>
<div> </div>
<div>//sender_code</div>
<div> </div>
<div>    P2UdpSocket 
sck=p2udp_socket_create(P2UDP_SOCKET_TYPE_DATAGRAM);<br>    
if(INVALID_SOCKET == sck){<br>        
fprintf(stderr,"error while create 
sccket\n");<br>        
return(EXIT_FAILURE);<br>    }</div>
<div>    //set as broadcast option<br>    
p2udp_socket_set_option(sck,P2UDP_SOCKOPT_BROADCAST,1);</div>
<div>    //set broadcast addrs<br>    P2UdpAddress 
p2u_addr;<br>    
p2u_addr.host=P2UDP_HOST_BROADCAST;<br>    
p2u_addr.port=P2UDP_HOST_TO_NET_16(P2U_PORT);</div>
<div>    printf("starting send broadcast packages...\n");</div>
<div>    //gererate notify package<br>    char 
p2u_act_msg[16];<br>    
strcpy(p2u_act_msg,P2U_ASK_ALIVE_FLAG_MSG);</div>
<div>    int 
p2u_alive_host_count=0;            
//queue  length</div>
<div>    while(1){<br>        
p2udp_socket_send(sck,&p2u_addr,&p2u_act_msg,sizeof(p2u_act_msg));<br>        
printf(".");<br>        
Sleep(1000);<br>    }</div>
<div> </div>
<div> </div>
<div>//recv_code</div>
<div> </div>
<div>   P2UdpSocket 
sck=p2udp_socket_create(P2UDP_SOCKET_TYPE_DATAGRAM);<br>    
if(INVALID_SOCKET == sck){<br>        
fprintf(stderr,"error while create a 
cscoket\n");<br>        
return(EXIT_FAILURE);<br>    }</div>
<div>    
p2udp_socket_set_option(sck,P2UDP_SOCKOPT_BROADCAST,1);</div>
<div>    P2UdpAddress serv_addr;<br>    
serv_addr.host=P2UDP_HOST_ANY;<br>    
serv_addr.port=P2UDP_HOST_TO_NET_16(P2U_PORT);</div>
<div>    int 
iret=p2udp_socket_bind(sck,&serv_addr);<br>    
if(SOCKET_ERROR == iret){<br>        
fprintf(stderr,"bind socket with 
error\n");<br>        
p2udp_socket_shutdown(sck,0);<br>        
return(EXIT_FAILURE);<br>    }<br>    //host who 
send broadcast msg<br>    P2UdpAddress 
host_addr;<br>    char msg_buf[1024];</div>
<div>    int recv_len=0;<br>    
while(1){printf("data length:%d\n",recv_len);   ------------> this 
alway got 0 or -1 ,why?<br>        
recv_len=p2udp_socket_receive(sck,&host_addr,&msg_buf,sizeof(msg_buf));<br>        
if(recv_len <= 
0){<br>            
fprintf(stderr,"error receiving 
data..\n");<br>            
//p2udp_socket_shutdown(sck,0);<br>            
//return(EXIT_FAILURE);<br>            
//break;          //no msg or 
blcked,just wait<br>        
}else{<br>            
static int 
seq_count=0;<br>            
memset(msg_buf+recv_len,'/0',1);<br>            
printf("get broadcast message from %u:%d with length:%d has content:%s 
seq_id=%d\n",<br>                    
host_addr.host,<br>                    
host_addr.port,<br>                    
recv_len,<br>                    
msg_buf,<br>                    
seq_count++);</div>
<div>            //check 
if send is alive 
flag<br>            
if(strcmp(msg_buf,P2U_ASK_ALIVE_FLAG_MSG)){<br>                
printf("i am 
alive\n");<br>            
}else{<br>                
printf("i am 
dead\n");<br>            
}<br>            
memset(msg_buf,0,sizeof(msg_buf));<br>        
}<br>        
Sleep(1000);<br>    }</div>
<div>    p2udp_socket_shutdown(sck,0);</div>
<div> </div>
<div> </div>
<div> </div>
<div></div>
<div>enetcub,<a href="mailto:enet_club@126.com" target="_blank">enet_club@126.com</a></div>
<div>2013-01-07 </div></div>
<br>_______________________________________________<br>
ENet-discuss mailing list<br>
<a href="mailto:ENet-discuss@cubik.org">ENet-discuss@cubik.org</a><br>
<a href="http://lists.cubik.org/mailman/listinfo/enet-discuss" target="_blank">http://lists.cubik.org/mailman/listinfo/enet-discuss</a><br>
<br></blockquote></div><br></div>