Chinaunix首页 | 论坛 | 博客
  • 博客访问: 294847
  • 博文数量: 119
  • 博客积分: 1235
  • 博客等级: 少尉
  • 技术积分: 1056
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-08 19:59
文章分类

全部博文(119)

文章存档

2018年(2)

2017年(4)

2016年(4)

2015年(43)

2014年(1)

2013年(12)

2012年(16)

2011年(36)

2010年(1)

分类: 其他平台

2017-11-06 19:20:26

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#include
//将ts文件以组播方式发送,timeduration为ts文件播放时长。如: ./sendMCASTUdp 232.1.1.1 23456 64.125
#define BUFLEN 1024*1024
int main(int argc, char **argv)
{
    struct sockaddr_in peeraddr;
    int sockfd,ret;
    char* preaddata = malloc(BUFLEN);
    unsigned int socklen;

int filefd = -1,port;
char MCAST_IP[20],MCAST_PORT[10],filename[1024],length[15];
if(argc < 5)
{
printf("usage:sendMCASTUdp:ip port filename timeduration(seconds)\n");
return 0;
}
memset(MCAST_IP,0,20);
memset(MCAST_PORT,0,10);
memset(filename,0,1024);
memset(length,0,15);
strcpy(MCAST_IP,argv[1]);
strcpy(MCAST_PORT,argv[2]);
strcpy(filename,argv[3]);
strcpy(length,argv[4]);
port = atoi(MCAST_PORT);
filefd = open(filename,O_RDONLY);
if(filefd == -1)
{
printf("open file err,%s\n",strerror(errno));
return -1;
}    
printf("open file %s sucess \n",filename);


    /* 创建 socket 用于UDP通讯 */
    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (sockfd < 0) 
{
        perror("socket\n");
        exit(errno);
    }
    printf("socket created success!!!\n");
    socklen = sizeof(struct sockaddr_in);


    /* 设置对方的端口和IP信息 */
    memset(&peeraddr, 0, socklen);
    peeraddr.sin_family = AF_INET;
    peeraddr.sin_port = htons(port);


     if (inet_pton(AF_INET, MCAST_IP, &peeraddr.sin_addr) <= 0) 
{
            printf("wrong group address!\n");
            exit(0);
      }
int interval = 10;
int size = 0;
struct stat fileinfo;
stat(filename,&fileinfo);
size = fileinfo.st_size;
unsigned long int startt,endt,ms;
volatile unsigned long int tmpt;
long int sleept;
ms = atof(length)*1000;
int sendcount = (ms )/ interval;
int sendUnitlen = 1316;
int sendUnitnum = size/sendUnitlen;
float sendNumPer = (sendUnitnum * 1.00)/sendcount;
int i = 1,count = 0,totalsend = 0,beginpos = 0,tmppos = 0;


struct timeval tv;
gettimeofday(&tv,NULL);  
startt = tv.tv_sec * 1000 + tv.tv_usec/1000;
endt = startt + ms;
printf(" file size:%u, length:%u ms,sendcount:%d,sendUnitnum:%d,sendNumPer:%f\n",size,ms,sendcount,sendUnitnum,sendNumPer);
while(i <= sendcount)
{
memset(preaddata,0,BUFLEN);
beginpos = 0;
int lastread = (i-1)*sendNumPer;
int readcount = i*sendNumPer ;
readcount -= lastread;
int datalen = readcount * sendUnitlen;
ret = read(filefd,preaddata,datalen);
int tmpendcount = i * sendNumPer;
// printf("read size:%d,readcount:%d\n",ret,readcount);
while(beginpos < datalen)
{
ret = sendto(sockfd, preaddata+beginpos, sendUnitlen, 0,(struct sockaddr *)&peeraddr, sizeof(struct sockaddr_in));
if(ret < 0)
{
printf("sendto error!,%s\n",strerror(errno));
exit(errno);
}

totalsend += ret;
beginpos += sendUnitlen;
//count += 1;

}
gettimeofday(&tv,NULL);  
tmpt = tv.tv_sec * 1000 + tv.tv_usec/1000;
sleept= startt + i * interval - tmpt;
// printf("[%d]i:%d,read size %d,[%d]totalsend:%d,readcount:%d,tmpt:%lld,sleept:%d\n",sendcount,i,datalen,size,totalsend,readcount,tmpt,sleept);
if(sleept > 0)
{

struct timeval delay;
delay.tv_sec = 0;
delay.tv_usec = sleept * 1000; // 20 ms
select(0, NULL, NULL, NULL, &delay);
}
// else
// printf("sleept:%d,startt:%lld,tmpt:%lld\n",sleept,startt,tmpt);
i++;
}
// printf("===== totalsend:%d,left :%d\n",totalsend,size - totalsend);
int left = size - totalsend ;
memset(preaddata,0,BUFLEN);
beginpos = 0;
ret = read(filefd,preaddata,left);
ret = sendto(sockfd, preaddata+beginpos, sendUnitlen, 0,(struct sockaddr *)&peeraddr, sizeof(struct sockaddr_in));
left -= ret;
beginpos += ret;


gettimeofday(&tv,NULL);  
tmpt = tv.tv_sec * 1000 + tv.tv_usec/1000;
sleept = endt - tmpt;
if(sleept > 0)
{
printf("last sleep:%d\n",sleept);
struct timeval delay;
delay.tv_sec = 0;
delay.tv_usec = sleept * 1000; // 20 ms
select(0, NULL, NULL, NULL, &delay);
}
printf("total time:%d MS\n",time(NULL)*1000 -startt);


close(filefd);
    return 0;
}


#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include


#include


//接收udp组播ts流,并写入文件
#define MSGBUFSIZE 10240
//#define SO_REUSEPORT 15 


int main(int argc,char* argv[])
{
    struct sockaddr_in addr;
    int fd, nbytes, addrlen;
    struct ip_mreq mreq;
    char msgbuf[MSGBUFSIZE];
    int on;
int filefd = -1,port;
char MCAST_IP[20],MCAST_PORT[10],filename[40];
if(argc <= 1)
{
printf("usage:dumpMCASTUdp:ip port filename\n");
return 0;
}
memset(MCAST_IP,0,20);
memset(MCAST_PORT,0,10);
memset(filename,0,40);
strcpy(MCAST_IP,argv[1]);
strcpy(MCAST_PORT,argv[2]);
strcpy(filename,argv[3]);
port = atoi(MCAST_PORT);
filefd = open(filename,O_CREAT|O_RDWR|O_TRUNC,0X775);
if(filefd == -1)
{
printf("open file err,%s\n",strerror(errno));
return -1;
}    
printf("open file %s sucess \n",filename);
    if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    {
        perror("socket \n");
        return -1;
    }
printf("socket %d sucess \n",fd);


    on = 1;
    /* 设置地址复用许可, 根据具体情况判断是否增加此功能 */
    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
    {
        perror("SO_REUSEADDR \n");
        return -1;
    }
printf("setsockopt %d sucess \n",fd);



    /* 初始化地址 */
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(port);


    if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("bind \n");
        return -1;
    }
    printf("bind %d sucess \n",fd);




    /*加入多播组*/
    mreq.imr_multiaddr.s_addr = inet_addr(MCAST_IP);
    mreq.imr_interface.s_addr = htonl(INADDR_ANY);
    if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0)


    {
        perror("setsockopt \n");
        return -1;
    }
printf("setsockopt %d IP_ADD_MEMBERSHIP sucess \n",fd);


    addrlen = sizeof(addr);


nbytes = 0;
int total = 0;
int lastsize = 0;
int nowsize = 0,second =0,now;
second = time(NULL);


while (1)
{


    if ((nbytes = recvfrom(fd, msgbuf, MSGBUFSIZE, 0, (struct sockaddr *)&addr, (socklen_t *)&addrlen)) < 0)
    {
        perror("recvfrom \n");
        return -1;
    }


if(nbytes)
{
on = write(filefd,msgbuf,nbytes);
if(on == -1)
{
printf("write %d,fd:%d,%s\n",on,filefd,strerror(errno));
}
}
total += nbytes;
nowsize= total / 1000000;


if (nowsize - lastsize == 1)
{
now = time(NULL);
printf(" now file size:%d M, time elapse:%d seconds\n",nowsize,(now - second));
lastsize = nowsize;
}
}
    printf("recv ok!\n");
close(filefd);
    /*退出多播组*/
    setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
    close(fd);


    return 0;
}


阅读(1546) | 评论(0) | 转发(0) |
0

上一篇:描述符(socket)就绪(可读可写)的条件

下一篇:没有了

给主人留下些什么吧!~~