socket 编程实例

#include <stdio.h>        //printf()
#include <unistd.h>        //pause()
#include <signal.h>        //signal()
#include <string.h>        //memset()
#include <sys/time.h>    //struct itimerval, setitimer()
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include <bits/pthreadtypes.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <netdb.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>  

#define  BIT_RATE   3760000  //bps

#define  MAX_PATH_LENGTH  255
#define  MAX_NODE_NUM  100

#define MAXBUF 256
#define SEND_LENGTH   7*188

static char send_buf[SEND_LENGTH+1];
struct  commond
{
    struct sockaddr_in peeraddr;
    long   Byte_rate;
    char  path[MAX_PATH_LENGTH];
}config_parameter;

char * GetLocalIp(void)  
{  
    struct ifaddrs *ifaddr, *ifa;  
    int family, s;  
    char host[NI_MAXHOST];  
 
    if (getifaddrs(&ifaddr) == -1) {  
        perror("getifaddrs");  
        exit(EXIT_FAILURE);  
    }  
 
    /* Walk through linked list, maintaining head pointer so we
     *               can free list later */  
 
    for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {  
        if (ifa->ifa_addr == NULL)  
            continue;  
 
        family = ifa->ifa_addr->sa_family;  
 
        /* Display interface name and family (including symbolic
         *                   form of the latter for the common families) */  
 
        printf("%s  address family: %d%s\n",  
                ifa->ifa_name, family,  
                (family == AF_PACKET) ? " (AF_PACKET)" :  
                (family == AF_INET) ?   " (AF_INET)" :  
                (family == AF_INET6) ?  " (AF_INET6)" : "");  
 
        /* For an AF_INET* interface address, display the address */  
 
        if (family == AF_INET /* family == AF_INET6*/) {  
            s = getnameinfo(ifa->ifa_addr,  
                    (family == AF_INET) ? sizeof(struct sockaddr_in) :  
                    sizeof(struct sockaddr_in6),  
                    host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);  
            if (s != 0) {  
                printf("getnameinfo() failed: %s\n", gai_strerror(s));  
                exit(EXIT_FAILURE);  
            }  
            if((strncmp(ifa->ifa_name,"p5p1",4) == 0) ||(strncmp(ifa->ifa_name,"eth0",4) == 0))
            {
                printf("\t  we want address: <%s>\n", host);
                break;
            }
            
            printf("\taddress: <%s>\n", host);  
        }  
    }  
 
    freeifaddrs(ifaddr);  
    return host;
}  
int main(int argc, char * argv[])
{

    int    port,bit_rate,i;
    int    res,len;
    
    char    *ip;
    char    p_myaddr[256] = {0};

    int     fd_id;
    int     byte_per_10ms;
    char    *p_buffer;
    unsigned long diff;
    long    next_start,current_begin;
    int     times = 0,send_len = 0;
    
    int     fd;
    struct sockaddr_in srv,local;
    char buf[MAXBUF];
    struct timeval  start;
    struct timeval  current;
    
    if ( argc != 5 )
    {
        printf("parameter num is not rigth   :%d \n",argc);
        exit(EXIT_FAILURE);
    }

    if ( argv[1] )
    {
        len = strlen(argv[1]);
        if ( len > MAX_PATH_LENGTH )
        {
            printf("too deep  path ,failed   :%d\n",len);
            exit(EXIT_FAILURE);
        }
        memcpy(config_parameter.path, argv[1], len);
    }

    if ( argv[2] )
    {
        if ( inet_pton(AF_INET,argv[2],&config_parameter.peeraddr.sin_addr) < 0 )
        {
            printf("wrong  group address \n");
            exit(EXIT_FAILURE);
        }
        config_parameter.peeraddr.sin_family =AF_INET;
    }

    if ( argv[3] )
    {
        port = atoi(argv[3]);
        config_parameter.peeraddr.sin_port = htons(port);
    }

    if ( argv[4] )
    {
        bit_rate = atoi(argv[4]);
        printf("  %d \n ",bit_rate);
        config_parameter.Byte_rate = (bit_rate/8);
    }

 
    fd_id = open(config_parameter.path, O_RDONLY);
    if(fd_id == -1)
    {
        printf("creat or open failed \n");
        return 1;
    }
   
    
      ip = GetLocalIp();
    if( !ip )
    {
        printf("get local ip failed \n");
        exit(EXIT_FAILURE);
    }
       memcpy(p_myaddr,ip,32);   

    memset( &local, 0, sizeof(local) );
    memset( &srv, 0, sizeof(srv) );

    srv.sin_family = AF_INET;
    srv.sin_port = config_parameter.peeraddr.sin_port;//htons(PUERTO);
    srv.sin_addr = config_parameter.peeraddr.sin_addr;

    local.sin_family = AF_INET;
    local.sin_port = htons(16006);
    inet_aton(p_myaddr, &(local.sin_addr) );
    
    if( (fd = socket(AF_INET, SOCK_DGRAM, 0) ) < 0 ){
        perror("socket");
        return -1;
    }
    if( bind( fd, (struct sockaddr *)&local, sizeof(local) ) < 0 ){
        perror("bind:");
        return -1;
    }
    byte_per_10ms = (config_parameter.Byte_rate/100);

    printf("[producer_read] read byte_per_10ms  :%d 0 \n",byte_per_10ms);
    p_buffer = (char *)malloc(byte_per_10ms+1);
    if ( !p_buffer )
    {
        printf("[producer_read_file]   allocate  buffer failed \n");
        return ;
    }
    p_buffer[byte_per_10ms] = 0;

    char *p = NULL;
    long  send_length = 0;
    int   read_length;
    gettimeofday(&start, NULL);
    while ( 1 )
    {
        //perror(" read error   \n");
        //printf("read len :%d  send :%d \n",read_length,send_length);
        if ( (read_length = read(fd_id,p_buffer,byte_per_10ms)) == 0 )
        {
            lseek(fd_id, 0, SEEK_SET);
            gettimeofday(&start, NULL);
            times = 0;
            continue;
        }
        p = p_buffer;
        send_length = 0;
        while ( send_length < read_length)
        {
            len = SEND_LENGTH;
            if ((read_length - send_length) < SEND_LENGTH)
            {
                len = (read_length - send_length);
                
            }
            
            memcpy(send_buf,  p, len);
            send_buf[SEND_LENGTH] = 0;
            
            if( (send_len = sendto(fd, send_buf, len, 0, (struct sockaddr *)&srv, sizeof(srv))) < 0 )
            {
                perror("sendto  erro");
            }
            else
            {
                fprintf(stdout, "Enviado a   send: %d totole: %d    \n", send_len,byte_per_10ms);
            }
            
            send_length += SEND_LENGTH;
            p += SEND_LENGTH;
        }
        times++;       
        gettimeofday(&current, NULL);
        next_start =(1000000*start.tv_sec + (start.tv_usec + times *10000));
        current_begin =(1000000*current.tv_sec + current.tv_usec);
        if ( next_start > current_begin )
        {
            diff = next_start-current_begin;
        }
        else
        {
            diff = 0;
        }       
        printf("  sleep  time  %d  \n",diff);
        usleep(diff);        
    }
    free(p_buffer);
    printf("send SUCCESS \n");   
    return 0;            
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值