文章目录
- 一、思维导图
- 二、基于UDP的TFTP文件传输
- 三、牛客网刷题
一、思维导图

二、基于UDP的TFTP文件传输
1、myhead.h
#ifndef __MYHEAD_H__
#define __MYHEAD_H__#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>#define ERR_MSG(msg) do{perror(msg);printf("%d\n",__LINE__);return -1;}while(0)
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <pwd.h>
#include <grp.h>
#include <sys/wait.h>
#include <pthread.h>
#include <semaphore.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/msg.h>
#endif
2、客户端代码
#include<myhead.h>#define SER_IP "192.168.109.125"
#define SER_PORT 69
#define CLI_IP "192.168.117.113"
#define CLI_PORT 9999
void dowload(int cfd,struct sockaddr_in sin,socklen_t addrlen)
{char msgBuf[516] = ""; char fileName[21] = ""; printf("请输入您要下载的文件名:");scanf("%s", fileName);short *p1 =(short *)msgBuf; *p1 = htons(1); char *p2 = msgBuf + 2; strcpy(p2, fileName);char *p4 = p2 + strlen(p2) + 1; strcpy(p4, "octet");int msgLen = 2 + strlen(p2) + strlen(p4) +2; sendto(cfd, msgBuf, msgLen, 0, (struct sockaddr*)&sin, sizeof(sin));ssize_t len;umask(0);int fd=open(fileName,O_WRONLY | O_CREAT | O_TRUNC,0664);while(1){len=recvfrom(cfd,msgBuf,516,0,(struct sockaddr*)&sin,&addrlen);if(msgBuf[1]==3){ write(fd,msgBuf+4,len-4);msgBuf[1]=4;sendto(cfd, msgBuf, 4, 0, (struct sockaddr*)&sin, sizeof(sin));if(len-4<512){break;}}else if(msgBuf[1]==5){if(msgBuf[3]==1){remove(fileName);}printf("%s\n",msgBuf+4);break;}}
}
void updoading(int cfd,struct sockaddr_in sin,socklen_t addrlen)
{char msgBuf[516] = ""; char fileName[21] = ""; printf("请输入您要上传的文件名:");scanf("%s", fileName); int fd=open(fileName,O_RDONLY);if(fd==-1){perror("open error");return;}short *p1 =(short *)msgBuf; *p1 = htons(2); char *p2 = msgBuf + 2; strcpy(p2, fileName);char *p4 = p2 + strlen(p2) + 1; strcpy(p4, "octet");int msgLen = 2 + strlen(p2) + strlen(p4) +2; sendto(cfd, msgBuf, msgLen, 0, (struct sockaddr*)&sin, sizeof(sin));int len;while(1){recvfrom(cfd,msgBuf,4,0,(struct sockaddr*)&sin,&addrlen);if(msgBuf[1]==4){ len=read(fd,msgBuf+4,512);printf("len=%d\n",len);msgBuf[1]=3;sendto(cfd, msgBuf,len+4, 0, (struct sockaddr*)&sin, sizeof(sin));if(len<512){break;}}else if(msgBuf[1]==5){printf("%s\n",msgBuf+4);break;}}}int main(int argc, const char *argv[])
{int cfd = socket(AF_INET, SOCK_DGRAM, 0);if(-1 == cfd){perror("socket error");return -1;}printf("socket success cfd = %d\n", cfd); struct sockaddr_in cin;cin.sin_family = AF_INET; cin.sin_port = htons(CLI_PORT); cin.sin_addr.s_addr = inet_addr(CLI_IP); if(bind(cfd, (struct sockaddr*)&cin, sizeof(cin)) == -1){perror("bind error");return -1;}printf("bind success\n");struct sockaddr_in sin; sin.sin_family = AF_INET;sin.sin_port = htons(SER_PORT); sin.sin_addr.s_addr = inet_addr(SER_IP); socklen_t addrlen = sizeof(sin); int num;while(1){printf("1.下载\n");printf("2.上传\n");printf("3.退出\n");scanf("%d",&num);switch(num){case 1:dowload(cfd,sin,addrlen);break;case 2:updoading(cfd,sin,addrlen);break;case 3:break;}if(num==3)break;}close(cfd);return 0;
}
三、牛客网刷题
