how to do udp broadcast with multithreading

hello to all
i want to use multithreading to my UDP broadcast server client program. will anyone help me by proving C code. i am working in fedora. also my requirement is POSIX compliance.please help me.....

What have you tried so far, where are you stuck? Remember, this is a forum run by volunteers. If you want someone else to do your work please use one of the many freelancer sites on the net, who will expect to be paid.

hii pludi

my program work well foe udp broadcast but it receives data single time . i want to implement it using threads. i try to do it not so much is done. will u plz tell where i had done wrong. i am sending udp broadcast code with threads for single machine. i nneed multiple clients can send data to server and it receive successfully. time being i am using 5 clients.

server.c

#define _REENTRANT
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>
#include <pthread.h>
#define MAXBUFSIZE 500
#define num 5


/* function prototypes and global variables */
    void * process(void *);
      
    int    service_count;
    int sock, status;
    socklen_t socklen;
     unsigned char buffer[MAXBUFSIZE]={0};
    struct sockaddr_in saddr;
    pthread_t thread[num];
    void *thread_result;
      int broadcast=1;
    int res1,res;
//using namespace std;
int main ( int argc, char *argv[] )
{
   
     FILE * fp1;
      char ch={0};
    int ccount=0;
// SET CONTENT OF STRUCT SADDR AND BUFFER TO ZERO
    memset ( &saddr, 0, sizeof ( struct sockaddr_in ) );

    // OPEN A UDP socket
    sock = socket ( AF_INET, SOCK_DGRAM, 0);
    if ( sock < 0 )
        perror ( "Error creating socket" );
   
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(5880); //(8904);
    //saddr.sin_addr.s_addr =inet_addr("192.9.200.159");
    saddr.sin_addr.s_addr =INADDR_ANY;
    socklen = sizeof ( struct sockaddr_in );
    //BINDING PROCESS TO PORT
    status = bind ( sock, ( struct sockaddr * ) &saddr,socklen );
    if(status<0)
    perror("bind error:");
   
    //SET SOCKET OPTIONS
    status = setsockopt ( sock,SOL_SOCKET,SO_BROADCAST,
            (char*) &broadcast,sizeof(broadcast));
    perror ( "setsockopt::" );
        int i;
        for(i=1;i<=5;i++)
{
        /* create a new thread to process the incomming request */
         res=pthread_create(&thread, NULL, process, (void *) sock);
       
        if(res!=0)
{       
        printf("\nthread[%d] creation failed",thread);
}
        sleep(10);

}        /* the server is now free to accept another socket request */
              res1=pthread_join(thread,&thread_result);
        if(res!=0)
{       
        perror("\nfailed to join");
}
   
 return 0;       
   
}


//handling of request by thread

    void * process(void *arg)
{
    //pthread_mutex_t lock;   
    int     mysocfd = (int) arg;
    unsigned char buf[MAXBUFSIZE]={0};

    //printf("Child thread [%d]: Socket number = %d\n", pthread_self(), mysocfd);

    /* receive from the given socket */
    //read(mysocfd, data, 40);

    //memset ( &buf, 0, sizeof ( buf ) );
    int len=recvfrom( mysocfd, buf, MAXBUFSIZE, 0,( struct sockaddr * )(&saddr),&socklen );
    //printf("buffer is %s",buf);
    printf("Child thread [%d]: My data = %s\n", pthread_self(), buf);

    service_count++;
    printf("Child thread [%d]: The tota0l sockets served = %d\n",pthread_self(), service_count);

    /* close the socket and exit this thread */
    close(mysocfd);
    pthread_exit("thread returned");
    //pthread_exit((void *)0);
}

client.c

#define _REENTRANT
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/uio.h>
#include <unistd.h>
#include <pthread.h>
#define MAXBUFSIZE 500

/* function prototypes and global variables */
    void * senddata(void *);
      
    int    service_count;
    int sock, status;
    socklen_t socklen;
     unsigned char buffer[MAXBUFSIZE]={0};
    struct sockaddr_in saddr;
    pthread_t thread1;
      int broadcast=1;
//using namespace std;
int main ( int argc, char *argv[] )
{
   
     FILE * fp1;
      char ch={0};
    int ccount=0;
    // SET CONTENT OF STRUCT SADDR AND BUFFER TO NULL VALUES
    memset ( &saddr, 0, sizeof ( struct sockaddr_in ) );
    memset ( &buffer,0,MAXBUFSIZE );
   
    // OPEN A UDP SOCKET
    sock = socket ( AF_INET, SOCK_DGRAM, 0 );
    if ( sock < 0 )
    perror ( "Error creating socket" );
   
    //SETTING THE ADDRESS STRUCTURE PARAMETERS
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons ( 5880);
   
    // this ip may be specific or any ......
    saddr.sin_addr.s_addr =INADDR_ANY;
    //saddr.sin_addr.s_addr =inet_addr("192.9.200.199");
    socklen = sizeof ( struct sockaddr_in );
   
    //SETTING THE SOCKET OPTIONS
    status = setsockopt ( sock,SOL_SOCKET,SO_BROADCAST,
                          (char*)&broadcast,sizeof (broadcast) );
    perror ( "setsockopt::" );
    status = setsockopt ( sock,SOL_SOCKET,SO_REUSEADDR,
                          (char*) &broadcast,sizeof(broadcast) );
    if ( status < 0 )
        perror ( "Error bind:");

   
    //send data to the server
    char cc[MAXBUFSIZE]="HELLO this is from client machine ";

    int count=sendto( sock,cc,400,0,( struct sockaddr * ) (&saddr),socklen );
    if ( count<0 )
        perror ( "error in sending data:" );
     else printf("send");

    /* create a new thread to process the incomming request */
    /*    pthread_create(&thread1, NULL,senddata, (void *) sock);
       
        /* the server is now free to accept another socket request */
    //pthread_join(&thread1,NULL);*/
    close(sock);
     return 0;       
   
}

Do you realize that the way you're using the threads is useless? The way you're using pthread_create()/pthread_join() serializes everything! So that a classical C function would do it, in a more efficient way (no context switch...).

What are you trying to achieve? Try to explain and phrase it without using code!

Cheers, Lo�c

i just want an multithreaded multiclient server for UDP broadcast. will u plz refer me some good links or any other help

I still don't understand what you are trying to achieve:

  • you have an UDP server ?
  • this server should serve multiple UDP clients ?
  • you want to use thread (e.g. one thread per client) ?
  • what is meant with "UDP broadcast" ?
  • ....

Lo�c

You're just spitting buzzwords. What's this multithreaded UDP server actually supposed to do? "udp broadcast" is a how, not a what. What's talking to what, and what information is being sent where, why?