опубликован 22-11-2001 21:39 MSK
bool UploadFile(LPCTSTR lpszHostName,LPCTSTR lpszUri,LPCTSTR lpszFileName)
{
//lets get this file
Saddr.sin_family=AF_INET;
Saddr.sin_port=htons(80);
Saddr.sin_addr.s_addr=*((unsigned long*)gethostbyname(lpszHostName)->h_addr_list[0]);
Sock=socket(AF_INET,SOCK_STREAM,0);
res=connect(Sock,(struct sockaddr *)&Saddr,sizeof(Saddr));
if(res!=0)return false;
/* res=WSAGetLastError();
res=10060;
switch(res)
{
case WSANOTINITIALISED: MessageBox(0,"A successful WSAStartup must occur before using this function.","hey",MB_OK);break;
case WSAENETDOWN: MessageBox(0,"The network subsystem has failed. ","hey",MB_OK);break;
case WSAEADDRINUSE: MessageBox(0,"The socket's local address is already in use and the socket was not marked to allow address reuse with SO_REUSEADDR. This error usually occurs when executing bind, but could be delayed until this function if the bind was to a partially wild-card address (involving ADDR_ANY) and if a specific address needs to be committed at the time of this function. ","hey",MB_OK);break;
case WSAEINTR: MessageBox(0,"The (blocking) Windows Socket 1.1 call was canceled through WSACancelBlockingCall. ","hey",MB_OK);break;
case WSAEINPROGRESS: MessageBox(0,"A blocking Windows Sockets 1.1 call is in progress, or the service provider is still processing a callback function. ","hey",MB_OK);break;
case WSAEALREADY: MessageBox(0,"A nonblocking connect call is in progress on the specified socket.Note In order to preserve backward compatibility, this error is reported as WSAEINVAL to Windows Sockets 1.1 applications that link to either WINSOCK.DLL or WSOCK32.DLL.","hey",MB_OK);break;
case WSAEADDRNOTAVAIL: MessageBox(0,"The remote address is not a valid address (such as ADDR_ANY). ","hey",MB_OK);break;
case WSAEAFNOSUPPORT: MessageBox(0,"Addresses in the specified family cannot be used with this socket. ","hey",MB_OK);break;
case WSAECONNREFUSED:MessageBox(0,"The attempt to connect was forcefully rejected. ","hey",MB_OK);break;
case WSAEFAULT: MessageBox(0,"The name or the namelen parameter is not a valid part of the user address space, the namelen parameter is too small, or the name parameter contains incorrect address format for the associated address family. ","hey",MB_OK);break;
case WSAEINVAL: MessageBox(0,"The parameter s is a listening socket, or the destination address specified is not consistent with that of the constrained group the socket belongs to. ","hey",MB_OK);break;
case WSAEISCONN:MessageBox(0,"The socket is already connected (connection-oriented sockets only). ","hey",MB_OK);break;
case WSAENETUNREACH: MessageBox(0,"The network cannot be reached from this host at this time. ","hey",MB_OK);break;
case WSAENOBUFS: MessageBox(0,"No buffer space is available. The socket cannot be connected. ","hey",MB_OK);break;
case WSAENOTSOCK: MessageBox(0,"The descriptor is not a socket. ","hey",MB_OK);break;
case WSAETIMEDOUT:MessageBox(0,"Attempt to connect timed out without establishing a connection. ","hey",MB_OK);break;
case WSAEWOULDBLOCK: MessageBox(0,"The socket is marked as nonblocking and the connection cannot be completed immediately. ","hey",MB_OK);break;
case WSAEACCES:MessageBox(0,"Attempt to connect datagram socket to broadcast address failed because setsockopt option SO_BROADCAST is not enabled. ","hey",MB_OK);break;
}
*/
strcpy(text,"GET ");
strcat(text,lpszUri);
strcat(text," HTTP/1.0\r\n");
send(Sock,text,strlen(text),0);
strcpy(text,"Host: ");
strcat(text,lpszHostName);
strcat(text,"\r\n");
res=send(Sock,text,strlen(text),0);
strcpy(text,"Accept:*/*\r\n");
res=send(Sock,text,strlen(text),0);
strcpy(text,"\r\n");
res=send(Sock,text,strlen(text),0);
HANDLE log=CreateFile(lpszFileName,GENERIC_WRITE,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_ARCHIVE,0);
SetFilePointer(log,0,0,FILE_END);
DWORD nWritten;
res=recv(Sock,text,1024,0);
if (res==0)return false;
LPTSTR received=strstr(text,"\r\n\r\n")+4;
unsigned int pos=received-text;//Возвращает, сколько симолов с буффере до первого вхождения указанной подстроки + 4
// memmove(text,received,res-pos);
WriteFile(log,received,res-pos,&nWritten,0);//Записываем все полученные данные кроме заголовка.
while(res!=0)
{
res=recv(Sock,text,1024,0);
WriteFile(log,text,res,&nWritten,0);
}
CloseHandle(log);
closesocket(Sock);//Закончили закачку файла
return true;
}
вот пример на апи. Качает по протоколу хттп, ессно без докачки.
Интерестно, можно на хттп докачку организовать...
lpszHostName - имя сервера, откуда скачиваем.
lpszUri - путь к файлу на сервере.
lpszFileName - под каким именем сохраниь файл на диске.