Добавил:
Kaz
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:
#include <winsock.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <string>
#include <fstream>
#include <iostream>
#include <conio.h>
#pragma comment (lib, "ws2_32")
#define MAX_CONNECTION_COUNT 2
#define MESSAGE_LENGTH 1024 * 4
using namespace std;
int PORT = 10001;
char* IP = "192.168.1.2";
void Initialization()
{
WSADATA WsaDat;
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
printf("WSA Initialization failed. Error code: %d", WSAGetLastError());
}
SOCKET CreateSocket()
{
SOCKET sock;
if (INVALID_SOCKET == (sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP) ) )
{
printf("Create socket error. Error code: %d", WSAGetLastError());
return NULL;
}
else return sock;
}
int setSockOptions(SOCKET sock)
{
timeval timeout;
timeout.tv_sec = 500;
timeout.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&timeout, sizeof(timeout));
return 0;
}
int Connect(SOCKET sock)
{
sockaddr_in name;
ZeroMemory (&name, sizeof (name));
name.sin_family = AF_INET;
name.sin_addr.s_addr = inet_addr(IP);
name.sin_port = htons(PORT);
if (connect(sock, (SOCKADDR *) &name, sizeof(name)) == SOCKET_ERROR)
{
printf("Connect error. Error code: %d", WSAGetLastError());
return 1;
}
return 0;
}
int SendData(SOCKET clientSocket, char* serverMessage)
{
if(send(clientSocket, serverMessage, strlen(serverMessage), 0) == SOCKET_ERROR)
{
closesocket(clientSocket);
return 1;
}
return 0;
}
/////------------------------------
char* GetFileData(string fileName, long position, long offset)
{
std::ifstream file(fileName, ios::in |ios::binary);
if (!file.is_open())
return NULL;
char* memblock;
file.seekg (position, ios::beg);
file.read (memblock, offset);
file.close();
return memblock;
}
int ParseRequest(string mess)
{
int index = mess.find("404");
if(index != string::npos)
return 4;
index = mess.find("102");
if(index != string::npos)
return 1;
index = mess.find("OOB_MSG recived!");
if(index != string::npos)
return 9;
return 0;
}
string ReceiveData(SOCKET clientSocket)
{
char* tmpMessage = new char[MESSAGE_LENGTH];
int messageLength;
u_long noOOB = -1;
ioctlsocket(clientSocket, SIOCATMARK, &noOOB);
if ((messageLength = recv (clientSocket, tmpMessage, MESSAGE_LENGTH, 0)) == -1 )
{
byte oob = -1;
int retVal2 = recv(clientSocket, (char *)(&oob), 1, MSG_OOB);
if (retVal2 == SOCKET_ERROR)
{
printf("Recive message error. Error code: %d", WSAGetLastError());
closesocket(clientSocket);
exit(1);
}
return string("OOB_MSG recived!");
}
string response(tmpMessage,messageLength);
delete []tmpMessage;
return response;
}
long getTempFileInfo(string filename)
{
string tempFileName(filename + ".tmp");
std::fstream tempFile(tempFileName, ios::out | ios::in | ios::app);
if(!tempFile.is_open()){
printf("Could not create temorary file!\n\r");
return -1;
}
tempFile.seekg (0, ios::end);
int length = tempFile.tellg();
tempFile.seekg (0, ios::beg);
char* tempFileContent = new char[length];
memset(tempFileContent,0,length);
tempFile.read(tempFileContent,length);
string temp(tempFileContent);
delete []tempFileContent;
if(temp.length() < 1 )
{
string tempFileLine(filename);
tempFileLine += " = 0\n\r";
tempFile.write(tempFileLine.c_str(),tempFileLine.length());
tempFile.close();
return 0;
}
int offset = temp.find(filename);
return atol(temp.substr(offset + filename.length() + 3, temp.find("\n\r",offset)).c_str());
}
int setTempFileInfo(string filename, long offset)
{
string tempFileName(filename + ".tmp");
std::fstream tempFile(tempFileName, ios::out | ios::trunc);
if(!tempFile.is_open()){
printf("Could not create temorary file!\n\r");
return -1;
}
string tempFileLine(filename);
tempFileLine += " = ";
char* tmp = new char(64);
tempFileLine += ltoa(offset, tmp , 10);
tempFileLine += "\n\r";
tempFile.write(tempFileLine.c_str(),tempFileLine.length());
tempFile.close();
return 0;
}
int SaveFilePart(string file, string content)
{
std::fstream File(file, ios::out | ios::app | ios::binary);
if(!File.is_open()){
printf("Could not create file!\n\r");
return 1;
}
File.seekg(0, ios::end);
File.write(content.c_str(),content.length());
long offset = File.tellg();
File.close();
setTempFileInfo(file, offset);
return 0;
}
int main(int argc, char *argv[])
{
char* file = new char[256];
printf("Input filename for download: ");
scanf("%s", file);
string filename(file);
delete []file;
long offset = getTempFileInfo(filename);
Initialization();
SOCKET Socket = CreateSocket();
setSockOptions(Socket);
if(Socket != NULL)
{
Connect(Socket);
do
{
string request("GET\n\r" + filename + "\n\r");
char* tmp = new char(64);
request += ltoa(offset, tmp , 10);
request += "\n\r";
if(SendData(Socket, (char*)request.c_str()))
{
break;
}
string response(ReceiveData(Socket));
if(response.length() < 30)
{
int status = ParseRequest(response);
if(status == 1)
{
printf(response.c_str());
break;
}
if(status == 4)
{
printf("Error : %s",response.c_str());
break;
}
if(status == 9)
{
printf("OOB data recived! Total bytes recived: %ld\n", offset);
continue;
}
}
SaveFilePart(filename,response);
offset += response.length();
}
while(1);
}
remove(filename.append(".tmp").c_str());
closesocket(Socket);
WSACleanup();
printf ("\n\nDone!\n\n");
getch();
return 0;
}