#include <stdio.h>
#include <unistd.h>

#include "udp_t.h"

void *thread_udprcv(void *args)
{
    thread_udpsvr_arg* para = (thread_udpsvr_arg*)args;
    
    int     nr = 0;
    udp_t   *udp = NULL;
    char    msg[UDPBUFSIZE] = { 0 };
    unsigned char buff[UDPBUFSIZE]={0};
    
    while(1)
    {
        udp = openudpsvr(para->path, msg);
        if (udp == NULL)
        {
            printf(" udp server open failed, path = %s, msg = %s\n", para->path, msg);        
        }
        
        while((nr = readudpsvr(udp, buff, UDPBUFSIZE, msg)) != -1)
        {
            printf("%s", buff);
            
            if (para->handle)   para->handle(buff, nr);
        }
        
        closeudpsvr(udp);
    }
    
    return NULL;
}

int main(int argc, char *argv[])
{
    const char *udppath = "192.168.2.180:4001";
    
    pthread_t           thread_id;
    thread_udpsvr_arg   thread_arg;
    
    
    strcpy(thread_arg.path, udppath);
    
    
    if (pthread_create(&thread_id, NULL, thread_udprcv, (void*)&thread_arg))
    {
        printf(" thread create error\n");
        return 0;
    }

    pthread_join(thread_id, NULL);
    return 0;
}
