server.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char** argv)
{
struct sockaddr_in sin;
struct sockaddr_in rin;
int sockfd;
int address_size;
char buf[1024];
char sbuf[1];
sbuf[0]='a';
char str[INET_ADDRSTRLEN];
int i;
int len;
int n;
bzero(&sin,sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(9999);
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(sockfd == -1)
{
perror("socket error");
return -1;
}
n=bind(sockfd,(struct sockaddr*)&sin,sizeof(sin));
if(n==-1)
{
perror("bind error");
return -1;
}
FILE *fp;
if((fp=fopen("/mnt/2010.mpg","w+"))==NULL)
{
perror("error\n");
return 0;
}
while(1)
{
address_size = sizeof(rin);
n=recvfrom(sockfd,buf,sizeof(buf),0,(struct sockaddr*)&rin,&address_size);
if(n==-1)
{
perror("recvfrom error");
return -1;
}
fwrite(buf,1,n,fp);
bzero(buf,sizeof(buf));
n=sendto(sockfd,sbuf,1,0,(struct sockaddr*)&rin,address_size);
if(n==-1)
{
perror("sendto error");
return -1;
}
}
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc,char** argv)
{
struct sockaddr_in pin;
struct sockaddr_in rin;
int sockfd;
char buf[1024];
char str[1024];
char sip[INET_ADDRSTRLEN];
int n;
int address_size;
bzero(&pin,sizeof(pin));
pin.sin_family = AF_INET;
pin.sin_addr.s_addr =inet_addr("192.168.0.210");
pin.sin_port = htons(9999);
sockfd = socket(AF_INET,SOCK_DGRAM,0);
if(sockfd == -1)
{
perror("socket error");
return -1;
}
FILE* fp;
if((fp=fopen("/home/leaf/2010.mpg","r+"))==NULL)
{
perror("error\n");
return 0;
}
printf("1\n");
do
{
n=fread(str,1,1024,fp);
if(n>0)
{
sendto(sockfd,str,n,0,(struct sockaddr*)&pin,sizeof(pin));
}
if(feof(fp))
{
perror("play over\n");
break;
}
socklen_t address_size = sizeof(rin);
recvfrom(sockfd,buf,1,0,(struct sockaddr*)&rin,&address_size);
bzero(str,sizeof(str));
}
while(1);
close(sockfd);
return 0;
}
阅读(1647) | 评论(0) | 转发(0) |