how to do udp broadcast with multithreading


 
Thread Tools Search this Thread
Top Forums Programming how to do udp broadcast with multithreading
# 1  
Old 05-20-2011
Data 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.....
# 2  
Old 05-20-2011
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.
# 3  
Old 05-20-2011
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
Code:
#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[i], NULL, process, (void *) sock);
       
        if(res!=0)
{       
        printf("\nthread[%d] creation failed",thread[i]);
}
        sleep(10);

}        /* the server is now free to accept another socket request */
              res1=pthread_join(thread[i],&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

Code:
#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;       
   
}


Last edited by pludi; 05-20-2011 at 08:49 AM..
# 4  
Old 05-20-2011
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, Loc
# 5  
Old 05-23-2011
UDP broadcast

i just want an multithreaded multiclient server for UDP broadcast. will u plz refer me some good links or any other help
# 6  
Old 05-23-2011
Quote:
Originally Posted by moti12
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" ?
- ....

Loc
# 7  
Old 05-23-2011
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?
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Help with multithreading

I take this question of the The Linux Programming Interface: A Linux and Unix System Programming page 652 exercise 30.1 I want someone to explain the under line statement because it sounds complex to me couldn't understand anything 30-1 Modify the program (thread_incr.c) so that each loop in... (3 Replies)
Discussion started by: fwrlfo
3 Replies

2. Shell Programming and Scripting

Nc won't send udp broadcast!?

Greetings, I want to send broadcast udp from a script. This works but is not broadcast: echo -n "this is my message\r\n" | nc -u 192.168.0.12 5100 The broadcast version does not work: echo -n "this is my message\r\n" | nc -u 192.168.0.255 5100 Suggestions on the right way to do this... (2 Replies)
Discussion started by: anotherstevest
2 Replies

3. IP Networking

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..... (0 Replies)
Discussion started by: moti12
0 Replies

4. Programming

MultiThreading using Pthreads

Situation: i have multiple pthread_create calls like this: pthread_create(...., ThreadFunc1,.....); pthread_create(...., ThreadFunc2,.....); . . which i am using to create multiple threads.All the "ThreadFunc<i>" functions are actually calling same function "Receive" of a class using same... (3 Replies)
Discussion started by: Sastra
3 Replies

5. Shell Programming and Scripting

Multithreading program

Hi I need to insert 1million records into MySQL database, but it is taking lot of time as there is no bulk insert support. I want to spawn 10 processes which will insert 100k records each parallely. Can somebody help me with a example program to execute this task through shell scripting. (5 Replies)
Discussion started by: sach_roger
5 Replies

6. UNIX for Advanced & Expert Users

Why UDP broadcast don't run?

I use FreeBSD7.0, I want to use UDP broadcast,my code is following: /*udpcli01.c*/ int main(int argc, char **argv) { int sockfd; struct sockaddr_in servaddr; if (argc != 2) err_quit("usage: udpcli <IPaddress>"); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET;... (2 Replies)
Discussion started by: konvalo
2 Replies

7. UNIX for Advanced & Expert Users

multithreading in UNIX

Hi, Can you please give me a suitable reference to learn multithreading programming in C in UNIX? Thanks (3 Replies)
Discussion started by: naan
3 Replies

8. Programming

multithreading on OSX

Hi all, I have a query about multithreading. What I would like to do is, at the start of my main update() function, start a couple of threads in parallel, once they are all complete carry on with my main update function. void update() { thread1->update(); // fluid solver ... (3 Replies)
Discussion started by: memoid
3 Replies

9. Programming

Multithreading in Pro*C

:confused: Hi! I have created a Multhreaded Application in Pro*C (using pthreads) with about 5 Threads running simultaneously. The Application is basically to Update a Centralized Table in Oracle, which updates different rows in the Table (Each Thread updates different rows!). The... (16 Replies)
Discussion started by: shaik786
16 Replies
Login or Register to Ask a Question

Featured Tech Videos