简单视频网络传输客户端
#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);
}