简单视频网络传输客户端

简单视频网络传输客户端

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


#define PORT 4567                 
#define LENGTH 1024                

int main(int argc, char *argv[]){
   
   int    fd,sockfd,numbytes;   
   int i;
//	int	 addrlen;                                    
	unsigned char   buf[LENGTH];
	char   msg[]={"hello"};  
	socklen_t addrlen;           						
	struct sockaddr_in addr_sev;  
	struct sockaddr_in addr_cli;             
   //struct hostent     *he;

   if (argc != 2)
   	{                          
      printf("Please input server IP!\n");
      exit(1);
 	}
   fd=open("/home/sf6/jieen/recv.ts",O_CREAT|O_WRONLY); 
   if(fd<0)
   	{           
	      printf("open file error!\n");
	      exit(1);
    } 

    /*if((he=gethostbyaddr((char *)&addr_sev.sin_addr,4,AF_INET)) == NULL){
      printf("get error\n");
      exit(1);
          }  */
   if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
   	{
       printf("creat socket error!\n");
       exit(1);
    }
   bzero(&(addr_cli), sizeof(addr_cli));                       
   addr_cli.sin_family = AF_INET;                        
   addr_cli.sin_port = htons(PORT);
	
	addrlen = sizeof(struct sockaddr);                      
   //addr_sev.sin_addr=*((struct in_addr *)he->h_addr);  

	if(inet_aton(argv[1],&addr_cli.sin_addr)<0)
	{
		printf("IP convert error!\n");
		exit(1);
	}    
  	printf("Receiving data please wait......\n");
   //bzero(buf,LENGTH);
            
	while(1){
		if(sendto(sockfd, msg, strlen(msg), 0, (struct sockaddr *)&addr_cli, sizeof(addr_cli))==-1)
		{
	      	printf ("sendto fail!\n");
	      	exit(1);
		}

      numbytes=recvfrom(sockfd,buf,LENGTH,0,(struct sockaddr *)&addr_sev,&addrlen);
		if(numbytes<0){
			printf("download from server error\n"); 			
			exit(1);
		}
		printf("\nCurent buf is :\n");
		for(i=0;i<numbytes;i++)
		{
			printf("%02x ",buf[i]);
		}
		printf("\n");
		sleep(5);
	   if((write(fd,buf,numbytes))<0){
			printf("write fail!\n");
			exit(1);	      
               }    
		if(numbytes != LENGTH){
			printf("Receive data complete!\n");
			break;
		}

//		close(sockfd); 
//		close(fd);  
	}
	exit(0);
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值