• C

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:
//............................................
    }
}

yuexbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.