how to handle multiple send and receive with udp socket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apollo135
    New Member
    • Sep 2007
    • 9

    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-------
    [code=c]
    ............... .
    int main(){
    ............
    //create socket
    if (sd = socket(PF_INET, SOCK_DGRAM, ptrp->p_proto)) {
    fprintf(stderr, "socket creation failed\n");
    exit(1);
    }

    // sockaddr structure
    memset((char *)&sad, 0, sizeof(sad));
    sad.sin_port = htons(port);
    sad.sin_family = AF_INET;
    sad.sin_addr.s_ addr = htons(INADDR_AN Y);

    //bind between the socket and the address
    if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
    fprintf(stderr, "bind failed\n");
    exit(1);
    }
    ...............
    sub-prog1(int sd){
    ...............
    if (recvfrom(sd,bu ff,MAXBUF,0,(st ruct sockaddr *) &sad,&fromSize) ) {
    perror("read data failled");
    exit(1);
    }
    .........
    if (sendto(sd,buff ,sizeof(buff),0 ,(struct sockaddr *) &sad,sizeof(sad )) {
    perror("write data failled");
    exit(1);
    }
    ............
    }
    sub-prog2(int sd){
    ...............
    if (recvfrom(sd,bu ff,MAXBUF,0,(st ruct sockaddr *) &sad,&fromSize) ) {
    perror("read data failled");
    exit(1);
    }
    .........
    if (sendto(sd,buff ,sizeof(buff),0 ,(struct sockaddr *) &sad,sizeof(sad )) {
    perror("write data failled");
    exit(1);
    }
    ............
    }
    sub-prog3(int sd){
    ...............
    if (recvfrom(sd,bu ff,MAXBUF,0,(st ruct sockaddr *) &sad,&fromSize) ) {
    perror("read data failled");
    exit(1);
    }
    .........
    if (sendto(sd,buff ,sizeof(buff),0 ,(struct sockaddr *) &sad,sizeof(sad )) {
    perror("write data failled");
    exit(1);
    }
    ............
    }
    } // end of main

    ------client.c------
    ............
    #define MAXSOCK 3
    ............... .
    int main(){
    ............... .
    // sockaddr structure
    memset((char *)&sad, 0, sizeof(sad));
    sad.sin_port = htons(port);
    sad.sin_family = AF_INET;
    sad.sin_addr.s_ addr = htons(INADDR_AN Y);

    for (i=0;i<MAXSOCK; i++){
    //create socket
    if (sd[MAXSOCK] = socket(PF_INET, SOCK_DGRAM, ptrp->p_proto)) {
    fprintf(stderr, "socket creation failed\n");
    exit(1);
    }
    } // end of for
    ...............
    sub-prog1(int sd[0]){
    .........
    if (sendto(sd[0],buff,sizeof(bu ff),0,(struct sockaddr *) &sad,sizeof(sad )) {
    perror("write data failled");
    exit(1);
    }
    ...............
    if (recvfrom(sd[0],buff,MAXBUF,0, (struct sockaddr *) &sad,&fromSize) ) {
    perror("read data failled");
    exit(1);
    }
    ............
    }
    sub-prog2(int sd[1]){
    .........
    if (sendto(sd[1],buff,sizeof(bu ff),0,(struct sockaddr *) &sad,sizeof(sad )) {
    perror("write data failled");
    exit(1);
    }
    ...............
    if (recvfrom(sd[1],buff,MAXBUF,0, (struct sockaddr *) &sad,&fromSize) ) {
    perror("read data failled");
    exit(1);
    }
    ............
    }
    sub-prog3(int sd[2]){
    .........
    if (sendto(sd[2],buff,sizeof(bu ff),0,(struct sockaddr *) &sad,sizeof(sad )) {
    perror("write data failled");
    exit(1);
    }
    ...............
    if (recvfrom(sd[2],buff,MAXBUF,0, (struct sockaddr *) &sad,&fromSize) ) {
    perror("read data failled");
    exit(1);
    }
    ............
    }
    } // end of main
    [/code]
    --------------------------
    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
    Last edited by sicarie; Oct 1 '07, 01:03 PM. Reason: Code tags
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    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?

    Comment

    • apollo135
      New Member
      • Sep 2007
      • 9

      #3
      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

      Comment

      Working...