我在C中写了一个客户端/服务器程序,客户端将文件发送到服务器。 它可以工作,但是一些垃圾字节被添加到字节1400的接收文件中。 我尝试使用文本文件,然后在发送之前将内存设置为0。 为什么那个垃圾字节?
这里是代码:
macro.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
client.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;
}
server.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字节(发送更多数据包),则添加一些垃圾字节。 我无法找到原因!
答案 0 :(得分:1)
字符串末尾的垃圾字节是一个丢失的字符串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");
答案 1 :(得分:0)
现在一切正常。我在 server.c 中的while循环中弄乱了ifs。
server.c 中的 run()功能:
void run(int sockfd, struct sockaddr *cliaddr, socklen_t clilen)
{
int n = 0, count = 0, w = 0;
long received = 0, writed = 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");
// abort_on_error(setvbuf(file, buf, _IOFBF, MAXLINE*MUL), "setvbuf() 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);
int num_packet = filesize / sizeof(packet.data);
int packet_header_size = sizeof(packet) - sizeof(packet.data);
int last_size = filesize-num_packet*(n-packet_header_size);
while ((n = recvfrom(sockfd, &packet, sizeof(packet), 0, cliaddr, &len))) {
abort_on_error(n == -1, "recvfrom() data error");
if (!(strcmp((const char *) packet.data, END_FLAG))) {
printf("END_FLAG received. Stopping receiving data.\n");
break;
}
received += n;
count += 1;
// memset(buf, (int) packet.data, sizeof(packet.data));
printf("Bytes received: %d\n", n);
// printf("num_packet: %d\n", num_packet);
if (!num_packet) {
printf("byte to write: %ld\n", filesize);
w = fwrite(packet.data, sizeof(char), filesize, file);
printf("byte writed: %d\n", w);
writed += w;
}
else {
if (count <= num_packet) {
printf("byte to write: %d\n", n-packet_header_size);
w = fwrite(packet.data, sizeof(char), n-packet_header_size, file);
printf("byte writed: %d\n", w);
writed += w;
}
else {
printf("byte to write: %d\n", last_size);
w = fwrite(packet.data, sizeof(char), last_size, file);
printf("byte writed: %d\n", w);
writed += w;
}
}
}
printf("Tot Bytes writed: %ld\n", writed);
printf("Tot Bytes received: %ld\n", received);
fclose(file);
}