先看任务需求:
实验二 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:表示通讯结束,双方退出。
先画一下流程图方便理顺思路:
下面上代码:
1.定义了一个protocol结构体方便传输信息和信息校验等;
#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
2.客户端程序:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "protocol.h" /*-----------------------变量声明区------------------*/
int socketfd;
int addrlen;
struct sockaddr_in server;
struct protocol sentbuf;
struct protocol recvbuf;
int num;
char ip[];
int port;
int choice;
char filename[];
/*------------------------函数声明区------------------*/
void ShowMenu();
void DownLoad();
void UpLoad();
void ShutDown(); int main() { /*-------------create UDP socket------------*/
if((socketfd = socket(AF_INET,SOCK_DGRAM,)) == -){
perror("socket error\n");
exit();
} /*-----------------IO-----------------------*/
printf("Please input the ip:\n");
scanf("%s",ip);
printf("Please input the port:\n");
scanf("%d",&port); /*--------------------recvfrom and sendto----------*/
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip);
addrlen = sizeof(server); /*--------------------调试信息---------------------------
recvbuf.command = DOWNLOAD;
recvbuf.no =0;
printf("input the message:\n");
scanf("%s",recvbuf.buf);
recvbuf.len = strlen(recvbuf.buf);
sendto(socketfd,&recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&server,sizeof(server)); */ while (){
ShowMenu();
scanf("%d",&choice);
if(choice == SHUTDOWN){
ShutDown();
break;
}
else if(choice == DOWNLOAD){
DownLoad();
}
else if(choice == UPLOAD){
UpLoad();
}
else{
printf("Please make a right choice!\n");
}
} /*-----------------close------------------------*/
close(socketfd);
return ;
} void ShowMenu(){
printf("=====================================\n");
printf(" Please make a choice: \n");
printf(" 0: ||shutdown|| \n");
printf(" 1: ||download|| \n");
printf(" 2: || upload || \n");
printf("=====================================\n");
}
void DownLoad(){
bzero(&recvbuf,sizeof(recvbuf));
bzero(&sentbuf,sizeof(sentbuf));
bzero(filename,sizeof(filename));
printf("Please input the file name:\n");
scanf("%s",sentbuf.buf);
sentbuf.command = DOWNLOAD;
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&server,sizeof(server));
bcopy(sentbuf.buf,filename,strlen(sentbuf.buf));
recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&server,&addrlen);
if(recvbuf.command == YES){
printf("YES!\n");
printf("Start to transmission!\n");
sentbuf.command = START;
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&server,sizeof(server));
int no =;
int fd =open(filename,O_CREAT | O_TRUNC |O_WRONLY,);
if(fd < ){
perror("create file error\n");
exit();
}
bzero(&recvbuf,sizeof(recvbuf));
while((num = recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&server,&addrlen)) >){
if(recvbuf.command == CONTENT){
if(no == recvbuf.no){
write(fd,recvbuf.buf,recvbuf.len);
//printf("kkk%s\n",recvbuf.buf );
no++;
bzero(&recvbuf,sizeof(recvbuf));
}
else{
perror("The file no is not same.Some message is missed! error occured! \n");
break;
}
}
if(recvbuf.command == END){
close(fd);
printf("transmission is successful!\n");
break;
}
}
}
else if(recvbuf.command ==NO){
perror("NO such file on server!\n");
}
else{
perror("recvbuf.command error\n");
exit();
}
}
void UpLoad(){
bzero(&recvbuf,sizeof(recvbuf));
bzero(&sentbuf,sizeof(sentbuf));
bzero(filename,sizeof(filename));
printf("Please input the file name:\n");
scanf("%s",sentbuf.buf);
sentbuf.command = UPLOAD;
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&server,sizeof(server));
bcopy(sentbuf.buf,filename,strlen(sentbuf.buf));
//判断要传输的文件是否存在
int fd;
fd = open(filename,O_RDONLY);
if(fd < ){
perror("The file you want to trans is not exist!\n");
exit();
}
recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&server,&addrlen);
if(recvbuf.command == START){
int no =;
while((num = read(fd,sentbuf.buf,INFOLEN)) >){
sentbuf.no = no;
sentbuf.command = CONTENT;
sentbuf.len = strlen(sentbuf.buf);
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&server,sizeof(server));
no++;
bzero(&sentbuf,sizeof(sentbuf));
}
bzero(&sentbuf,sizeof(sentbuf));
sentbuf.command = END;
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&server,sizeof(server));
}
else if(recvbuf.command == NO){
printf("not transmission\n");
printf("The file is exist!\n");
}
else {
perror("error! wrong choice!\n");
}
} void ShutDown(){
printf("client is shutdown now!\n");
bzero(&sentbuf,sizeof(sentbuf));
sentbuf.command == SHUTDOWN;
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&server,sizeof(server));
}
3.服务器端程序:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "protocol.h" /*-----------------------变量声明区------------------*/
int socketfd;
int addrlen;
struct sockaddr_in server;
struct sockaddr_in client;
struct protocol sentbuf;
struct protocol recvbuf;
int num;
char ip[];
int port;
int choice; int main(){ /*-------------create UDP socket------------*/
if((socketfd = socket(AF_INET,SOCK_DGRAM,)) == -){
perror("socket error\n");
exit();
} /*-----------------IO-----------------------*/
printf("Please input the ip:\n");
scanf("%s",ip);
printf("Please input the port:\n");
scanf("%d",&port); /*-----------------bind----------------------*/
bzero(&server,sizeof(server));
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip);
if (bind(socketfd,(struct sockaddr *)&server,sizeof(server)) == -){
perror("bind error\n");
exit();
} /*---------------------调试信息------------
addrlen = sizeof(client);
recvfrom(socketfd,&recvbuf,sizeof(recvbuf),0,(struct sockaddr *)&client,&addrlen);
num = strlen(recvbuf.buf);
recvbuf.buf[num] = '\0';
printf("command %d\n",recvbuf.command );
printf("len %d\n",recvbuf.len );
printf("no %d\n", recvbuf.no);
printf("buf %s\n",recvbuf.buf ); */ addrlen = sizeof(client);
while(){
bzero(&recvbuf,sizeof(recvbuf));
num =recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&client,&addrlen);
choice = recvbuf.command;
if(choice == DOWNLOAD){
char buf[];
int fd;
fd = open((recvbuf.buf),O_RDONLY);
if(fd <){
sentbuf.command = NO;
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
printf("no such file!\n");
exit();
}
else{
sentbuf.command = YES;
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&client,&addrlen);
if(recvbuf.command == START){
int no =;
while((num = read(fd,sentbuf.buf,INFOLEN)) >){
sentbuf.no = no;
sentbuf.command = CONTENT;
sentbuf.len = strlen(sentbuf.buf);
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
no++;
bzero(&sentbuf,sizeof(sentbuf));
}
bzero(&sentbuf,sizeof(sentbuf));
sentbuf.command = END;
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
}
}
}
else if(choice == UPLOAD){
int fd1= open(recvbuf.buf,O_RDONLY);
if (fd1 < ){
sentbuf.command = START;
}
else{
sentbuf.command = NO;
}
close(fd1);
sendto(socketfd,&sentbuf,sizeof(sentbuf),,(struct sockaddr *)&client,sizeof(client));
if(sentbuf.command ==START){
int no =;
int fd =open(recvbuf.buf,O_CREAT | O_TRUNC |O_WRONLY,);
if(fd < ){
perror("create file error\n");
exit();
}
bzero(&recvbuf,sizeof(recvbuf));
while((num = recvfrom(socketfd,&recvbuf,sizeof(recvbuf),,(struct sockaddr *)&server,&addrlen)) >){
if(recvbuf.command == CONTENT){
if(no == recvbuf.no){
write(fd,recvbuf.buf,recvbuf.len);
//printf("kkk%s\n",recvbuf.buf );
bzero(&recvbuf,sizeof(recvbuf));
}
else{
perror("The file no is not same.Some message is missed! error occured! \n");
break;
}
}
if(recvbuf.command == END){
close(fd);
printf("transmission is successful!\n");
break;
}
}
}
else if(sentbuf.command == NO){
printf("Not to trans the file\n");
printf("The file is exist!\n");
}
else{
perror("error! wrong choice!\n");
exit();
} }
else if (recvbuf.command == SHUTDOWN){
printf("Now the server is shutdown!\n");
break;
}
} /*----------------------close ----------*/
close(socketfd);
return ;
}
4.makefile文件;
main:udpserver.o udpclient.o
gcc -o udpserver udpserver.o
gcc -o udpclient udpclient.o
udpserver.o:udpserver.c protocol.h
gcc -c udpserver.c
udpclient.o: udpclient.c protocol.h
gcc -c udpclient.c
clean:
rm -rf *.o
代码写的挺烂的,里面有很多重复的代码和功能,完全可以抽取出来做成一个函数的。以后慢慢再改吧。先完成功能,把上机考试过了再说。