<HTML><HEAD><TITLE>Mail</TITLE>
<META name=GENERATOR
content="KsDHTMLEDLib.ocx, FreeWare HTML Editor 1.164.2, ?Kurt Senfer">
<META content="text/html; charset=GB2312" http-equiv=Content-Type></HEAD>
<BODY style="FONT-FAMILY: ÐÂËÎÌå; FONT-SIZE: 9pt" leftMargin=5 topMargin=5 #ffffff>
<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">enet_club@126.com</A></DIV>
<DIV>2013-01-07 </DIV></BODY></HTML>