znalazłem cos takiego ale wystepuja mi błedy w kompilacji mozecie sprawdzic u siebie poprawnosc kodu i kompilacji?
credit:MSDN:
Kod:
#define NO_FILE_SPECIFIED -1
#define BAD_HOST -1
#define CONNECTION_FAILURE -1
#define BAD_SOCKET -1
#define RECEIVE_BUFFER_SIZE 100
int HTTPDownload(char *URL, char *LocalPath)
{
HANDLE hFile;
DWORD dwRead;
WSAData wsadata;
sockaddr_in HTTPData;
struct hostent *HTTPHost;
SOCKET HTTPSocket;
int iFile;
int iConnect;
int ct;
unsigned int RemoteAddr;
char *pFile;
char RemoteFileBuf[1000];
char Host[1000];
char *pHost;
char recvbuf[100];
char sendbuf[512];
if(strstr(URL, "/"))
{
sprintf(Host, "%s", URL);
pFile = strstr(URL, "/");
sprintf(RemoteFileBuf, "%s", pFile);
pFile = strstr(Host, "/");
*pFile = NULL;
printf(RemoteFileBuf);
pHost = &Host[0];
}
else
{
return NO_FILE_SPECIFIED;
}
WSAStartup(MAKEWORD(2,2), &wsadata);
HTTPSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(HTTPSocket == INVALID_SOCKET)
{
WSACleanup();
return BAD_SOCKET;
}
HTTPData.sin_family = AF_INET;
HTTPData.sin_port = htons(80);
if(isalpha(pHost[0]))
{
HTTPHost = gethostbyname(pHost);
if(HTTPHost == NULL)
return BAD_HOST;
}
else
{
RemoteAddr = inet_addr(pHost);
HTTPHost = gethostbyaddr((char*)&RemoteAddr, 4, AF_INET);
if(HTTPHost == NULL)
return BAD_HOST;
}
memcpy(&HTTPData.sin_addr.s_addr, HTTPHost->h_addr, HTTPHost->h_length);
iConnect = connect(HTTPSocket, (SOCKADDR*)&HTTPData, sizeof(HTTPData));
if(iConnect == SOCKET_ERROR)
{
WSACleanup();
return CONNECTION_FAILURE;
}
sprintf(sendbuf, "GET %s\r\n", RemoteFileBuf);
send(HTTPSocket, sendbuf, strlen(sendbuf), 0);
hFile = CreateFile(LocalPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
while(ct = recv(HTTPSocket, recvbuf, RECEIVE_BUFFER_SIZE, 0))
{
iFile = WriteFile(hFile, recvbuf, ct, &dwRead, NULL);
memset(recvbuf, 0, sizeof(recvbuf));
}
iFile = CloseHandle(hFile);
closesocket(HTTPSocket);
WSACleanup();
return 0;
}