日期:2014-05-16  浏览次数:20751 次

Linux下的udp广播与接收程序 包含序列号

[代码] udp_send.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include <time.h>
#include <sys/time.h>
#include <errno.h> 

#define PORT 5555
#define RECV_NO 250
//#define	RECV_TIMES 10

typedef struct sockaddr ADDR;

int main(int argc, char *argv[])
{
	ssize_t n;
	int serverfd,port;
	int k=0;
	struct sockaddr_in myaddr,peeraddr;
	struct timeval tv; 
	struct timeval tvnow,tvnowb;
	
	FILE * fp; 
	extern int errno;  
	char file[]="handle_delay_test1_send.txt"; /*需要各自修改*/
	int on = 1;
	char buf[124]= {};

	struct Record 
	{ 		
		time_t sec; 
		suseconds_t usec; 
	};
	struct Record record[RECV_NO+1];

	if(argc < 1)
	{
		printf("the usage :%s",argv[0]);
		exit(-1);
	}
	if((serverfd = socket(PF_INET,SOCK_DGRAM,0)) < 0)
	{
		perror("fail t socket");
		exit(-1);
	}

	myaddr.sin_family = PF_INET;
	myaddr.sin_port = htons(PORT);
	myaddr.sin_addr.s_addr = inet_addr("10.42.0.255");
	
	setsockopt(serverfd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on));
	
	socklen_t peerlen = sizeof(peeraddr);
	

	gettimeofday(&tvnow,NULL); 
  	printf("nowtime %ld:%ld\n",tvnow.tv_sec,tvnow.tv_usec);

	memset(buf,0,sizeof(buf));

	k=0;
	memset(buf,k,1);

	while(k<RECV_NO)
	{
		gettimeofday(&tv, NULL);
		if(tv.tv_sec==tvnow.tv_sec+1&&tv.tv_usec<100000)//每1s发送一次广播
		{
			sendto(serverfd,buf,sizeof(buf),0,(ADDR *)&myaddr,16);
			gettimeofday(&tvnow,NULL);
			printf(" %d send_after time %ld:%ld %d B\n",k+1,tvnow.tv_sec,tvnow.tv_usec,sizeof(buf));
			record[k].sec=tvnow.tv_sec;
			record[k++].usec=tvnow.tv_usec;

			memset(buf,0,sizeof(buf));
			memset(buf,k,1);//报文中包含序号

			printf("buff=%s\n",buf);
		}	
	}
	close(serverfd);

	fp=fopen(file,"a+"); //数据存储到文件中
	if(fp==NULL) 
	{ 
		printf("cant't open file %s.\n",file); 
		printf("errno:%d\n",errno); 
		printf("ERR  :%s\n",strerror(errno)); 
		return; 
	} 
	else 
	{ 
		printf("%s was opened.\n",file);       
	} 
	for(k=0;k<RECV_NO;k++)
	{
		fprintf(fp,"%d send_after time %ld:%ld \n",k+1,record[k].sec,record[k].usec);
	}
	fprintf(fp,"\n");//分开每组数据
	printf("%d records was written into %s.\n",k,file);
	fclose(fp);
	
	return 0 ;
}

[代码] udp_receive.c

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<fcntl.h>
#include<unistd.h>
#include <errno.h> 

#define RECV_NO 250

typedef struct sockaddr ADDR;

int main(int argc, char *argv[])
{
	ssize_t n;
	char buf[2];
	int k=0,serverfd,nread=0;
	struct sockaddr_in myaddr,peeraddr;
	struct timeval tv;
	FILE * fp; 
	extern int errno;  
	char file[]="handle_delay_test1_recv.txt"; 
	
	struct Record 
	{ 
		time_t sec; 
		suseconds_t usec;  
	};
	struct Record record[RECV_NO+1];

	if(argc < 1)
	{
		printf("the usage :%s",argv[0]);
		//printf("the usage :%s ip port",argv[0]);
		exit(-1);
	}
	if((serverfd = socket(PF_INET,SOCK_DGRAM,0)) < 0)
	{
		perror("fail t socket");
		exit(-1);
	}
	myaddr.sin_family = PF_INET;
	myaddr.sin_port =htons(5555);		//绑定端口监听
	myaddr.sin_addr.s_addr = INADDR_ANY;	//系统