gpt4 book ai didi

c - 为什么我在 C 中使用 UDP 套接字接收文件时会出现一些垃圾字节?

转载 作者:行者123 更新时间:2023-11-30 19:06:46 25 4
gpt4 key购买 nike

我用 C 语言编写了一个客户端/服务器程序,其中客户端将文件发送到服务器。它可以工作,但是一些垃圾字节被添加到接收到的文件中字节的 1400 位置。我尝试使用文本文件,并在发送之前将内存设置为 0。为什么是垃圾字节?

代码如下:

宏.h

#ifndef MACRO_H
#define MACRO_H

#include <stdio.h>
#include <errno.h>
#include <stdint.h>


#define SERV_PORT 49312
#define MAXLINE 1400

#define END_FLAG "================END"

#define abort_on_error(cond, msg) do { \
if (cond) { \
fprintf(stderr, "%s (errno=%d [%s])\n", msg, errno, strerror(errno)); \
exit(EXIT_FAILURE); \
} \
} while (0)

struct pkt {
// struct sockaddr_in addr;
// int sockfd;
uint16_t seq_num;
uint32_t status;
uint8_t data[MAXLINE];
};

typedef struct pkt PACK;

#endif // MACRO_H

客户端.c

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

#include "macro.h"

int main(int argc, char **argv)
{
int sockfd, n, count = 0;
struct sockaddr_in servaddr;
// char buf[MAXLINE];
char *target, *path;
FILE *file;
PACK packet;
long filesize;

bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(SERV_PORT);
inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

sockfd = socket(AF_INET, SOCK_DGRAM, 0);

path = argv[2];
target = argv[3];

packet.seq_num = 0;
packet.status = 0;
memset(packet.data, 0, sizeof(packet.data));
memcpy(packet.data, target, strlen(target));

abort_on_error(sendto(sockfd, (struct pkt *) &packet, sizeof(packet), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1,
"sendto() filename error");

memset(packet.data, 0, sizeof(packet.data));
abort_on_error(recvfrom(sockfd, &packet, sizeof(packet), 0, NULL, NULL) == -1, "recvfrom() ok error");
if (!strncmp((const char *) packet.data, "ok", 2)) {
printf("Filename sent.\n");
}

file = fopen(path, "r");
abort_on_error(file == NULL, "fopen() error");

fseek(file, 0L, SEEK_END);
filesize = ftell(file);
printf("filesize: %ld\n", filesize);
fseek(file, 0L, SEEK_SET);
memset(packet.data, 0, sizeof(packet.data));
sprintf((char *) packet.data, "%ld", filesize);

abort_on_error(sendto(sockfd, (struct pkt *) &packet, sizeof(packet), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1,
"sendto() data error");

memset(packet.data, 0, sizeof(packet.data));
while ((n = fread(packet.data, sizeof(char), MAXLINE, file))) {
// packet.data[n] = '\0';
// printf("n: %d\n", n);
abort_on_error(n == -1, "fread() error");
abort_on_error(sendto(sockfd, (struct pkt *) &packet, sizeof(packet), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1,
"sendto() data error");
count = count + n;
memset(packet.data, 0, sizeof(packet.data));
}
printf("Tot Bytes sent: %d\n", count);

memset(packet.data, 0, sizeof(packet.data));
memcpy(packet.data, END_FLAG, strlen(END_FLAG));
abort_on_error(sendto(sockfd, (struct pkt *) &packet, sizeof(packet), 0, (struct sockaddr *) &servaddr, sizeof(servaddr)) == -1,
"sendto() end_flag error");

fclose(file);
return 0;
}

服务器.c

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

#include "macro.h"

void run(int sockfd, struct sockaddr *cliaddr, socklen_t clilen)
{
int n = 0, count = 0, status = 0;
socklen_t len;
// char buf[MAXLINE];
FILE *file;
PACK packet;

len = clilen;
n = recvfrom(sockfd, &packet, sizeof(packet), 0, cliaddr, &len);
abort_on_error(n == -1, "recvfrom() filename error");
// buf[n] = 0;
printf("Received from client: [%s]\n", packet.data);

file = fopen((const char *) packet.data, "w+");
abort_on_error(file == NULL, "fopen() error");

packet.seq_num = 0;
packet.status = 0;
memset(packet.data, 0, sizeof(packet.data));
memcpy(packet.data, "ok", strlen("ok"));
abort_on_error(sendto(sockfd, (struct pkt *) &packet, sizeof(packet), 0, cliaddr, len) == -1,
"sendto() ok error");

memset(packet.data, 0, sizeof(packet.data));
abort_on_error(recvfrom(sockfd, &packet, sizeof(packet), 0, cliaddr, &len) == -1,
"recvfrom() error");
long filesize = strtol((const char *) packet.data, NULL, 0);
printf("filesize: %ld\n", filesize);

while ((n = recvfrom(sockfd, &packet, sizeof(packet), 0, cliaddr, &len))) {
abort_on_error(n == -1, "recvfrom() data error");
// packet.data[n] = '\0';
// printf("%s\n", packet.data);
if (!(strcmp((const char *) packet.data, END_FLAG))) {
break;
}

int num_packet = filesize / n;
// printf("num_packet: %d\n", num_packet);

count += n;
if(!num_packet){
fwrite(packet.data, sizeof(char), n-(n-filesize), file);
printf("byte to write: %ld\n", n-(n-filesize));
}
else {
if(count <= filesize) {
fwrite(packet.data, sizeof(char), n-8, file);
printf("byte to write: %d\n", n);
}
else if ((count > filesize) && (filesize-num_packet*n-8) && !status) {
fwrite(packet.data, sizeof(char), filesize-num_packet*(n-8), file);
printf("byte to write: %ld\n", filesize-num_packet*(n-8));
status = 1;
}
}
}
printf("Tot Bytes received: %d\n", count);
fclose(file);
}

int main(int argc, char **argv)
{
int sockfd;
struct sockaddr_in servaddr, cliaddr;

sockfd = socket(AF_INET, SOCK_DGRAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERV_PORT);

bind(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr));

run(sockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));

return 0;
}

对于小于 1400 字节的文件,它可以完美运行!如果文件大于 1400 字节(需要发送更多数据包),则会添加一些垃圾字节。我找不到原因!

最佳答案

字符串末尾的垃圾字节是丢失字符串 nul 终止问题的致命弱点。

这里:

memcpy(packet.data, target, strlen(target));

或这里:

memcpy(packet.data, "ok", strlen("ok"));

您正在复制不带终止符的字符串。

现在在接收端你无法知道字符串在哪里结束,fprintf 也不知道。

因此,复制字符串时只需使用 strcpy 而不是 memcpy 即可。它会工作并添加 nul 终止符

strcpy(packet.data, target);

strcpy(packet.data, "ok");

关于c - 为什么我在 C 中使用 UDP 套接字接收文件时会出现一些垃圾字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47691432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com