如何在C语言中实现高效的网络文件读写操作?
- 行业动态
- 2025-01-25
- 4755
网络文件读写涉及多种方法,如 Python 中 read、readline、readlines 用于读取文件内容,write、writelines 用于写入文件;C 语言中 fopen、fread、fwrite 等函数进行文件操作。
在C语言中,网络文件读写通常涉及到套接字编程和文件I/O操作的结合,以下是一个简单的示例代码,展示了如何在C语言中实现客户端从服务器读取文件并保存到本地,以及服务器端如何发送文件给客户端。
服务器端代码(server.c)
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <arpa/inet.h> #include <sys/socket.h> #include <sys/types.h> #define PORT 8080 #define BUFFER_SIZE 1024 void sendFile(FILE *fp, int sockfd) { int n; char sendline[BUFFER_SIZE] = {0}; while (fgets(sendline, BUFFER_SIZE, fp) != NULL) { if (send(sockfd, sendline, strlen(sendline), 0) == -1) { perror("Error in sending file."); exit(1); } memset(sendline, 0, BUFFER_SIZE); } } int main() { int server_fd, new_socket; struct sockaddr_in address; int opt = 1; int addrlen = sizeof(address); char buffer[BUFFER_SIZE] = {0}; char *filename = "send.txt"; // Creating socket file descriptor if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { perror("socket failed"); exit(EXIT_FAILURE); } // Forcefully attaching socket to the port 8080 if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { perror("setsockopt"); exit(EXIT_FAILURE); } address.sin_family = AF_INET; address.sin_addr.s_addr = INADDR_ANY; address.sin_port = htons(PORT); // Bind the socket to the address and port number specified in its argument if (bind(server_fd, (struct sockaddr *)&address, sizeof(address))<0) { perror("bind failed"); exit(EXIT_FAILURE); } // Listen for client connections with a maximum of 3 pending connections in the queue if (listen(server_fd, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } // Accept an incoming connection if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen))<0) { perror("accept"); exit(EXIT_FAILURE); } // Open the file to be sent FILE *fp = fopen(filename, "r"); if (fp == NULL) { perror("File not found"); exit(EXIT_FAILURE); } // Send the file to the client sendFile(fp, new_socket); printf("File data sent successfully. "); // Close the file and the socket fclose(fp); close(new_socket); close(server_fd); return 0; }
客户端代码(client.c)
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <netinet/in.h> #define PORT 8080 #define BUFFER_SIZE 1024 int main() { struct sockaddr_in serv_addr; int sock = 0; char buffer[BUFFER_SIZE] = {0}; char *filename = "recv.txt"; FILE *fp; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { printf(" Socket creation error "); return -1; } serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(PORT); // Convert IPv4 and IPv6 addresses from text to binary form if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0) { printf(" Invalid address/ Address not supported "); return -1; } if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { printf(" Connection Failed "); return -1; } // Open the file to write the received data fp = fopen(filename, "w"); if (fp == NULL) { perror("File not found"); exit(EXIT_FAILURE); } // Read the data sent by the server and write it to the file while ((read(sock, buffer, BUFFER_SIZE)) > 0) { fputs(buffer, fp); memset(buffer, 0, BUFFER_SIZE); } printf("File received and saved as %s ", filename); // Close the file and the socket fclose(fp); close(sock); return 0; }
FAQs:
Q1: 为什么服务器需要绑定端口?
A1: 服务器需要绑定端口以便监听来自客户端的连接请求,端口是计算机网络通信中的一个逻辑通道,用于标识特定的网络服务,通过绑定端口,服务器可以在指定的端口上接收客户端的连接请求,从而实现数据的传输。
Q2: 为什么客户端需要知道服务器的IP地址和端口号?
A2: 客户端需要知道服务器的IP地址和端口号才能建立与服务器的连接,IP地址用于在网络上唯一标识服务器的位置,而端口号则用于指定服务器上运行的特定服务或应用程序,只有知道了这两个信息,客户端才能准确地找到并连接到服务器,进而进行数据传输或其他网络通信。
小编有话说:
网络文件读写是网络编程中的一个常见应用场景,它涉及到文件I/O操作和网络通信的结合,通过上述示例代码,我们可以看到如何使用C语言实现简单的文件传输功能,在实际应用中,还需要考虑更多的细节和异常处理,如网络延迟、数据丢失、安全性等问题,在进行网络文件读写时,建议充分了解相关技术和最佳实践,以确保程序的稳定性和可靠性。
本站发布或转载的文章及图片均来自网络,其原创性以及文中表达的观点和判断不代表本站,有问题联系侵删!
本文链接:http://www.xixizhuji.com/fuzhu/399037.html