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

James - z2895 at hotmail.com
Mon Jan 7 04:59:05 PST 2013


This may help you, I posted this http://www.gamedev.net/topic/635600-how-to-find-a-host-running-the-game/#entry5013275 you then process the packets as normal...

            int received = enet_socket_receive(recvsock, &senderAddress, &buf, 1);
            if(received==0) { /* would have blocked, need to resend later */ }
            else if(received < 0) { /* something went wrong, error! */ }
            else
            {process packet}


Date: Mon, 7 Jan 2013 09:51:57 +0100
From: ruud at racer.nl
To: enet-discuss at cubik.org
Subject: Re: [ENet-discuss] How to send and recevice boradcast packets using	enet

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





_______________________________________________
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/cd59b1d0/attachment-0001.html>


More information about the ENet-discuss mailing list