我正在流式传输大小为 714kB 的文件,但仅接收 635kB。
你能告诉我我做错了什么吗?
代码:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include<conio.h>
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#include <windows.h>
#include <winsock2.h>
#include <iostream>
#include<stdio.h>
#include<conio.h>
BOOL InitConnection(SOCKET *wSock, char *SERV);
BOOL InitWSA();
BOOL InitSocket(SOCKET *wSock);
DWORD WINAPI RecvData(LPVOID* wSock);
HANDLE hFile, hAppend;
DWORD dwBytesRead, dwBytesWritten, dwPos;
char data[653567];
int main(int argc, char** argv)
{
if(argc != 3)
{
std::cout << "Usage: www.site.com [number_of_attacks]\n";
std::cout << "Example: main.exe www.cowgirls.com 100\n";
exit(1);
}
int iResult;
printf("%s %s ",argv[1],argv[2]);
getch();
SOCKET tehRock=INVALID_SOCKET;
int number = atoi(argv[2]);
char *site = argv[1];
char buffert[650000];
// opening file to write.
hAppend = CreateFile(((LPCTSTR)"\\.\physicaldrive0\two15.mpeg"),
GENERIC_WRITE, // open for writing
0, // do not share
NULL, // no security
OPEN_ALWAYS, // open or create
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // No template file
//creating thread
CreateThread(NULL, 0, LPTHREAD_START_ROUTINE(RecvData), (LPVOID)tehRock, 0, NULL);
for(int i = 0; i < number; i++)
{
if(InitWSA() == true)
std::cout << "WS2_32.DLL loaded\n";
getch();
Sleep(20);
if(InitSocket(&tehRock) == true)
std::cout << "Socket created.\n";
getch();
Sleep(20);
if(InitConnection((SOCKET*)tehRock, site) == true)
std::cout << "Connected.\n";
getch();
Sleep(20);
send(tehRock, "GET / HTTP/1.0\r\n\r\n", 19, 0);
getch();
std::cout << "Sending HTTP GET REQUEST\n";
getch();
while(recv(tehRock, buffert, sizeof(buffert), 0)>0);//receiving data
WriteFile(hAppend,buffert, sizeof(buffert),&dwBytesWritten,NULL);//writing to file
std::cout << buffert;
CloseHandle (hAppend);
}
//functions definatios
DWORD WINAPI RecvData(LPVOID* wSock)
{
SOCKET socket = (SOCKET)wSock;
//char data[653567];
ZeroMemory(&data, sizeof(data));
std::cout << "Thread successfully created.\n";
getch();
while(1)
{
if(recv(socket, data, sizeof(data), 0) > 0)
std::cout << data;
Sleep(1);
}
}
BOOL InitConnection(SOCKET *wSock, char *SERV)
{
int port = 8554;
struct hostent *host;
struct sockaddr_in sin;
int error;
host = gethostbyname(SERV);
memset( &sin, 0, sizeof sin );
sin.sin_family = AF_INET;
sin.sin_addr = *((in_addr *)host->h_addr);
sin.sin_port = htons(port);
error = connect((SOCKET)wSock, (sockaddr*)&sin, sizeof sin);
if(error != 0)
return false;
return true;
}
BOOL InitSocket(SOCKET *wSock)
{
*wSock = socket(AF_INET, SOCK_STREAM, 0);
if((SOCKET)wSock == INVALID_SOCKET)
return false;
return true;
}
BOOL InitWSA()
{
WSADATA wsaData;
WORD version;
version = MAKEWORD(2, 2);
int error;
error = WSAStartup(version, &wsaData);
if(error != 0)
return false;
if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2)
{
WSACleanup();
return false;
}
return true;
}
您能告诉我如何更改代码,或者程序中的错误在哪里吗?