Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 695
  • Last Modified:

udp socket broadcasting and listening

hi, all,
im trying to program a rabbitcore board in dynamic c 8.10 so the board can broadcast on a udp socket and listen to feedback on the same socket. my broadcasting part seems working fine, but the program can't detect any feedback on the socket sent by the other end. does any one have any idea how to fix the problem? here's my code:

int broadcast(void)
{
   bytes = udp_send(&sockUDP, "hello", 5);
   switch (bytes)
   {
         case -1: //error
            return -1;
      default:
            printf("%s", "broadcasting\n");
   }
   return 1;
}

int getFeedback()
{
      bytes = udp_recv(&sockUDP, remote_ip, IP_SIZE);
   //printf("%s\n", *remote_ip);
   if (bytes>=0)
         return 1;
   else return 0;
}

void main()
{
      int state,status;
      long start;
      float endtime,TimePerTrans,TimePerByte,TimePerBit,BitsPerTime;
      state = 0;


      sock_init();

      while(1)
      {
            switch(state)
            {
                  case 0:/*CREATE SESSION*/
                        if(!udp_open(&sockUDP,MY_PORT,-1,REMOTE_PORT,NULL))
                              printf("%sERROR OPENNING SOCKET!\n",RED);
                        else
                        {
                              printf("%sUDP session created\n",GREEN);
                              printf("%swaiting for socket to be established\n",GREEN);
                              statetime = MS_TIMER+TIME_OUT;      // setup the time to be LISTEN state
                              state++;                                                      // complete move onto next state
//                              i = NUM_OF_SENDS;
//                              j=0;                                                            // contains the number of packets sent
                        }
                        break;
                  case 1:/*establish udp connection*/
                        if(sock_established(&sockUDP))                  // check for a connection
                        {
                              printf("%sUDP socket established\n",GREEN);
                              start = MS_TIMER;                                    // time socket was established
                              statetime = MS_TIMER+TIME_OUT;      // reset time to be in the SEND state
                              state++;                                                      //  we have connection so move on
                        }
                        else if ((long)(MS_TIMER-statetime) > 0)      // if time out and no sock
                        {
                  state = 0;
               printf("time out. abort UDP socket");
                                                                           //        abort and re-init
                              sock_abort(&sockUDP);                                    // close the socket
                     printf("\n");
            }
                        break;
                  case 2:/*BROADCAST*/
               if (broadcast()==1)//success
                  state = 3;
            else  //failure
            {
                  printf("broadcast failed.\n");
            }

            /*if (sock_write(&sock, "hello", 5)>-1)
                  state=3;
            else
                  state=4;*/
        case 3:/*Listen to feedback*/
                /*      if (udp_peek(sockUDP, NULL)==1)
                  printf("see a datagram.\n");
            else
                  printf("NO DATAGRAM.\n");*/
                    if (getFeedback()==1)
            {
                  state=4;
            }
            else if ((long)(MS_TIMER-statetime) > 0) // if X millisecs and still waiting
                        {
                  state = 0;                                                //        abort and re-init
               printf("no feedback.\n");
               sock_abort(&sockUDP);
            }
          break;
         case 4:
//............................................
    }
}

0
yuexb
Asked:
yuexb
  • 5
  • 5
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:

First, this code can't possibly run.  look in case 3 of the switch() statement.

But, it looks like you check for a udp response once and return to state 0.  Any non-zero timer value will exit the section.

And it takes longer than that for a message to be generated by the partner system and returned to you.


Kent
0
 
yuexbAuthor Commented:
this statement
            else if ((long)(MS_TIMER-statetime) > 0) // if X millisecs and still waiting
waits about 5 seconds for the partner system to respond. that should be an enough amount of time.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Do you know if the partner program is sending a reply?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
yuexbAuthor Commented:
yes, im pretty sure the other end is sending a reply.
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Check the return value from udp_recv().  If the value is -1, no data is available.  If the value is < -1, an error occurred.


Kent
0
 
yuexbAuthor Commented:
it returns -1. im thinking maybe it's the other end that's not sending a packet right. is there any ways to check that? the other end is written in VB6. and after hearing the broadcasting,  i just did

winsock.sendDate "...."

winsock is the udp socket that the program broadcasts on. does that seem right?

0
 
Kent OlsenData Warehouse Architect / DBACommented:

Let's think this through....

1)  You're putting a UDP packet on the local network that is being picked up by "someone".

2)  That someone is receiving your broadcast and is responding by sending its own UDP packet.

3)  You program isn't receiving the returned packet.


Since you seem to have source code for the VB portion, check to make sure that it's opening a UDP socket on th eport you expect!

It's quite possible (perhaps even likely) that the VB program is at fault.  Will it print any diagnostics so that you know that it read your packet.

Also, check the port designations!  Make sure that the two programs are in agreement.


Kent
0
 
yuexbAuthor Commented:
the VB program does show that it receives the packet.  and the port designations match on both ends. i also think there's something missing in the VB part and i will post the problem onto the VB section. thanks, Kent.

xiao
0
 
yuexbAuthor Commented:
well, i checked everything on the vb side, and people in the vb section also helped me with that. but it is still not working. :(  im looking back at the c part again, but i really couldn't see why its not working. i hooked both the rabbitcore board and the computer onto a hub, could that be a problem for UDP packet? i know the UDP packet doesn't go thru a router, but im not sure about a hub. but since the computer can receive a udp packet, it should be ok, right?

xiao
0
 
Kent OlsenData Warehouse Architect / DBACommented:

A hub is about the dumbest device available to connect multiple devices.  It should simply forward messages so that's not the problem.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now