温馨提示×

如何在Linux中使用socket进行文件传输

小樊
89
2024-08-30 20:30:32
栏目: 智能运维

在 Linux 中,你可以使用 socket 编程进行文件传输

服务器端代码(server.c):

#include <arpa/inet.h>
#include <netinet/in.h>
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include <sys/socket.h>
#include <unistd.h>

#define PORT 8080
#define BUFFER_SIZE 1024

int main() {
    int server_fd, client_fd;
    struct sockaddr_in server_addr, client_addr;
    socklen_t client_addr_size;
    char buffer[BUFFER_SIZE];
    FILE *file;

    // 创建 socket
    server_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (server_fd == -1) {
        perror("socket");
        exit(1);
    }

    // 配置服务器地址
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
    server_addr.sin_port = htons(PORT);

    // 绑定 socket
    if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("bind");
        exit(1);
    }

    // 监听连接
    if (listen(server_fd, 1) == -1) {
        perror("listen");
        exit(1);
    }

    // 接受客户端连接
    client_addr_size = sizeof(client_addr);
    client_fd = accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_size);
    if (client_fd == -1) {
        perror("accept");
        exit(1);
    }

    // 打开文件
    file = fopen("file.txt", "rb");
    if (file == NULL) {
        perror("fopen");
        exit(1);
    }

    // 读取并发送文件内容
    while (!feof(file)) {
        size_t bytes_read = fread(buffer, 1, BUFFER_SIZE, file);
        send(client_fd, buffer, bytes_read, 0);
    }

    // 关闭文件和 socket
    fclose(file);
    close(client_fd);
    close(server_fd);

    return 0;
}

客户端代码(client.c):

#include <arpa/inet.h>
#include <netinet/in.h>
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#include <sys/socket.h>
#include <unistd.h>

#define SERVER_IP "127.0.0.1"
#define PORT 8080
#define BUFFER_SIZE 1024

int main() {
    int client_fd;
    struct sockaddr_in server_addr;
    char buffer[BUFFER_SIZE];
    FILE *file;

    // 创建 socket
    client_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (client_fd == -1) {
        perror("socket");
        exit(1);
    }

    // 配置服务器地址
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    server_addr.sin_port = htons(PORT);

    // 连接服务器
    if (connect(client_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) {
        perror("connect");
        exit(1);
    }

    // 打开文件
    file = fopen("received_file.txt", "wb");
    if (file == NULL) {
        perror("fopen");
        exit(1);
    }

    // 接收并写入文件内容
    while (1) {
        ssize_t bytes_received = recv(client_fd, buffer, BUFFER_SIZE, 0);
        if (bytes_received <= 0) {
            break;
        }
        fwrite(buffer, 1, bytes_received, file);
    }

    // 关闭文件和 socket
    fclose(file);
    close(client_fd);

    return 0;
}

要编译和运行这些示例代码,请按照以下步骤操作:

  1. 将上述代码分别保存为 server.cclient.c
  2. 使用 gcc 编译器分别编译这两个文件:gcc server.c -o servergcc client.c -o client
  3. 首先运行服务器端程序:./server
  4. 然后在另一个终端中运行客户端程序:./client
  5. 客户端程序将从服务器接收文件并将其保存为 received_file.txt

注意:这个示例代码仅用于演示目的,实际应用中可能需要考虑更多的错误处理和优化。

0