简单的一个视频网络传输服务端

简单的一个视频网络传输服务端

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <linux/tcp.h>
#include <linux/ip.h>

#define URL_LEN     256
#define ALIGN_4096   (4096 - 1)
#define BUF_SIZE 	(188*6)
#define NUM_BUF_DESC 	24
#define O_DIRECT	0x8000


double difftime1(struct timeval *start, struct timeval *stop) 
{
	double dt = 1000000.*(stop->tv_sec - start->tv_sec) + (stop->tv_usec - start->tv_usec);
	return dt;
}

int main()
{
    struct sockaddr_in dest;
    int c, i, j;
    int sfd, payload_len;
    char *srcip = "10.0.14.188";
    char *dstip = "10.0.1.37";
    int dstport = 15888;
    char fileName[128];
    int fd = -1;
    unsigned char *xbuf, *buf;
    int bufLen = BUF_SIZE; 
    int len; 
    unsigned long count = 0;
	loff_t bytes= 0;
	struct timeval start, stop;
	double rate = 0,dt=0,maxrate = 10;
    FILE *file;
    struct iovec iov[NUM_BUF_DESC];
    int nextBuf;
    char pingStr[256];
	int protocol = 0;
    int encrypt = 0;
    int loop = 1;
    int settopInit = 0;
	int forceSpeed = 1;
	int pktDrop = 0;
	int socket_type;
	int verbose = 0;
    int send_from_memory = 0;

    for (i=0; i<NUM_BUF_DESC; i++) {
        if ( (xbuf = malloc(BUF_SIZE + ALIGN_4096)) == NULL ) {
            perror("posix_memalign failure():");
            goto exit;
        }
        iov[i].iov_base = (unsigned char *)(((unsigned long)xbuf + ALIGN_4096) & ~ALIGN_4096);
        iov[i].iov_len = BUF_SIZE;
    }
	printf("Allocated %d-bytes for buffering, DESC=%d\n",NUM_BUF_DESC*(BUF_SIZE + ALIGN_4096),NUM_BUF_DESC);

	if (send_from_memory) {
    	for (i=0; i<NUM_BUF_DESC; i++) 
			memset(iov[i].iov_base, 0xff, BUF_SIZE);
	}
	else {

    	if ( (fd = open("10070322.ts", O_RDONLY|O_DIRECT)) <= 0) {
        	perror("open():");
        	goto exit;
    	}
    	printf("Content File opened\n");
	}

	socket_type = SOCK_DGRAM;
    if ( (sfd = socket(PF_INET, socket_type, 0)) < 0 ) {
        perror("socket():");
        goto exit;
    }


	/* increase the socket receive buffer */
	int rcv_buf = 600000;   
	if (setsockopt( sfd, SOL_SOCKET, SO_SNDBUF, (const char*)&rcv_buf, sizeof(rcv_buf) ) < 0 ){
		perror("SO_RCVBUF Socket Error:\n");
		return -1;
	}
	

    /* initialize the destination address */
    dest.sin_family = AF_INET;
    dest.sin_port = htons(dstport);
    dest.sin_addr.s_addr = inet_addr(dstip);

	/* need to get the arp resolution done for the destination host */
    if (!IN_MULTICAST(ntohl(dest.sin_addr.s_addr))) {
        snprintf(pingStr, sizeof(pingStr), "ping -q -c 1 %s > /dev/null", dstip);
        system(pingStr);
    }
   
	/* streaming time... */
	gettimeofday(&start,NULL);
    nextBuf = 0;

	while(1) {

        buf = (unsigned char *)iov[nextBuf++].iov_base;

        nextBuf %= NUM_BUF_DESC;

		if (send_from_memory) 
			goto send_data;

		/* read the next block */
		if ( (bufLen = read(fd, buf, BUF_SIZE)) < 0 ) {
            perror("read():");
            goto exit;
        }
        else if (bufLen == 0) {
            printf("**** Reached EOF *******\n");
            if (loop) {
                lseek(fd, 0, SEEK_SET); 
                continue;
            }
            break;
        }
        
send_data:
        /* send the data */
        if ( (bytes += sendto(sfd, buf, bufLen, 0, (struct sockaddr *)&dest, sizeof(struct sockaddr_in))) <= 0) {
            perror("sendto():");
            goto exit;
        }
			usleep(2000);
        /* simple pacing logic */ 
		count++;
		gettimeofday(&stop,NULL);
		dt = difftime1(&start,&stop);
		rate = 8.*bytes/dt;
		while(rate > maxrate) {
			usleep(10000);
			gettimeofday(&stop,NULL);
			dt = difftime1(&start,&stop);
			rate = 8.*bytes/dt;
		} 

		if(verbose && (count % 100) == 0) {
			printf("%u (%u) Packets (Bytes) dt=%lf rate=%lf/%lf written\n", count, bytes, dt, rate, maxrate);  
		}
	}

exit:
    for (i=0; i<NUM_BUF_DESC; i++)
	    free(iov[i].iov_base);
	printf("Final stats: %u (%u) Packets (Bytes) dt=%lf rate=%lf/%lf written\n", count, bytes, dt, rate, maxrate);  
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值