Ciao ragazzi..io per comunicare via ftp con i server utilizzo questo metodo:
Scrivere file
codice:
bool AddFileToServer(string buffer, string file)/*string directory = "tree" for default*/
{
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
if(hInternet == NULL)
return false;
hFtpSession = InternetConnect(hInternet,"www.dominio.com",INTERNET_DEFAULT_FTP_PORT, "nome_utente","password", INTERNET_SERVICE_FTP, 0,0 );
if(hFtpSession == NULL)
{
int i = GetLastError();
DWORD dwError;
char error [500];
DWORD dwSize = sizeof(error);
InternetGetLastResponseInfo(&dwError,error,&dwSize);
return false;
}
char this_exe_path [600];
GetModuleFileName(NULL,this_exe_path,600);
string temp_file_path = this_exe_path;
int found = temp_file_path.find_last_of('\\');
temp_file_path = temp_file_path.substr(0,found);
temp_file_path += "\\temp_file";
ofstream out(temp_file_path);
out << buffer;
out.close();
string server_file_path = "";
server_file_path += file;
BOOL bRet2 = FtpPutFile(hFtpSession, temp_file_path.c_str(), server_file_path.c_str(), FTP_TRANSFER_TYPE_BINARY, 0);
DeleteFile(temp_file_path.c_str());
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
if(bRet2 == FALSE)
return false;
return true;
}
Leggere file
codice:
string GetFileToServer(string file_name)
{
string file_content;
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL,INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
if(hInternet == NULL)
{
MessageBox(NULL,"Impossibile aprire un HANDLE internet","Fatal Error!!",MB_ICONERROR);
return false;
}
hFtpSession = InternetConnect(hInternet,"www.dominio.com",INTERNET_DEFAULT_FTP_PORT, "nome_utente","password", INTERNET_SERVICE_FTP, 0,0 );
if(hFtpSession == NULL)
{
int i = GetLastError();
DWORD dwError;
char error [500];
DWORD dwSize = sizeof(error);
InternetGetLastResponseInfo(&dwError,error,&dwSize);
MessageBox(NULL,error,"Fatal Error!!",MB_ICONERROR);
InternetCloseHandle(hInternet);
return "";
}
string id = ::target_id;
string file_to_read;
file_to_read = file_name;
HINTERNET hFile = FtpOpenFile(hFtpSession,file_to_read.c_str(),GENERIC_READ,FTP_TRANSFER_TYPE_BINARY|INTERNET_FLAG_HYPERLINK,NULL);
if(hFile == NULL)
{
int i = GetLastError();
DWORD dwError;
char error [500];
DWORD dwSize = sizeof(error);
InternetGetLastResponseInfo(&dwError,error,&dwSize);
MessageBox(NULL,error,"Erorr FTP file",MB_ICONERROR);
return "";
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
}
int i = GetLastError();
DWORD high_order;
DWORD low_order = FtpGetFileSize(hFile,&high_order);
unsigned long long file_size = (high_order * (MAXDWORD+1)) + low_order;
char * buffer;
buffer = new char [file_size];
DWORD bytes_read;
BOOL res = InternetReadFile(hFile,buffer,file_size,&bytes_read);
if(bytes_read != file_size)
MessageBox(NULL,"bytes read different from file size!","Error",MB_ICONERROR);
file_content = buffer;
delete [] buffer;
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
InternetCloseHandle(hFile);
return file_content;
}
Sono comnsapevole che non sono funzioni perfette, nel senso che per esempio la scrittura dei dati su file nel primo dovrebb essere binaria, poi come parametri delle funzioni dovrei mettere nome utente, donio, password e gestire meglio le eccezzioni.
IN ogni caso il mio problema è un altro.
Se io instauro una connessione ftp con questo programma da due computer differenti, ipotiziamo che il computer1 esegua l'upload via ftp di "ciao.txt", come fa il computer2 a capire quando il computer1 ha finito l'upload e quindi può scaicare il file?
Avevo pensato a scriveree un codice PHP e fare due funzioni, che funzionano come i mutex per "bloccare" e "sbloccare" i file. Però sinceramente preferirei rimanero all'interno del linguaggio C++. Qualcuno ha idee?
2 piccoli OT
1) Vi darò notizie del problema nell'altro mio thread
2) (posterò anche in cerco lavoro/collaborazione ma già che ci sono spero non disturbi i moderatori) Ho intenzione di sviluppare una classe per la gestione delle connnessioni ftp con il modello della fstream, exempio
codice:
ftp::istream in(dominio,nome_utente,passwor,file_da_scaricare);
in >> mia_striga;
in.close();
Progetto non troppo complicato, c'è qualcuno che vorrebbe collaborare? me lo scriva :=)