1、基于UDP的TFTP客户端文件传输
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/ip.h>
#include<string.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#define ERR_MSG(msg) do{\
fprintf(stderr,"%d",__LINE__);\
perror("msg");\
}while(0)
#define SER_IP "192.168.2.131"
#define SER_PORT 69
#define CLI_IP "192.168.0.111"
#define CLI_PORT 7777
void Download();
void Upload();
int main(int argc, const char *argv[])
{
int option =-1;
while(1)
{
printf("************************\n");
printf("********1、下载*********\n");
printf("********2、上传*********\n");
printf("********3、退出*********\n");
printf("************************\n");
printf("************************\n");
scanf("%d",&option);
while(getchar() != 10);
if(1 == option)
Download();
else if(2 == option)
Upload();
else if(3 == option)
break;
}
//关闭
return 0;
}
void Download()
{
//创建报式套接字
int cfd = socket(AF_INET,SOCK_DGRAM,0);
if(cfd < 0)
{
ERR_MSG("socket");
return;
}
char buf[516]={0};
//创建填充服务器地址信息结构体
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);
//设置读请求
*(short*)buf = htons(1);
//printf("%hd\t__%d__\n",ntohs(*(short*)buf),__LINE__);
//将文件名写入到buf中
printf("请输入要下载的文件名>>>>>>\n");
scanf("%s",buf+2);
while(getchar() != 10);
char pathname[128]="";
strcpy(pathname,buf+2);
strcpy(buf+2+strlen(buf+2)+1,"octet");
//printf("%hd %s %s\n",ntohs(*(short*)buf),buf+2,buf+2+strlen(buf+2)+1);
c