1、作业要求
在Linux系统中使用C语言代码,实现简单的TFTP服务器的下载与上传功能。
2、实现过程
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#define ERR_MSG(msg) do{\
fprintf(stderr, "line:%d\n", __LINE__);\
perror(msg);\
}while(0)
#define IP "192.168.8.231" //windows IP地址
#define PORT 69 //tftp服务器的端口号
int do_download(int sfd, struct sockaddr_in sin);
int do_upload(int sfd, struct sockaddr_in sin);
int main(int argc, const char *argv[])
{
//创建报式套接字
int sfd = socket(AF_INET, SOCK_DGRAM, 0);
if(sfd < 0)
{
ERR_MSG("socket");
return -1;
}
//填充服务器自身的地址信息结构体,AF_INET: man 7 IP
//供于下方的sendto使用,因为sendto必须要指明发给谁
struct sockaddr_in sin;
sin.sin_family = AF_INE