473,406 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to handle multiple send and receive with udp socket

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
2 4104
sicarie
4,677 Expert Mod 4TB
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
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

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

Similar topics

0
by: Alphamacaroon | last post by:
All, I'm wondering if anyone can help me with a strange problem I'm having. First off, here's what I'm trying to do: I'm developing a UDP network application that should allow the end user to...
2
by: Bruce Vander Werf | last post by:
I am developing a network client application (using the Socket class) that will need to make simultaneous TCP connections to many (100 or more) servers. In this case, which would be a better...
13
by: Manfred Braun | last post by:
Hi All, I am trying to understand the blocking method socket.Send(). The call blocks as expected, but does this mean, it returnes after the underlying TCP layer got a confirmation, that the send...
7
by: Sharon | last post by:
Hi all, I've implemented a TCP server using the Socket async methods. When connecting to the server from 3 instances of hyper terminal, i've noticed that each of the newly created server sockets,...
2
by: yvan | last post by:
Hi, Here is my client/server scenario: Step1: Client connects to server and sends data sucessfully (using Socket.Send()). Step2: Server gracefully exists (calls Socket.Shutdown() and...
3
by: BuddyWork | last post by:
Hello, Could someone please explain why the Socket.Send is slow to send to the same process it sending from. Eg. Process1 calls Socket.Send which sends to the same IP address and port, the...
0
by: Buddy Home | last post by:
There is two examples of code. Example 1. Send and Receive within the same process. Put this code in a console app called SendAndReceive and run the code. using System; using...
2
by: =?Utf-8?B?YmJn?= | last post by:
Hi all, I am testing how well my server application is connecting to multiple clients and give and take some information. All actions(connect, accept, send, receive) are performed by async...
4
by: =?Utf-8?B?Y2hyaXNiZW4=?= | last post by:
Hi, My understanding is that for the same socket, assuming send and receive run at two different threads, I have to use BeginReceive to avoid the blocking on send part. However, I can jsut use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.