Re: Help with UDP data handler

Steve Trigero <seecwriter-/[email protected]>
Newsgroups gmane.comp.hardware.rabbit-semiconductor
Message-ID <[email protected]>
I can't tell you what you might doing wrong, but I can offer
some suggestions based on how I did something similar.
I did not use the callback feature. It seems to me there are
too many messages flying around to use this feature reliably.
 
I open several UDP sockets listening for broadcast messages.
 
 for(i=0;i<UDP_SOCKETS;i++){
   udp_extopen(&udpcon[i].socket,
                 IF_DEFAULT,
                 UDP_BROADCAST_PORT,
                 -1,-1,NULL,0,0);
 }

 
In the main loop I call a function that checks each socket for
a received message. If a message with data is found, the data
is copied to a buffer and passed to a paring routine.
 
// Look for incoming packets. for( s=0;s<UDP_SOCKETS;s++) { if( !udp_peek( &udpcon[s].socket, NULL) ) { continue;// No packets... } numBytes=udp_recvfrom( &udpcon[s].socket, udpcon[s].buf, sizeof(udpcon[s].buf), &udpcon[s].ip, &udpcon[s].port); if( numBytes<1) { continue;// No data... } udpcon[s].buf[numBytes]=NUL; 
UdpParse( &udpcon[s]); 
}
 
Occasionally, my program sends its own broadcast status. 
When it does, it opens a UDP port, sends the message, then
closes the port.
 
// Open a Broadcast socket if( !udp_extopen( &udpsock.socket, IF_DEFAULT, UDP_BROADCAST_PORT-1, -1, UDP_BROADCAST_PORT, NULL, 0, 0) ) return;// Oops! // Build a status message FormatUDPStatusMsg( udpsock.buf); // Send it... udp_send( &udpsock.socket, udpsock.buf, strlen(udpsock.buf) ); udp_close( &udpsock.socket); 
 
This works well in our system.
 
 
 
>From: Jeff Powell <jrpinjapan-/[email protected]>
>To: [email protected] 
>Sent: Monday, July 30, 2012 8:57 AM
>Subject: [rabbit-semi] Help with UDP data handler
>
>
>  
>I posted this as part of a different question and nobody answered so I 
>will try again as it's own topic.
>
>I have opened a udp socket with a datahandler.
>I opened a second udp socket for sending multicast data when values change.
>I get a connection, read from client, write back to client.
>I get another connection. It does not go to the datahandler.
>The main loop runs and multicast data continues to send properly.
>
>What am I missing?
>
>-- code --
>if (!udp_open(&tsock, LOCAL_PORT, 0, 0, dataHandler))
>{
>printf("udp_lst_open failed!\n");
>exit(0);
>}
>else
>{
>printf("Listener %s:%d\n",MY_IP_ADDRESS,LOCAL_PORT);
>}
>if (!udp_open(&usock, MBUS_PORT, resolve(MBUS_ADDRESS), MBUS_PORT, 
>NULL))
>{
>printf("udp_open failed!\n");
>exit(0);
>}
>
>...
>
>while (1)
>{
>idleProcessing(); // where values are processed and 
>multicast data is sent as needed.
>tcp_tick(NULL);
>}
>
>...
>
>int dataHandler(int event, udp_Socket *s, ll_Gather *g, 
>_udp_datagram_info *udi)
>{
>int status;
>char cip[20];
>CARDMSG_S *pkt;
>if(handleUdp) // stops recursion, does not effect 
>handler death
>return 1;
>handleUdp=1;
>if(event == UDP_DH_ICMPMSG) // from the example
>{
>return 1;
>}
>memset(&replyMsg, 0, sizeof (REPLYMSG_S));
>xmem2root(pktbuf, g->data2, g->len2);
>if(g->len3>0)
>xmem2root(pktbuf+g->len2, g->data3, g->len3);
>pkt=(CARDMSG_S*) &pktbuf;
>inet_ntoa( cip, udi->remip);
>printf("%s:%u Slot %d Type %d Device %d Offset %x Width %x Data %lx 
>%lx\n", cip, udi->remport, pkt->cSlot, pkt->cOperation, pkt->cDevice, 
>pkt->cOffset, pkt->cDataWidth, pkt->sData.longs[1], pkt->sData.longs[0] );
>status = process_packet(pktbuf);
>replyMsg.eStatus = status;
>printf("Status: %x\n", status);
>udp_sendto(s, (char*) &replyMsg, sizeof(REPLYMSG_S), udi->remip, 
>udi->remport); // straight from the
>// even tried this but it does not help either. Returns 1 for NULL or 
>bytes recv'd for the incoming socket.
>printf("Sent response %u\n", tcp_tick(NULL));
>handleUdp=0;
>return 1;
>}
>
>
>
>
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.