网络 - tftp上传 - 20230610

UDP, tftp上传练习

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/wait.h>
#include <pthread.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>

#define ERROR_MSG(msg) do{\
	fprintf(stderr, "line:%d: %s %s", __LINE__, __FILE__, __func__);\
	perror(msg);\
}while(0)

#define SUCCESS_MSG(msg) do {\
	printf("%s, line:%d \n", msg, __LINE__);\
}while(0)

#define IP "192.168.50.130"
#define PORT 69

int main(int argc, const char *argv[]) {

	if (argc != 2) {
		printf("参数错误\n");
		return -1;
	}
	
	char pathname[128];
	sprintf(pathname, "./%s", filename);
	int fd = open(pathname, O_RDONLY);
	if (fd < 0) {
		ERROR_MSG("打开文件失败");
		return -1;
	}

	int cfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (cfd < 0) {
		ERROR_MSG("socket:");
		return -1;
	}
	SUCCESS_MSG("socket success:");

	//配置读写请求
	char buf[516] = "";
	char filename[20];
	strcpy(filename, argv[1]);
	int size = sprintf(buf, "%c%c%s%c%s%c", 0, 2, filename, 0, "octet", 0);

	struct sockaddr_in cin;
	cin.sin_family = AF_INET;
	cin.sin_port = htons(PORT);
	cin.sin_addr.s_addr = inet_addr(IP);

	if (sendto(cfd, buf, size, 0, (struct sockaddr *)&cin, sizeof(cin))<0) {
		ERROR_MSG(" sendto error:");
		return -1;
	}
	SUCCESS_MSG("发送写请求成功");

	//sin记录服务器端信息
	struct sockaddr_in sin;
	int slen = sizeof(sin);
	if (recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, &slen) < 0) {
		ERROR_MSG(" recvfrom error:");
		return -1;
	}
	//写请求的应答
	SUCCESS_MSG("收到写请求应答");
	short *op_ptr = (short *)buf;
	short *block_ptr = op_ptr + 1;
	short op_val = ntohs(*op_ptr);
	short block_val = ntohs(*block_ptr);
	printf("op_val = %d \n", op_val);
	printf("block_val = %d \n", block_val);

	char con_buf[512] = "";
	unsigned short curBlockIndex = 0;
	while(1) {
		bzero(con_buf, sizeof(con_buf));
		bzero(buf, sizeof(buf));
		ssize_t readRes = read(fd, con_buf, sizeof(con_buf));
		printf("read count = %ld \n", readRes);
		if (readRes > 0) {
			printf("发送的块编号 = %d\n", curBlockIndex);
			//数据包的操作码
			short *ptr = (short *)buf;
			*ptr = htons(3);
			//数据包的块编号
			short *ptr2 = ptr + 1;
			*ptr2 = htons(curBlockIndex);
			//数据包的数据
			char *ptr3 = (char *)(ptr2+1);
			strcpy(ptr3, con_buf);

			if (sendto(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
				ERROR_MSG("sendto ");
				return -1;
			}
			printf("发送成功 \n");

			bzero(buf, sizeof(buf));
			if (recvfrom(cfd, buf, sizeof(buf), 0, (struct sockaddr *)&sin, &slen) < 0) {
				ERROR_MSG(" recvfrom ");
				return -1;
			}
			curBlockIndex++;
			printf("接收ack成功:");
			op_ptr = (short *)buf;
			block_ptr = op_ptr + 1;
			op_val = ntohs(*op_ptr);
			block_val = ntohs(*block_ptr);
			printf("接收的ack的操作码:%d \n", op_val);
			printf("接收的块编码:%d \n", block_val);
		} else {
			//读取文件结束
			close(fd);
			break;
		}			
	}
	
	return 0;
}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值