?
Solved

udp socket broadcasting and listening

Posted on 2005-03-24
10
Medium Priority
?
691 Views
Last Modified: 2008-03-10
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
Comment
Question by:yuexb
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13625656

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
 

Author Comment

by:yuexb
ID: 13625807
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13626001

Do you know if the partner program is sending a reply?
0
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.

 

Author Comment

by:yuexb
ID: 13626031
yes, im pretty sure the other end is sending a reply.
0
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13626077

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
 

Author Comment

by:yuexb
ID: 13626196
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
 
LVL 46

Accepted Solution

by:
Kent Olsen earned 2000 total points
ID: 13626277

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
 

Author Comment

by:yuexb
ID: 13633399
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
 

Author Comment

by:yuexb
ID: 13684422
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
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 13684494

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question