1. 多线程中的newfd,能否修改成全局,不行,为什么?
2. 多线程中分支线程的newfd能否不另存,直接用指针间接访问主线程中的newfd,不行,为什么?
因为线程间的newfd的改变会改变整体的newfd的值,使线程间相应的newfd值发生混乱
3.思维导图
4.文件上传和下载
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>
#define IP "192.168.124.11"
/**************************下载*******************************/
int tftp_download(int cfd, struct sockaddr_in sin)
{
char buf[516] = "";
//输入文件名
char name[128] = "";
printf("请输入要下载的文件名:");
scanf("%s",name);
getchar();
//读写请求
char *ptr = buf;
unsigned short *p1 = (unsigned short*)buf;
*p1 = htons(1);
char *p2 = buf + 2;
strcpy(p2,name);
char *p3 = p2 + strlen(p2) + 1;
strcpy(p3,"octet");
unsigned short Ack[2]; //ACK
//申请读写请求
if(sendto(cfd, buf, 2+strlen(p2)+2+strlen(p3), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
//创建并打开文件
int fd = open(name,O_WRONLY|O_CREAT|O_TRUNC,0664);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
//存储临时端口
struct sockaddr_in rcvaddr;
socklen_t addrlen = sizeof(rcvaddr);
while(1)
{
//接收数据包
bzero(buf,sizeof(buf));
ssize_t res = recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr*)&rcvaddr, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
char *q = buf + 2;
unsigned short *k = (unsigned short*)q;
write(fd, buf+4, res-4);
//发送ACK包
Ack[0]=htons(4);
Ack[1]=htons(ntohs(*k));
if(sendto(cfd, Ack, sizeof(Ack), 0, (struct sockaddr*)&rcvaddr, sizeof(rcvaddr)) < 0)
{
ERR_MSG("sendto");
return -1;
}
bzero(Ack,sizeof(Ack));
//判断数据传输是否完毕
if(res-4 < 512)
{
break;
}
}
printf("下载完成\n");
close(fd);
return 0;
}
/*************************上传******************************/
int tftp_Upload(int cfd, struct sockaddr_in sin)
{
char buf[516] = "";
//输入文件名
char name[128] = "";
printf("请输入要上传的文件名:");
scanf("%s",name);
getchar();
//读写请求
char *ptr = buf;
unsigned short *p1 = (unsigned short*)buf;
*p1 = htons(2);
char *p2 = buf + 2;
strcpy(p2,name);
char *p3 = p2 + strlen(p2) + 1;
strcpy(p3,"octet");
unsigned short Ack[2]; //ACK
unsigned short operate,block; //操作码,块编号
ssize_t res = 0;
//申请读写请求
if(sendto(cfd, buf, 2+strlen(p2)+2+strlen(p3), 0, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{
ERR_MSG("sendto");
return -1;
}
//打开文件
int fd = open(name,O_RDONLY);
if(fd < 0)
{
ERR_MSG("open");
return -1;
}
//存储临时端口
struct sockaddr_in rcvaddr;
socklen_t addrlen = sizeof(rcvaddr);
bzero(Ack, sizeof(Ack));
if ((res = recvfrom(cfd, Ack, sizeof(Ack), 0, (struct sockaddr*)&rcvaddr, &addrlen)) < 0)
{
ERR_MSG("recvfrom");
return -1;
}
// 判断服务发来的ACK包是否合法
operate = ntohs(Ack[0]);
block = ntohs(Ack[1]);
if (operate!= 4 || block != 0)
{
return -1;
}
printf("服务器ACK首次应答成功,开始传输数据……\n");
while(1)
{
//发送数据包
bzero(buf,sizeof(buf));
res = read(fd,buf+4,512);
if(res == 0)
{
printf("上传成功\n");
break;
}
else if(res == -1)
{
ERR_MSG("read");
return -1;
}
unsigned short *p_operate = (unsigned short*)ptr;
*p_operate = htons(3);
block++;
unsigned short *p_block = p_operate + 1;
*p_block = htons(block);
if(sendto(cfd, buf, 4+res, 0, (struct sockaddr*)&rcvaddr, sizeof(rcvaddr)) < 0)
{
ERR_MSG("sendto");
return -1;
}
//接收Ack包
bzero(Ack,sizeof(Ack));
res = recvfrom(cfd, Ack, sizeof(Ack), 0, (struct sockaddr*)&rcvaddr, &addrlen);
if(res < 0)
{
ERR_MSG("recvfrom");
return -1;
}
operate = ntohs(Ack[0]);
//判断数据传输是否完毕
if(operate==4)
{
if(block!=ntohs(Ack[1]))
break;
}
else if(operate==5)
break;
}
close(fd);
return 0;
}
int main(int argc, const char *argv[])
{
//创建报式套接字
int cfd = socket(AF_INET, SOCK_DGRAM, 0);
if(cfd < 0)
{
ERR_MSG("socket");
return -1;
}
printf("cfd = %d\n", cfd);
//填充服务器自身的地址信息结构体,真实的地址信息结构体根据地址族指定
//AF_INET : man 7 ip
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons(69);
sin.sin_addr.s_addr = inet_addr(IP);
int t = 0;
while(1)
{
printf("******请选择功能******\n");
printf("*******1.下载*********\n");
printf("*******2.上传*********\n");
printf("*******3.退出*********\n\n");
printf("请输入:");
scanf("%d",&t);
switch(t)
{
case 1: tftp_download(cfd,sin); break;
case 2: tftp_Upload(cfd,sin); break;
case 3: close(cfd); return 0;
default: printf("error\n");
}
}
return 0;
}