我正在开发一个关于客户端和服务器的c++程序(最像一个练习课),使用HTTP协议,用户给客户端一个文件文件名和大小(字节),然后客户端创建n个线程,每个线程向服务器请求特定数量的字节,服务器参加订单,客户端接收数据并将它们放在一起。
我的程序正常工作对于小文件(100 kb - 200 kb),但是当我试着发送大文件(例如Mb)接收来自服务器的所有字节但最终文件损坏,每个线程有自己的init和结束字节数和创建一个名为"file_n.txt"所以的文件字节的顺序没有问题的时候一起把所有的字节,最终损坏的文件具有相同的比原来的字节数(所有字节都收到了,我也检查服务器日志关于字节间隔的线程请求),但它的hexdump是不同的(显然)。
你认为fwrite函数与这个问题有关吗?如果是,请给我指出正确的方向,我正在努力解决这个问题,这是我的客户端。cpp代码
#include <pthread.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <netdb.h>
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
const int MAX_HEADER_SIZE = 1000;
int threadsEnd = 0;
struct bytes
{
int initByte;
int endByte;
int bufferSize;
int id;
char * port;
char * ip;
char * image;
};
void error(const char *msg)
{
perror(msg);
exit(0);
}
void * request_bytes (void * parameters)
{
struct bytes * p = (struct bytes *) parameters;
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
int totalBuffer = MAX_HEADER_SIZE + p->bufferSize + 1;
int totalBodyContent = p->bufferSize + 1;
char buffer[totalBuffer];
char bodyContent[totalBodyContent];
portno = atoi(p->port);
server = gethostbyname(p->ip);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
ostringstream init,end;
init << p->initByte;
end << p->endByte;
string HttpRequestString = string("POST / HTTP/1.1rn")
+ string("Host: ") + p->ip + string("n")
+ string("Connection: Closen")
+ string("Content-Length: 4n")
+ string("Content-Type: txtnn")
+ string("nombre=") + p->image + string("&inicio=") + init.str() + string("&fin=") + end.str() + string("n");
const char * HttpRequest = HttpRequestString.c_str();
n = write(sockfd,(void *)HttpRequest, strlen(HttpRequest));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,(MAX_HEADER_SIZE + p->bufferSize));
int headerEndDetermined = 0, bodyEnd = 0;
int x = 1;
int bodyInit = 1;
int total_bytes = 0;
n = read(sockfd,buffer,((MAX_HEADER_SIZE + p->bufferSize) - 1));
if (n < 0)
error("ERROR reading from socket");
for(; x < strlen(buffer); x++)
if(buffer[x - 1] == 'n')
if(buffer[x] == 'n')
{
headerEndDetermined = 1;
bodyInit = x + 1;
break;
}
for(x = 0; x < p->bufferSize ; x++)
{
bodyContent[x] = buffer[bodyInit];
bodyInit++;
}
//Escritura de archivo
char filename[32];
snprintf(filename, sizeof(char) * 32, "file%i", p->id);
FILE * pFile;
pFile = fopen (filename,"wb");
if(pFile != NULL)
{
fwrite (bodyContent,1,sizeof(bodyContent) - 1,pFile);
fclose (pFile);
}
close(sockfd);
threadsEnd++;
return NULL;
}
int main (int argc, char *argv[])
{
if (argc < 5) {
fprintf(stderr,"uso %s hostname puerto image_name bytesn", argv[0]);
exit(0);
}
int globalByte = atoi(argv[4]);
int threadRequest = 10;
int requestBytes = (globalByte / threadRequest);
int globalInitialByte = 1;
int globalEndByte = requestBytes;
int x = 0, i = 1;
int totalBytesRequested = 0;
pthread_t request[threadRequest];
for(; x < threadRequest; x++){
struct bytes request_args;
request_args.initByte = globalInitialByte;
request_args.endByte = globalEndByte;
request_args.bufferSize = requestBytes;
request_args.id = x + 1;
globalInitialByte = globalEndByte + 1;
globalEndByte = globalEndByte + requestBytes;
if(x == (threadRequest - 1))
{
if((totalBytesRequested + requestBytes) < globalByte)
{
request_args.endByte = globalByte;
request_args.bufferSize = requestBytes + (globalByte - (totalBytesRequested + requestBytes));
}
}
request_args.ip = argv[1];
request_args.port = argv[2];
request_args.image = argv[3];
pthread_create (&request[x], NULL, &request_bytes, &request_args);
pthread_join (request[x], NULL);
totalBytesRequested += requestBytes;
}
/*do
{
cout<<"Threads completos: "<<threadsEnd<<endl;
}while(threadsEnd < threadRequest);*/
string createFileString = string("cat ");
for(; i <= threadRequest; i++)
{
ostringstream filen;
filen << i;
createFileString = createFileString + string("file") + filen.str() + string(" ");
}
createFileString = createFileString + string("> new_") + argv[3];
system(createFileString.c_str());
return 0;
}
p.
你有很多bug。
-
HTTP协议规定行必须以"rn"结尾,而不是"n"。
-
您指定的内容长度为4个字节,但您的内容超过了这个长度
-
当你的代码已经知道东西的大小时,不要使用
sizeof
或strlen
。这会给你带来麻烦的。 -
您只呼叫
read
一次。你需要一直调用read
,直到你收到所有的数据。 -
您指定HTTP 1.1遵从性,但您的代码实际上并不符合HTTP 1.1规范。例如,如果您接收到的数据采用分块编码,那么您的代码将严重中断。HTTP 1.1客户端要求支持分块编码。所有HTTP/1.1应用程序必须能够接收和解码
chunked
传输编码[。——RFC2616 3.6.1.
我不认为你可以在运行时声明字符串的大小,你需要改变
char buffer[totalBuffer];
char bodyContent[totalBodyContent];
char buffer = new char[totalBuffer];
char bodyContent = new char[totalBodyContent];
并删除末尾的缓冲区
delete [] buffer;
delete [] bodyContent;
或者,您可以使用malloc()
和free()
来分配和释放缓冲区