C:使用UDP套接字写入文件时出错

时间:2015-08-27 16:02:17

标签: c file sockets udp fwrite

我必须将UDP 1024字节的文件(在本例中为txt文件)发送到服务器,并将此字节写入输出文件。我收到此错误:

write(): bad address

我需要通过数据结构传递字节,因此创建了一个名为“pkt”的结构,其中void *buf可以写入字节。 这是我的示例程序。

客户方:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define SERV_PORT           5197
#define MAX_BLOCK_SIZE      1024

struct pkt {
    void *buf;
    int num;
};

int main(int argc, char *argv[]) {

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <IP SERVER> <file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int sockfd;
    struct sockaddr_in addr;
    int fd;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        perror("socket()");
        exit(EXIT_FAILURE);
    }

    memset((void*)&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(SERV_PORT);

    if (inet_pton(AF_INET, argv[1], &addr.sin_addr) <= 0)
    {
        perror("inet_pton()");
        exit(EXIT_FAILURE);
    }

    fd = open(argv[2], O_RDONLY);
    if(fd == -1)
    {
        perror("open()");
        exit(EXIT_FAILURE);
    }
    //READ FILE
    ssize_t n;
    void *buf;

    buf = alloca(MAX_BLOCK_SIZE);
    n = read(fd, buf, MAX_BLOCK_SIZE);
    if(n != MAX_BLOCK_SIZE)
    {
        perror("read()");
        exit(EXIT_FAILURE);
    }

    struct pkt packet;
    packet.buf = buf;
    packet.num = 0;

    if(sendto(sockfd, &packet, sizeof(struct pkt), 0, (struct sockaddr*)&addr, sizeof(addr)) < 0)
    {
        perror("sendto()");
        exit(EXIT_FAILURE);
    }

    close(fd);

    return EXIT_SUCCESS;

}

服务器端:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define SERV_PORT           5197
#define MAX_BLOCK_SIZE      1024

struct pkt {
    void *buf;
    int num;
};

int main() {
    int sockfd;
    struct sockaddr_in addr;
    socklen_t len;
    int output;

    output = open("test.txt", O_WRONLY|O_CREAT, 0664);
    if(output == -1)
    {
        perror("open()");
        exit(EXIT_FAILURE);
    }

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0)
    {
        perror("socket()");
        exit(EXIT_FAILURE);
    }

    memset((void*)&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    addr.sin_port = htons(SERV_PORT);

    if(bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
    {
        perror("bind()");
        exit(EXIT_FAILURE);
    }

    while (1) {
        len = sizeof(addr);
        ssize_t n;

        struct pkt packet;
        n = recvfrom(sockfd, &packet, sizeof(struct pkt), 0, (struct sockaddr*)&addr, &len);
        if( n < 0)
        {
            perror("recvfrom()");
            exit(EXIT_FAILURE);
        }

        printf("Packet number %d\n", packet.num);

        void *buf = (packet.buf);

        if (write(output, buf, MAX_BLOCK_SIZE) == -1) {
            perror("write()");
            exit(EXIT_FAILURE);

        }
    }

    return 0;
}

0 个答案:

没有答案