⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.c

📁 开源的组播程序
💻 C
字号:
#include <stdio.h>#include <stdlib.h>#include <time.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <fcntl.h>#include <time.h>#include <string.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h>#include <rs.h>#define NN 255#define SYMSIZE 8#define GENPOLY 0x187#define FCS 1#define PRIM 1#define NROOTS 8#define PAYLOAD 1000unsigned int send_packet (unsigned char * packet, unsigned int index,		unsigned char * destination);struct pmtp_header {	unsigned char tigertree[160];	unsigned int filesize;	unsigned int segmentsize;	unsigned int sequence;	unsigned int symsize;	unsigned int genpoly;	unsigned int fcs;	unsigned int prim;	unsigned int nroots;};	int main () {	unsigned char *source, *destination, *copy;	unsigned char *packets, sourcepacket[PAYLOAD];	unsigned char block[NN];	unsigned int blocksize = (NN - NROOTS) * PAYLOAD;	int i, index;	int errlocs[NROOTS];	int erasures = 0;	int derrors;	void *handle;	int bytes_read;	unsigned char const filename[] = "/home/steven/pmtp/weezer-hashpipe.mp3";	int fdin, fdout;	struct hostent *hp;	struct servent *sp;	struct sockaddr_in sin;	int s;	if ((s = socket (AF_INET, SOCK_DGRAM, 0)) < 0) {		perror ("socket");		exit (1);	}	if ((sp = getservbyname ("pmtp", "udp")) == NULL) {		fprintf (stderr, "%s/udp: unknown service\n", "pmtp");		exit (1);	}	if ((hp = gethostbyname ("PII")) == NULL) {		fprintf (stderr, "%s: host unknown.\n", "PII");		exit (1);	}	sin.sin_family = AF_INET;	sin.sin_port = sp->s_port;	bcopy (hp->h_addr, &sin.sin_addr, hp->h_length);	srandom(time(NULL));	printf ("Code (%d / %d)\n", NN, NROOTS);	if((handle = init_rs_char(SYMSIZE,GENPOLY,FCS,PRIM,NROOTS)) == NULL) {		printf("init_rs_int failed!\n");		exit (1);	}	if ((source = (unsigned char*) malloc (PAYLOAD * (NN-NROOTS))) == NULL) {		printf ("Couldn't malloc %d bytes for source\n",			PAYLOAD * (NN - NROOTS));		exit (1);	}	if ((packets = (unsigned char*) malloc (PAYLOAD * NN)) == NULL) {		printf ("Couldn't malloc %d bytes for packets\n", PAYLOAD * NN);		exit (1);	}	if ((destination = (unsigned char*) calloc (PAYLOAD * NN, 1)) == NULL) {		printf ("Couldn't malloc %d bytes for destination\n", PAYLOAD * NN);		exit (1);	}	if ((copy = (unsigned char*) malloc (blocksize)) == NULL) {		printf ("Couldn't malloc %d bytes for destination\n", blocksize);		exit (1);	}	if ((fdin = open(filename, O_RDONLY)) < 0) {		printf ("Error opening %s for reading\n", filename);		exit (1);	}	if ((fdout = open ("/home/steven/test.out", O_WRONLY | O_CREAT,			00755 )) < 0) {		printf ("Error opening '/home/steven/test.out' for writing\n");		exit (1);	}	while ((bytes_read = read(fdin, source, blocksize)) != 0) {		erasures = 0;		for (index = 0; index < PAYLOAD; index++) {			memcpy (block, (source + index * (NN-NROOTS)), NN-NROOTS);			encode_rs_char (handle, block, &block[NN-NROOTS]);			for (i = 0; i < NN; i++) {				*(packets + PAYLOAD * i + index) = block[i];			}		}		for (i = 0; i < NN; i++) {			memcpy (sourcepacket, packets + i * PAYLOAD, PAYLOAD);			if (send_packet (sourcepacket, i, destination)) {				if (erasures < NROOTS) {					errlocs[erasures] = i;				}				erasures++;			} else {				if (sendto (s, copy, PAYLOAD, 0,						(struct sockaddr *) &sin, sizeof(sin)) < 0) {					perror ("sendto");					exit (1);				}			}		}/*		if (erasures > NROOTS) {			printf ("Exiting because there were %d packets lost\n", erasures);			exit (1);		}*/		for (i = 0; i < PAYLOAD; i++) {			for (index = 0; index < NN; index++) {				block[index] = *(destination + index * PAYLOAD + i);			}			if ((derrors = decode_rs_char (handle, block, errlocs, erasures))					== -1) {				printf ("Decode failed!\n");				exit (1);			}			memcpy (copy + i * (NN - NROOTS), block, NN - NROOTS);		}		write (fdout, copy, bytes_read);		if (memcmp (copy, source, PAYLOAD * (NN - NROOTS))) {			printf ("Decode failed with %d lost packets\n",erasures);		} else {			printf ("Seemed to have worked with %d lost packets\n", erasures);		}	}	free (copy);	close (fdout);	close (fdin);	free (source);	free (packets);	free (destination);	return 0;}unsigned int send_packet (unsigned char * packet, unsigned int index,		unsigned char * destination) {	if ((random() & 255) < 10) {		printf ("Aiyeeee, lost packet %d!\n", index);		return 1;	}	memcpy (destination + index * PAYLOAD, packet, PAYLOAD);	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -