#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#define IP "192.168.31.205"
#define PORT 69
#define ERR_MSG(msg) do{\
fprintf(stderr,"line:%d",__LINE__);\
perror(msg);\
}while(0)
int do_download(int sfd,struct sockaddr_in sin);
int do_upload(int sfd,struct sockaddr_in sin);
int main(int argc, const char *argv[])
{
int sfd = socket(AF_INET,SOCK_DGRAM,0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("sfd = %d\n",sfd);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(PORT);
sin.sin_addr.s_addr = inet_addr(IP);
char choose = 0;
while(1)
{
printf("=========================\n");
printf("=========1.下载==========\n");
printf("=========2.上传==========\n");
printf("=========3.退出==========\n");
printf("=========================\n");
printf("请输入\n");
choose = getchar();
while(getchar()!=10);
switch(choose)
{
case '1':
do_download(sfd,sin);
break;
case '2':
do_upload(sfd,sin);
break;
case '3':
goto END;
break;
default:
printf("错误,请重新输入\n");
break;
}
}
END:
close(sfd);
return 0;
}
int do_download(int sfd,struct sockaddr_in sin)
{
char buf[516]="";
char filename[20]="";
ssize_t rev = -1;
unsigned short num = 0;
socklen_t addrlen = sizeof(sin);
printf("输入要下载的文件名字");
scanf("%s",filename);
while(getchar()!=10);
int size=sprintf(buf,"%c%c%s%c%s%c",0,1,filename,0,"octet",0);
if(sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
int fd = -1;
while(1)
{
bzero(buf,sizeof(buf));
rev = recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
if(rev<0)
{
ERR_MSG("recvfrom");
return -1;
}
if(3==buf[1])
{
if(htons(num+1)==*(unsigned short*)(buf+2))
{
num++;
if(fd==-1)
{
fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd<0)
{
ERR_MSG("open");
return -1;
}
}
if(write(fd,buf+4,rev-4)<0)
{
ERR_MSG("write");
return -1;
}
buf[1]=4;
if(sendto(sfd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
if(rev-4<512)
{
printf("下载完毕\n");
break;
}
}
}
else if(5 == buf[1])
{
printf("文件不存在\n");
return -1;
}
}
close(fd);
return -1;
}
int do_upload(int sfd,struct sockaddr_in sin)
{
char buf[516]="";
char filename[20]="";
printf("请输入需要上传的文件名:");
scanf("%s",filename);
while(getchar()!=10);
int fd=open(filename,O_RDONLY);
if (fd<0)
{
ERR_MSG("open");
return -1;
}
//发送上传请求
int size=sprintf(buf,"%c%c%s%c%s%c",0,2,filename,0,"octet",0);
if (sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin))<0)
{
ERR_MSG("sendto");
return -1;
}
socklen_t addrlen=sizeof(sin);
ssize_t res=0;
unsigned short num=0;
while(1)
{
bzero(buf,sizeof(buf));
res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
if (res<0)
{
ERR_MSG("recvfrom");
}
if (4==buf[1])
{
if(htons(num)==*(unsigned short*)(buf+2))
{
num++;
bzero(buf,sizeof(buf));
if((res=read(fd,buf+4,512))<0)
{
ERR_MSG("write");
}
buf[1]=3;
*(short*)(buf+2)=htons(num);
if(sendto(sfd,buf,res+4,0,(struct sockaddr*)&sin,addrlen)<0)
{
ERR_MSG("sendto");
}
if (res<512)
{
puts("上传完毕");
break;
}
}
}
else if (5==buf[1])
{
printf("%s\n",buf+4);
break;
}
}
close(fd);
return 0;
}
网络编程2023-3-14作业
最新推荐文章于 2024-11-02 15:22:06 发布
该程序实现了一个简单的UDP文件传输客户端,用户可以选择下载或上传文件。客户端通过socket与服务器建立连接,使用UDP协议进行数据传输。在下载过程中,它接收服务器分块发送的数据并写入文件;在上传时,它先发送文件名请求,然后读取文件并分块发送给服务器。
摘要由CSDN通过智能技术生成