By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,710 Members | 1,626 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,710 IT Pros & Developers. It's quick & easy.

how to handle multiple send and receive with udp socket

P: 9
Dear All,

Could someone help and tell me how to handle multiple send and receive operations with udp sockets? In fact here is my problem:

server.c is composing of serveral sub programs (the same for client.c)
-----server.c-------
Expand|Select|Wrap|Line Numbers
  1. ................
  2. int main(){
  3.  ............
  4.   //create socket
  5.   if (sd = socket(PF_INET, SOCK_DGRAM, ptrp->p_proto)) {
  6.     fprintf(stderr, "socket creation failed\n");
  7.     exit(1);
  8.   }  
  9.  
  10.   // sockaddr structure  
  11.   memset((char *)&sad, 0, sizeof(sad)); 
  12.   sad.sin_port = htons(port);  
  13.   sad.sin_family = AF_INET;  
  14.   sad.sin_addr.s_addr = htons(INADDR_ANY); 
  15.  
  16.   //bind between the socket  and the address
  17.   if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
  18.       fprintf(stderr, "bind failed\n");
  19.       exit(1);
  20.   }
  21. ...............
  22. sub-prog1(int sd){
  23.  ...............
  24.  if (recvfrom(sd,buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
  25.       perror("read data failled");
  26.       exit(1);
  27.     }  
  28.  .........
  29.  if (sendto(sd,buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
  30.     perror("write data failled");
  31.     exit(1);
  32.   } 
  33.  ............
  34. }
  35. sub-prog2(int sd){
  36.  ...............
  37.  if (recvfrom(sd,buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
  38.       perror("read data failled");
  39.       exit(1);
  40.     }  
  41.  .........
  42.  if (sendto(sd,buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
  43.     perror("write data failled");
  44.     exit(1);
  45.    } 
  46.  ............
  47.  }
  48. sub-prog3(int sd){
  49.  ...............
  50.  if (recvfrom(sd,buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
  51.       perror("read data failled");
  52.       exit(1);
  53.     }  
  54.  .........
  55.  if (sendto(sd,buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
  56.     perror("write data failled");
  57.     exit(1);
  58.    } 
  59.  ............
  60.  }
  61. } // end of main
  62.  
  63. ------client.c------
  64. ............
  65. #define MAXSOCK 3
  66. ................
  67. int main(){
  68.   ................
  69.   // sockaddr structure  
  70.   memset((char *)&sad, 0, sizeof(sad)); 
  71.   sad.sin_port = htons(port);  
  72.   sad.sin_family = AF_INET;  
  73.   sad.sin_addr.s_addr = htons(INADDR_ANY); 
  74.  
  75.  for (i=0;i<MAXSOCK; i++){
  76.   //create socket
  77.   if (sd[MAXSOCK] = socket(PF_INET, SOCK_DGRAM, ptrp->p_proto)) {
  78.     fprintf(stderr, "socket creation failed\n");
  79.     exit(1);
  80.   }  
  81.  } // end of for
  82. ...............
  83. sub-prog1(int sd[0]){
  84.  .........
  85.  if (sendto(sd[0],buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
  86.     perror("write data failled");
  87.     exit(1);
  88.   } 
  89.  ...............
  90.  if (recvfrom(sd[0],buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
  91.       perror("read data failled");
  92.       exit(1);
  93.     }  
  94.  ............
  95. }
  96. sub-prog2(int sd[1]){
  97.  .........
  98.  if (sendto(sd[1],buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
  99.     perror("write data failled");
  100.     exit(1);
  101.   } 
  102.  ...............
  103.  if (recvfrom(sd[1],buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
  104.       perror("read data failled");
  105.       exit(1);
  106.     }  
  107.  ............
  108. }
  109. sub-prog3(int sd[2]){
  110.  .........
  111.  if (sendto(sd[2],buff,sizeof(buff),0,(struct sockaddr *) &sad,sizeof(sad)) {
  112.     perror("write data failled");
  113.     exit(1);
  114.   } 
  115.  ...............
  116.  if (recvfrom(sd[2],buff,MAXBUF,0,(struct sockaddr *) &sad,&fromSize)) {
  117.       perror("read data failled");
  118.       exit(1);
  119.     }  
  120.  ............
  121.  }
  122. } // end of main
  123.  
--------------------------
I tried serveral ways to make it working but unfortunatly I haven't succeded yet. Could some please tell me the right way on how to make such programs working ..... thanking you in advance and any help would be appreciated.

Faithfully yours,
Azwaw
Oct 1 '07 #1
Share this Question
Share on Google+
2 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
Given that you've cut a lot of code, we need more information. Is it not working because of errors, or is there a logical error in there so that it compiles without warnings, but does not act the way you intended?
Oct 1 '07 #2

P: 9
Hi,

thanks for your reply. the problem is not about the compiling ...
Actually I don't get any warning or error when the programs are compiled, but the problem is when the sub-prog2 is exucuted and then I get the errors like: segmentation ...
My question is how to handle multipe sending and receiving data using udp socket ???? Any one has an idea, it would be appreciated. Thanks

Regards and thanks for your reply,
Azwaw
Oct 2 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.