使用C语言,基于socket编程,不用现成组件实现TFTP服务器程序
时间: 2024-09-30 07:03:54 浏览: 38
在C语言中,利用socket编程实现一个简单的Trivial File Transfer Protocol (TFTP) 服务器,TFTP是一种轻量级的文件传输协议,主要用于小型设备间的文件交换。下面是一个基础的TFTP服务器实现,只包含基本的功能,如接收请求、发送文件、以及一些错误处理。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#define PORT 69
#define MAX_PACKET_SIZE 512
typedef enum { TFTP_RRQ, TFTP_WRQ } TftpCommand;
struct TftpPacket {
uint8_t command;
char filename[100];
char mode[4]; // 模式,一般为"octet"
};
void send_error(socket_t s, int error_code) {
char error_msg[] = "ERROR code %d: %s";
char packet[MAX_PACKET_SIZE];
snprintf(packet, sizeof(packet), error_msg, error_code, strerror(error_code));
write(s, packet, strlen(packet));
}
void serve_request(socket_t s) {
struct TftpPacket packet;
memset(&packet, 0, sizeof(packet));
read(s, &packet, sizeof(packet));
printf("Received request: %s\n", packet.filename);
if (packet.command == TFTP_RRQ) {
// 文件读取
FILE *fp = fopen(packet.filename, "rb");
if (!fp) {
send_error(s, ENOENT);
return;
}
char data[MAX_PACKET_SIZE];
ssize_t bytes_read;
while ((bytes_read = fread(data, 1, MAX_PACKET_SIZE, fp)) > 0) {
write(s, data, bytes_read);
}
fclose(fp);
} else if (packet.command == TFTP_WRQ) {
// 文件写入暂时不做处理,可以简单返回错误
send_error(s, EPERM);
} else {
send_error(s, EPROTO);
}
}
int main() {
int server_socket = socket(AF_INET, SOCK_DGRAM, 0);
if (server_socket == -1) {
perror("Socket creation failed");
return 1;
}
struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
bind(server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
printf("Server started on port %d\n", ntohs(server_addr.sin_port));
while (true) {
socklen_t addr_len = sizeof(server_addr);
struct sockaddr_in client_addr;
char buffer[MAX_PACKET_SIZE];
int bytes_received = recvfrom(server_socket, buffer, MAX_PACKET_SIZE, 0, (struct sockaddr *)&client_addr, &addr_len);
if (bytes_received <= 0) {
perror("recvfrom failed");
continue;
}
serve_request(server_socket);
}
close(server_socket);
return 0;
}
```
这个TFTP服务器仅支持RRQ(Read Request)命令,即从服务器获取文件,WRQ(Write Request)功能未实现。实际部署时,你需要处理更复杂的交互,如文件大小确认、数据包顺序、ACK/NACK响应等。
阅读全文