我写了一个客户端/服务器程序,客户端将文件发送到服务器。
这里是代码:
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 {
uint32_t seq_num;
uint32_t status;
uint8_t data[MAXLINE];
};
typedef struct pkt PACK;
#endif // MACRO_H
我需要struct pkt在未来的项目中发送它。
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_STREAM, 0);
abort_on_error(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0,
"Error connect()");
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))) {
abort_on_error(ferror(file), "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*MUL];
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);
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))) {
break;
}
int num_packet = filesize / n;
int packet_header_size = sizeof(packet) - sizeof(packet.data);
// printf("num_packet: %d\n", num_packet);
count += n;
if(!num_packet){
fwrite(packet.data, sizeof(char), filesize, file);
// printf("byte to write: %ld\n", filesize);
}
else {
if(count <= filesize) {
fwrite(packet.data, sizeof(char), n-packet_header_size, file);
// printf("byte to write: %ld\n", n-packet_header_size);
}
else if ((count > filesize) && (filesize-num_packet*(n-packet_header_size)) &&!status) {
fwrite(packet.data, sizeof(char), filesize-num_packet*(n-packet_header_size), file);
// printf("byte to write: %ld\n", filesize-num_packet*(n-packet_header_size));
status = 1;
}
}
}
printf("Tot Bytes received: %d\n", count);
fclose(file);
}
int main(int argc, char **argv)
{
int sockfd, newsockfd;
struct sockaddr_in servaddr, cliaddr;
sockfd = socket(AF_INET, SOCK_STREAM, 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));
listen(sockfd, 5);
int clilen = sizeof(cliaddr);
newsockfd = accept(sockfd, (struct sockaddr *) &cliaddr, (unsigned int *) &clilen);
abort_on_error(newsockfd == -1, "Error accept()");
run(newsockfd, (struct sockaddr *) &cliaddr, sizeof(cliaddr));
return 0;
}
在while循环中,ifs用于正确处理文件的文件大小。
它适用于纯文本文件,但对于其他文件(大文件,例如jpg,zip,iso),它会丢失一些字节而且它不起作用! 我无法理解为什么!!!?
答案 0 :(得分:0)
当您使用非明文(也就是二进制数据)时,您需要更改打开文件的方式。
您目前正在执行此操作:
file = fopen(path, "r");
相反,改为:
file = fopen(path, "rb");
显然,你需要使用"rb"
打开写作。如果您不这样做,您将无法正确读取某些字符(例如,空值)。
答案 1 :(得分:0)
现在一切正常。我在 server.c 中的while循环中弄乱了ifs。
client.c :
int main(int argc, char **argv)
{
int sockfd, n, sent;
long 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_STREAM, 0);
abort_on_error(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0,
"Error connect()");
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))) {
abort_on_error(ferror(file), "fread() error");
abort_on_error((sent = sendto(sockfd, (struct pkt *) &packet, sizeof(packet), 0, (struct sockaddr *) &servaddr, sizeof(servaddr))) == -1,
"sendto() data error");
count += sent;
printf("Bytes sent: %d\n", sent);
memset(packet.data, 0, sizeof(packet.data));
}
printf("Tot Bytes sent: %ld\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上的 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);
}
其他代码( macro.h 和 server.c )是相同的。