代码:
#include <stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#define err_msg(pg)do{\
fprintf(stderr,"line:__%d__",__LINE__);\
perror(pg);\
}while(0);
//将客户端的端口与ip地址宏定义
#define cil_port 8856 //客户端的端口
#define cil_ip "192.168.8.168" //客户端ip地址
//将目标服务器的端口和ip地址宏定义
#define ser_port 69 //服务器端口
#define ser_ip "192.168.8.132" //服务器ip地址
int upload(int fd,struct sockaddr_in sin);
//上传函数
int upload(int fd,struct sockaddr_in sin)
{
/**********************************开始定义读写请求服务器下载包的协议数组*******************************************/
//定义一个字符数组 用于发送数据包
char buf[516]="";
//根据下载包协议定义数组
short* p1=(short*) buf; //定义一个short类型的指针指向数组的首地址 并将他类型转换为short
*p1=htons(2); //将协议中的操作码的读权限所对应的1填入指针中******操作码********
char* p2=(char*)(p1+1); //定义一个char 类型的指针指向数组中文件名所对应的位置****文件名****
strcpy(p2,"./1.c"); //将下载服务器中的文件名5.png填到里面 因为strcpy自动补\0,所以后面
//后面不用指针指向请求服务器下载下载包协议中0的位置******文件名*******
char* p3=p2+strlen(p2)+1; //strlen不计算字符串的\0位 但是请求下载下载包协议中有0位 所以加1*****模式**
strcpy(p3,"octet"); //将请求下载包协议中的模式放入数组的p3位置
int tlern=2+strlen(p2)+1+strlen(p3)+1;
//因为strcpy将字符串复制后自动补\0
//所以请求下载协议包最后一位不补\0;
/********************************读写请求服务器下载下载包的协议数组至此定义完毕**************************************/
/*****************************开始定义服务器返回客户端的数据包协议数组******************************************/
char str[4]="";
/****************************返回客户端的数据包协议数组定义完毕**************************************************/
/*****************************开始定义客户端发送给服务器的数据包协议数组******************************************/
char arr[516]="";
/****************