实验二 UDP数据发送与接收
【实验目的】
1、熟练掌握套接字函数的使用方法。
2、应用套接字函数完成基本UDP通讯,实现服务器与客户端的文件传送
【实验学时】
4学时
【实验内容】
要求:
(1)客户可以从服务器下载文件、或向服务器上传文件。
(2)客户可向服务器发送多种指令:DOWNLOAD、UPLOAD、YES、NO、START、END、SHUTDOWN、CONTENT、OKDOWLOAD格式:DOWLOAD [filename]表示从服务器下载filename文件,如果服务器存在该文件,返回YES,否则返回NO;客户接收如果是YES,可发送START表示开始下载,之后,服务器将文件传送给客户,客户接收并保存;UPLOAD格式:UPLOAD [filename]表示向服务器上传filename文件,服务器发送NO表示拒绝接收。服务器发送START表示开始传送,之后向服务器传输文件;END:表示文件传送结束SHUTDOWN:表示通讯结束,双方退出。
实验流程如下:
protocol.h:
#ifndef _protocol #define _protocol #define INFOLEN 1000 #define SHUTDOWN 0 #define DOWNLOAD 1 #define UPLOAD 2 #define YES 3 #define NO 4 #define START 5 #define END 6 #define CONTENT 7 #define OK 8 struct protocol { int command; int len; //length of buf int no; char buf[INFOLEN]; }; #endif
server:
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<unistd.h> #include"protocol.h" int main() { int sockfd; struct sockaddr_in server; struct sockaddr_in client; int len=sizeof(client); int port; char ip[20]; //sock sockfd=socket(AF_INET, SOCK_DGRAM, 0); printf("client ip: "); scanf("%s", ip); printf("server port: "); scanf("%d", &port); bzero(&server, sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(port); server.sin_addr.s_addr=inet_addr(ip); //bind bind(sockfd, (struct sockaddr *)&server, sizeof(server)); //sendto/recvfrom struct protocol pro; while(1) { int a; int num=recvfrom(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server,&len); printf("command= %d ",pro.command); int i=pro.command; switch(i) { case 1: a=access(pro.buf,F_OK); if(a!=-1) { pro.command=3; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); num=recvfrom(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server,&len); if(pro.command==5) { FILE *fd; pro.no=0; fd=fopen(pro.buf,"r+"); if(fd==NULL) { perror("create file error"); exit(1); } else { int f; while((f=fread(pro.buf,1,1000,fd)) !=0) { pro.command = 7; pro.len=f; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); pro.no++; bzero(&pro.buf,1000); } bzero(&pro.buf,1000); pro.command = 6; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); } } } else { printf("no such file! "); } case 2: num=recvfrom(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server,&len); pro.command=3; int no=0; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); FILE *fd; fd=fopen(pro.buf,"w+"); if(fd==NULL) { perror("create file error"); exit(1); } else{ while((num=recvfrom(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server,&len))>0) { if(pro.command==7&&pro.no==no) { fwrite(pro.buf,1,pro.len,fd); no++; } else if(pro.command==6) { break; } bzero(&pro.buf,1000); } } fclose(fd); break; } } //close close(sockfd); return 0; }
client:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <netinet/in.h> #include <sys/socket.h> #include <sys/types.h> #include "protocol.h" int main() { int sockfd; struct sockaddr_in server; int len=sizeof(server); int port; char ip[20]; //sock if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1) { perror("socket error"); exit(1); } printf("server ip:"); scanf("%s",ip); printf("server port:"); scanf("%d",&port); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(port); server.sin_addr.s_addr=inet_addr(ip); //sendto/recvfrom char buf[100]; int num; while(1) { printf("------------------------------ "); printf("--------input a number-------- "); printf("--------1:DOWNLOAD------------ "); printf("--------2:UPLOAD-------------- "); printf("--------3:SHUTDOWN------------ "); printf("------------------------------ "); int s; char p[10]; printf("input you switch :"); scanf("%d",&s); int shut=0; struct protocol pro; switch(s) { case 1: printf("input download filename:"); scanf("%s",pro.buf); pro.command=1; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); num=recvfrom(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server,&len); if(pro.command==3) { pro.command=5; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); FILE *fd; fd=fopen(pro.buf,"w+"); int no=0; if(fd==NULL) { perror("create file error"); exit(1); } else { bzero(&pro.buf,1000); while((num=recvfrom(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server,&len))>0) { if(pro.command==7&&pro.no==no) { fwrite(pro.buf,1,pro.len,fd); } else if(pro.command==6) { printf("file downover "); break; } no++; } } fclose(fd); } else printf("from server:no file "); break; case 2: len=sizeof(server); bzero(&pro.buf,1000); printf("input Upload filename:"); scanf("%s",pro.buf); pro.command=2; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); num=recvfrom(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server,&len); printf("command %d ",pro.command); if(pro.command==3) { FILE *fd; pro.no=0; fd=fopen(pro.buf,"r+"); if(fd==NULL) { perror("create file error"); exit(1); } else { int f; bzero(&pro.buf,1000); while((f=fread(pro.buf,1,1000,fd))!=0) { pro.command=7; pro.len=f; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); pro.no++; } pro.command=6; sendto(sockfd,(struct protocol *)&pro,sizeof(struct protocol),0,(struct sockaddr *)&server ,sizeof(server)); } fclose(fd); } break; case 3: exit(1); break; default: printf("input a right number "); } } close(sockfd); return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。