Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - inviare file via ftp
Forum - C/C++ - inviare file via ftp

Pagine: [ 1 2 3 ] Precedente | Prossimo
Avatar
nnloso (Normal User)
Newbie


Messaggi: 9
Iscritto: 16/10/2011

Segnala al moderatore
Postato alle 16:18
Domenica, 16/10/2011
salve vorrei riuscire a inviare un file a un mio sito potreste dirmi il comando per farlo o magari un esempio


in internet navigando ho trovato questo metodo usando questa stringa

Codice sorgente - presumibilmente Plain Text

  1. UploadFile ("c: \ \ UploadFile.doc", "ftp://FTPHostName/UploadPath/UploadFile.doc", "username", "Password");



xro nn dicevano in che libreria si trova e anche cercando in internet nn ho trovato nnt
mi serve urgentemente :hail::hail::hail::hail::hail::hail::hail:

PM Quote
Avatar
Nullable (Normal User)
Expert


Messaggi: 217
Iscritto: 12/07/2011

Segnala al moderatore
Postato alle 16:35
Domenica, 16/10/2011
In C non esiste la funzione UploadFile, semmai avrai letto qualcosa inerente al C++ ( programmazione ad oggetti ) come questo -> http://www.digitalcoding.com/Code-Snippets/CPP-CLI/C-CLI-C ...

PM Quote
Avatar
nnloso (Normal User)
Newbie


Messaggi: 9
Iscritto: 16/10/2011

Segnala al moderatore
Postato alle 16:39
Domenica, 16/10/2011
scusa ho sbagliato sono nuovo sto ancora imparando a programmare

PM Quote
Avatar
Nullable (Normal User)
Expert


Messaggi: 217
Iscritto: 12/07/2011

Segnala al moderatore
Postato alle 16:42
Domenica, 16/10/2011
Testo quotato

Postato originariamente da nnloso:

scusa ho sbagliato sono nuovo sto ancora imparando a programmare



Tranquillo, qui nessuno ti aggredirà o disprezzerà; qui sarai solo aiutato. Comunque, cosa hai intenzione di fare esattamente ? Un programma che uppa su un server dei files ? Se è questo ciò che vuoi fare ti consiglio di studiarti prima la teoria, partendo ovviamente dall'inizio, in modo da comprendere quello che vuoi andare a fare.

Ultima modifica effettuata da Nullable il 16/10/2011 alle 16:42
PM Quote
Avatar
Pitagora (Member)
Expert


Messaggi: 367
Iscritto: 12/06/2010

Segnala al moderatore
Postato alle 16:46
Domenica, 16/10/2011
Bene bene... Sempre la solita storia:

<< Il C è vecchio e quindi non dispone di funzioni fighe, come quella che cerchi tu. >>

nonostante questo, è possibile scrivere una funzione che invia un file usando il protocollo FTP. Come? Collegarsi al server tramite la porta 21, e dialogare con il server attraverso i socket. I comandi sono questi: http://www.nsftools.com/tips/RawFTP.htm e chiaramente dovranno essere inviati come se fossero stringhe. Tempo fa scrissi un applicazione, in C, che inviava al server un file.

Puoi studiarla:
Codice sorgente - presumibilmente Delphi

  1. //      ONLY FOR UNIX SYSTEM!
  2. //      NOT FOR WINDOWS!
  3. //
  4. //      this is the first version: 1.0
  5. //
  6. //      Mind_Blog_ADMIN.c
  7. //      
  8. //      Copyright 2011 Francesco <pitagora@laptop>
  9. //      email <francescoleone.23@gmail.com>
  10. //      website <http://pitagora.hellospace.net>
  11. //
  12. //      This program is free software; you can redistribute it and/or modify
  13. //      it under the terms of the GNU General Public License as published by
  14. //      the Free Software Foundation; either version 2 of the License, or
  15. //      (at your option) any later version.
  16. //      
  17. //      This program is distributed in the hope that it will be useful,
  18. //      but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. //      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. //      GNU General Public License for more details.
  21. //      
  22. //      You should have received a copy of the GNU General Public License
  23. //      along with this program; if not, write to the Free Software
  24. //      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25. //      MA 02110-1301, USA.
  26.  
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <time.h>
  30. #include <string.h>
  31. #include <netinet/in.h>
  32. #include <arpa/inet.h>
  33. #include <sys/socket.h>
  34. #include <sys/types.h>
  35.  
  36. #define FILE_PHP "index.php"
  37. #define FILE_XML "data.xml"
  38. #define MAX_BUF 500
  39. #define VERO 1
  40. #define FALSO 0
  41.  
  42.  
  43. int port_store; /* usata per ricevere/inviare file */
  44. int port_general = 21; /* port_general, usata per la connessione principale */
  45.        
  46. /* Principali :) */
  47. void install(void); /* permette di installare il CMS */
  48. void inet_nexus (struct sockaddr_in*, const in_addr_t, const int); /* permette di impostare i dati per la connessione */
  49. void my_send (int, const char*, const char*); /* permette di inviare dati al server */
  50. void my_recv (int, const int); /* permette di ricevere i dati dal server */
  51. void format (char []); /* formatta il testo rivecuto dal server */
  52. void store (const int, const char*); /* invia il file al server */
  53. void update (FILE *, int, char [], char [], char [], struct tm*, char []); /* aggiorna il file xml con i nuovi dati */
  54.        
  55. /* Sotto - funzioni :) */
  56. /* install --> */ void data_xml_func (FILE *); /* permette di creare il file data.xml */
  57. /* install --> */ void index_php_func (FILE *); /* permette di salvare il file index.php */
  58. /* add_new_article ---> */ void retrv (int, char[]); /* usata per il download dei file */
  59.  
  60.  
  61. void add_new_article (void); /* aggiunge nuovi articoli al blog */
  62.  
  63. int main (void)
  64. {
  65.         int scelta;
  66.         fprintf (stdout, "\t\tMind Blog - Admin\t\t\n\n");
  67.         fprintf (stdout, "1 = Installa Mind Blog sul tuo web!\n");
  68.         fprintf (stdout, "2 = Inserisci un nuovo articolo sul tuo Mind Blog!\n");
  69.         fprintf (stdout, "3 = Esci, semplicmente!\n");
  70.         fprintf (stdout, "? = ");
  71.         fscanf (stdin, "%d", &scelta);
  72.         switch (scelta) {
  73.                 case 1: install (); break;
  74.                 case 2: add_new_article (); break;
  75.                 case 3: exit (EXIT_SUCCESS);
  76.                 default: fprintf (stderr, "Errore nell'inserimento del numero!\n"); exit (EXIT_FAILURE);
  77.         }
  78.         return 0;
  79. }
  80.  
  81.  
  82. void install()
  83. {
  84.         FILE *data_xml, *index_php; /* usati per gli stream dei due file necessari per il funzionamento del CMS */
  85.         char ip[20]; /* necessario per collegarsi all' FTP */
  86.         char username[20]; /* necessario per accedere all' FTP */
  87.         char password[20]; /* necessario per accedere all' FTP */
  88.         struct sockaddr_in info_general, info_store; /* struttura usata per impostare la connessione */
  89.         int sockfd_general, sockfd_store; /* stream socket */
  90.          
  91.         /* creo ed apro i file */
  92.         index_php = fopen (FILE_PHP, "w");
  93.         data_xml = fopen (FILE_XML, "w");
  94.        
  95.         /* se trovo qualche errore, avvisa ed esci */
  96.         if (index_php == NULL || data_xml == NULL) {
  97.                 fprintf (stderr, "Errore nell'apertura del file!\n");
  98.                 exit (EXIT_FAILURE);
  99.         }
  100.        
  101.         /* scrivi nel file data.xml */
  102.         data_xml_func (data_xml);
  103.                    
  104.         /* scrivi nel file index.php */    
  105.         index_php_func (index_php);
  106.        
  107.         /* chiudi i file prcedentemente aperti */
  108.         fcloseall ();
  109.        
  110.         printf ("Inserisci il tuo IP es: xx.xx.xx.xx : ");
  111.         scanf ("%s", ip);
  112.        
  113.         printf ("Inserisci il tuo username: ");
  114.         scanf ("%s", username);
  115.        
  116.         printf ("Inserici la tua password: ");
  117.         scanf ("%s", password);
  118.  
  119.         /* imposto lo stream socket e controllo eventuali errori */
  120.         sockfd_general = socket (AF_INET, SOCK_STREAM, 0);
  121.         if (sockfd_general < 0)
  122.         {
  123.                 fprintf (stderr, "Errore nella creazione del socket!\n");
  124.                 exit(EXIT_FAILURE);
  125.         }
  126.        
  127.         /* inizializzo la connessione */
  128.         inet_nexus (&info_general, inet_addr(ip), port_general);
  129.        
  130.         /* mi connetto all'host e controllo se ci sono errori */
  131.         if ( (connect (sockfd_general, (struct sockaddr *) &info_general, sizeof (struct sockaddr_in) ) ) < 0)
  132.         {
  133.                 fprintf (stderr, "Erorre nella connessione al server!\n" \
  134.                                  "Controlla se si ha impostato correttamente l'ip, l'username ed infine la password\n");
  135.                 exit (EXIT_FAILURE);
  136.    }
  137.    /*invio dati */
  138.    my_recv (sockfd_general, FALSO);
  139.    my_send (sockfd_general, "USER", username);
  140.    my_recv (sockfd_general, FALSO);
  141.    my_send (sockfd_general, "PASS", password);
  142.    my_recv (sockfd_general, FALSO);
  143.    my_send (sockfd_general, "MKD", "blog");
  144.    my_recv (sockfd_general, FALSO);
  145.    my_send (sockfd_general, "CWD", "blog");
  146.    my_recv (sockfd_general, FALSO);
  147.    my_send (sockfd_general, "TYPE", "I");
  148.    my_recv (sockfd_general, FALSO);
  149.    my_send (sockfd_general, "PASV", "");
  150.    my_recv (sockfd_general, VERO);
  151.    
  152.    /* connetto alla nuova porta, inizializzando i dati */
  153.    sockfd_store = socket (AF_INET , SOCK_STREAM, 0);
  154.    if (sockfd_store < 0)
  155.    {
  156.                 fprintf (stderr, "Errore nella creazione del socket!\n");
  157.                 exit (EXIT_FAILURE);
  158.    }
  159.    
  160.    inet_nexus (&info_store, inet_addr(ip), port_store);
  161.    
  162.    if ( (connect (sockfd_store, (struct sockaddr *) &info_store, sizeof (struct sockaddr_in) ) ) < 0)
  163.    {
  164.                 fprintf(stderr, "Errore nella connessione al server!\n");
  165.                 exit (EXIT_FAILURE);
  166.         }
  167.        
  168.         /* ora che è aperta una nuova connessione, posso inviare il file, non prima però di dirlo al server! */
  169.        
  170.         fprintf (stdout, "\n[+] File creati con successo\n[+]Inizio ad inviare il file\n");
  171.         /* adesso, invia il file: DATA.XML */
  172.         my_send (sockfd_general, "STOR", FILE_XML);
  173.         my_recv (sockfd_general, FALSO);
  174.        
  175.         store (sockfd_store, FILE_XML);
  176.         my_recv (sockfd_general, FALSO);
  177.         close (sockfd_store);
  178.        
  179.         fprintf(stdout, "[+] %s spedito con successo\n[+]Inizio ad inviare il file\n", FILE_XML);
  180.        
  181.        
  182.         /* ripetiamo la stessa cosa con il file: INDEX.PHP */
  183.         my_send (sockfd_general, "PASV", "");
  184.    my_recv (sockfd_general, VERO);
  185.    
  186.    /* connetto alla nuova porta, inizializzando i dati */
  187.    sockfd_store = socket (AF_INET , SOCK_STREAM, 0);
  188.    if (sockfd_store < 0)
  189.    {
  190.                 fprintf (stderr, "Errore nella creazione del socket!\n");
  191.                 exit (EXIT_FAILURE);
  192.    }
  193.    
  194.    inet_nexus (&info_store, inet_addr(ip), port_store);
  195.    
  196.    if ( (connect (sockfd_store, (struct sockaddr *) &info_store, sizeof (struct sockaddr_in) ) ) < 0)
  197.    {
  198.                 fprintf(stderr, "Errore nella connessione al server!\n");
  199.                 exit (EXIT_FAILURE);
  200.         }
  201.         my_send (sockfd_general, "STOR", FILE_PHP);
  202.         my_recv (sockfd_general, FALSO);
  203.         store (sockfd_store, FILE_PHP);
  204.         my_recv (sockfd_general, FALSO);
  205.         close (sockfd_store);
  206.         fprintf(stdout, "[+] %s spedito con successo!\n", FILE_PHP);
  207.         /* esco dall'FTP e chiudo */
  208.     my_send (sockfd_general, "QUIT", "");
  209.     my_recv (sockfd_general, FALSO);
  210.     close (sockfd_general);
  211.         fprintf(stdout, "L'installazione e' avvenuta correttamente.\nGrazie per aver intallato, Mind Blog\n\nPitagora\n\nP.S. ");
  212.         fprintf(stdout, "Il tuo blog e' raggiungibile al tuo indirizzo aggiungendo un /blog\nes: www.pitagora.hellospace.net/blog\n");
  213.         remove(FILE_PHP);
  214.         remove(FILE_XML);
  215.         return;
  216. }
  217.  
  218. void store (int sockfd, const char * nome_file)
  219. {
  220.         int size;
  221.         char *buffer;
  222.         FILE *pag = fopen (nome_file, "r");
  223.         if (pag == NULL)
  224.         {
  225.                 fprintf(stderr, "Errore, nell'aprire il file %s, Assicurati di non averlo cancellato o spostato." \
  226.                                 "Ri-avvia l'installazione\n", nome_file);
  227.                 exit (EXIT_FAILURE);
  228.         }
  229.         /* ossento la dimensione del file */
  230.         fseek (pag, 0, SEEK_END);
  231.         size = ftell (pag);
  232.         rewind (pag);
  233.        
  234.         buffer = calloc(1, sizeof (char) * (size + 1) ); /* alloco un buffer da contenere tutti i caratteri del file */
  235.         if (buffer == NULL)
  236.         {
  237.                 fprintf(stderr, "Scusa, ma non puoi inserire più articoli, lo spazio e\' insufficiente\n");
  238.                 exit (EXIT_FAILURE);
  239.         }
  240.         /* leggo il testo e lo invio */
  241.        
  242.         fread (buffer, size, 1, pag);
  243.         write (sockfd, buffer, strlen (buffer) );
  244.        
  245.         /* chiudo tutto */
  246.         free(buffer);
  247.         fclose (pag);
  248.         close (sockfd);
  249.         return;
  250. }
  251.  
  252. void format (char testo[])
  253. {
  254.         int p1, p2;
  255.         int virgole = 0;
  256.         char *frase;
  257.         frase = strtok (testo, ",)");
  258.         while (frase != NULL)
  259.         {
  260.                 virgole += 1;
  261.                 if (virgole == 5) {
  262.                         p1 = atoi (frase);
  263.                         break;
  264.                 }
  265.                 frase = strtok (NULL, ",)");
  266.         }
  267.         frase = strtok (NULL, ")");
  268.         p2 = atoi (frase);
  269.         port_store = p1*256+p2; /* assegno la nuova porta per il trasferimento dei file */
  270.         return;
  271. }
  272.  
  273. void my_recv (int stream, const int bool)
  274. {
  275.         char buffer[MAX_BUF];
  276.         /* azzero il buffer ad ogni chiamata */
  277.         memset (buffer, 0, MAX_BUF);
  278.         read (stream, buffer, MAX_BUF);
  279.         if (bool)
  280.                 format (buffer);
  281.         return;
  282. }
  283.  
  284. void my_send (int stream, const char* comando, const char* dato)
  285. {
  286.         char buffer[MAX_BUF];
  287.         /* azzero il buffer ad ogni chiamata */
  288.         memset (buffer, 0, MAX_BUF);
  289.         /* l'invio deve essere così formattato: COMANDO dato \r\nSYST\r\n */
  290.         /* copio dentro buffer, prima il comando e poi aggiungo il dato, e poi \r\nSYST\r\n */
  291.         sprintf (buffer, "%s %s\r\n", comando, dato);
  292.         write (stream, buffer, strlen (buffer) );
  293.         return;
  294. }
  295.  
  296. void inet_nexus (struct sockaddr_in *data, const in_addr_t ip, const int port)
  297. {
  298.         data->sin_family = AF_INET;
  299.         data->sin_port = htons (port);
  300.         data->sin_addr.s_addr = (unsigned long) ip;
  301.         return;
  302. }
  303.  
  304. void data_xml_func (FILE *database)
  305. {
  306.         time_t raw_time; /* usata per prelevare l'ora */
  307.         struct tm *time_s; /* usata per poter stampare singoli pezzi della data */
  308.         time (&raw_time); /* prelevo il tempo */
  309.         time_s = localtime (&raw_time); /* prelevo la data */
  310.         fprintf (database, "<\?xml version=\"1.0\" encoding=\"utf-8\"\?>\n" \
  311.                           "<!-- 1 -->\n" \
  312.                           "<blog>\n"\
  313.                           "\t<articolo id=\"1\">\n" \
  314.                           "\t\t<titolo>Welcome to Mind blog</titolo>\n" \
  315.                           "\t\t<testo>Grazie per aver installato Mind Blog.\n" \
  316.                           "Grazie a Mind Blog, potrai gestire i tuoi dati, con totale sicurezza\n" \
  317.                           "e senza l'ausilio di un database!\n"
  318.                           "Per maggiori informazioni puoi contattare Pitagora all'indirizzo email: francescoleone.23@gmail.com\n" \
  319.                           "P.S. Ricorda che Mind Blog - CMS e' un progetto open source. Per visionare il codice visita il forum.\n" \
  320.                           "\t\t</testo>\n" \
  321.                           "\t\t<autore>Pitagora</autore>\n" \
  322.                           "\t\t<data>%d/%d/%d</data>\n" \
  323.                           "\t</articolo>\n" \
  324.                           "</blog>\n", time_s->tm_mday, (time_s->tm_mon + 1), (time_s->tm_year + 1900) );
  325.         return;
  326. }
  327.  
  328. void index_php_func (FILE *index)
  329. {
  330.         fprintf (index, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n" \
  331.                            "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" \
  332.                            "<head>\n" \
  333.                            "\t<title> Simple - Blog </title>\n" \
  334.                            "\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>" \
  335.                            "</head>\n" \
  336.                            "<body>\n" \
  337.                            "\t<p align=left>\n" \
  338.                            "\t<font color=\"red\" size=\"10\"> Blog </font>" \
  339.                            "\t</p>\n" \
  340.                            "\t<\?php\n" \
  341.                            "\t\t$xml = simplexml_load_file(\'data.xml\');\n" \
  342.                            "\t\tforeach ( $xml->articolo as $articolo ) {\n" \
  343.                            "\t\t\tprint \"<h1><center>\" . $articolo->titolo . \"</h1></center><br />\" .\n" \
  344.                            "\t\t\t \"<p align=justify> <font color=\\\"black\\\" size=\\\"5\\\">\" . $articolo->testo . \"</font></p><br /><br />\" .\n" \
  345.                            "\t\t\t \"<p align=right> <font color=\\\"grey\\\" size=\\\"6\\\">\" . $articolo->autore . \"-->\" . $articolo->data . \"</font></p><br /><br />\" .\n" \
  346.                            "\t\t\t \"<hr /> <br />\";\n" \
  347.                            "\t\t}\n" \
  348.                            "\t\?>\n" \
  349.                            "\t<font size=\"1\"><p><a href=\"http://mindblog.pitagora.hellospace.net\">Mind Blog - Pitagora</a></p></font>\n" \
  350.                            "</body>\n");
  351.         return;
  352. }
  353.        
  354.  
  355. void add_new_article ()
  356. {
  357.         char username[20], ip[20], password[20]; /* usati per connettersi al server */
  358.         char titolo[30], testo[BUFSIZ], autore[20]; /* usati per comporre il corpo del messaggio */
  359.         char corpo[BUFSIZ]; /* usata per concatenare: il titolo, il testo e l'autore */
  360.         char buffer[BUFSIZ];
  361.         time_t raw_time; /* usata per prelevare l'ora */
  362.         struct tm *time_s; /* usata per poter stampare singoli pezzi della data */
  363.         struct sockaddr_in info_general, info_store, info_retrv; /* per le connessioni */
  364.         int sockfd_general, sockfd_store, sockfd_retrv; /* stream associati alle connessioni */
  365.         FILE *xml; /* per aprire e scrivere nel file xml */
  366.         int elemento = 0; /* usato per inizializzare il nuovo articolo inserito nel xml */
  367.        
  368.         printf ("Inserisci il tuo IP es: xx.xx.xx.xx : ");
  369.         scanf ("%s", ip);
  370.        
  371.         printf ("Inserisci il tuo username: ");
  372.         scanf ("%s", username);
  373.        
  374.         printf ("Inserici la tua password: ");
  375.         scanf ("%s", password);
  376.  
  377.         /* imposto lo stream socket e controllo eventuali errori */
  378.         sockfd_general = socket (AF_INET, SOCK_STREAM, 0);
  379.         if (sockfd_general < 0)
  380.         {
  381.                 fprintf (stderr, "Errore nella creazione del socket!\n");
  382.                 exit(EXIT_FAILURE);
  383.         }
  384.        
  385.         /* inizializzo la connessione */
  386.         inet_nexus (&info_general, inet_addr(ip), port_general);
  387.        
  388.         /* mi connetto all'host e controllo se ci sono errori */
  389.         if ( (connect (sockfd_general, (struct sockaddr *) &info_general, sizeof (struct sockaddr_in) ) ) < 0)
  390.         {
  391.                 fprintf (stderr, "Erorre nella connessione al server!\n" \
  392.                                  "Controlla se si ha impostato correttamente l'ip, l'username ed infine la password\n");
  393.                 exit (EXIT_FAILURE);
  394.    }
  395.    
  396.    /*invio dati */
  397.    my_recv (sockfd_general, FALSO);
  398.    my_send (sockfd_general, "USER", username);
  399.    my_recv (sockfd_general, FALSO);
  400.    my_send (sockfd_general, "PASS", password);
  401.    my_recv (sockfd_general, FALSO);
  402.    my_send (sockfd_general, "CWD", "blog");
  403.    my_recv (sockfd_general, FALSO);
  404.    my_send (sockfd_general, "TYPE", "I");
  405.    my_recv (sockfd_general, FALSO);
  406.    my_send (sockfd_general, "PASV", "");
  407.    my_recv (sockfd_general, VERO);
  408.    
  409.    /* mi connetto alla nuova porta per il download dei file */
  410.    sockfd_retrv = socket (AF_INET , SOCK_STREAM, 0);
  411.    if (sockfd_retrv< 0)
  412.    {
  413.                 fprintf (stderr, "Errore nella creazione del socket!\n");
  414.                 exit (EXIT_FAILURE);
  415.    }
  416.    
  417.    inet_nexus (&info_retrv, inet_addr(ip), port_store);
  418.    
  419.    if ( (connect (sockfd_retrv, (struct sockaddr *) &info_retrv, sizeof (struct sockaddr_in) ) ) < 0)
  420.    {
  421.                 fprintf(stderr, "Errore nella connessione al server!\n");
  422.                 exit (EXIT_FAILURE);
  423.         }
  424.         my_send (sockfd_general, "RETR", FILE_XML);
  425.         my_recv (sockfd_general, FALSO);
  426.         retrv (sockfd_retrv, buffer);
  427.         my_recv (sockfd_general, FALSO);
  428.         close (sockfd_retrv);
  429.        
  430.         /* ottieni il numero di articoli inseriti dentro il database flat */
  431.         sscanf (buffer, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- %d -->", &elemento);
  432.        
  433.         /* incremento l'elemento */
  434.         ++elemento;
  435.        
  436.         while (getchar() != '\n'); /* svuoto il buffer */
  437.        
  438.         fprintf (stdout, "Inserisci il titolo dell'articolo: ");
  439.         fgets (titolo, sizeof (titolo), stdin);
  440.         titolo[strlen (titolo) - 1] = '\0'; /* elimino il new-line */
  441.        
  442.         fprintf (stdout, "Inserisci il testo dell'articolo. Per concludere premi invio:\n ");
  443.         fgets (testo, sizeof (testo), stdin);
  444.         testo[strlen (testo) - 1] = '\0';
  445.        
  446.         fprintf (stdout, "Inserisci l'autore: ");
  447.         fgets (autore, sizeof (autore), stdin);
  448.         autore[strlen (autore) - 1] = '\0';
  449.        
  450.         /* ottengo la data */
  451.         time (&raw_time);
  452.         time_s = localtime(&raw_time);
  453.        
  454.         /* apro il file per inserire i dati aggiornati */
  455.         xml = fopen (FILE_XML, "w");
  456.         if (xml == NULL)
  457.         {
  458.                 fprintf (stderr, "Errore nella creazione del file %s\n", FILE_XML);
  459.                 exit (EXIT_FAILURE);
  460.         }
  461.        
  462.         /* aggiorno il file aggiungendo il titolo il testo, l'autore, la data più il vecchio contenuto */
  463.         update (xml, elemento, titolo, testo, autore, time_s, buffer);
  464.        
  465.        
  466.         /* adesso devo rispedire il file */
  467.         my_send (sockfd_general, "PASV", "");
  468.     my_recv (sockfd_general, VERO);
  469.    
  470.     /* connetto alla nuova porta, inizializzando i dati */
  471.     sockfd_store = socket (AF_INET , SOCK_STREAM, 0);
  472.     if (sockfd_store < 0)
  473.     {
  474.                 fprintf (stderr, "Errore nella creazione del socket!\n");
  475.                 exit (EXIT_FAILURE);
  476.     }
  477.    
  478.     inet_nexus (&info_store, inet_addr(ip), port_store);
  479.    
  480.     if ( (connect (sockfd_store, (struct sockaddr *) &info_store, sizeof (struct sockaddr_in) ) ) < 0)
  481.     {
  482.                 fprintf(stderr, "Errore nella connessione al server!\n");
  483.                 exit (EXIT_FAILURE);
  484.         }
  485.         fprintf (stdout, "Invio i dati\n");
  486.         my_send (sockfd_general, "STOR", FILE_XML);
  487.         my_recv (sockfd_general, FALSO);
  488.         store (sockfd_store, FILE_XML);
  489.         my_recv (sockfd_general, FALSO);
  490.         close (sockfd_store);
  491.        
  492.         close (sockfd_general);
  493.        
  494.         remove (FILE_XML);
  495.        
  496.         fprintf(stdout, "Dati inviati con successo! Prova ad aggiornare la pagina.\n");
  497.        
  498.        
  499.         return;
  500. }
  501.  
  502. void update (FILE *stream, int n, char title[], char text[], char author[], struct tm *time_s, char buffer[])
  503. {
  504.         int contatore = 0;
  505.        
  506.         /* scrive nel file i nuovi dati ... */
  507.         fprintf (stream, "<\?xml version=\"1.0\" encoding=\"utf-8\"\?>\n" \
  508.                           "<!-- %d -->\n" \
  509.                           "<blog>\n"\
  510.                           "\t<articolo id=\"%d\">\n" \
  511.                           "\t\t<titolo>%s</titolo>\n" \
  512.                           "\t\t<testo>%s" \
  513.                           "\t\t</testo>\n" \
  514.                           "\t\t<autore>%s</autore>\n" \
  515.                           "\t\t<data>%d/%d/%d</data>\n" \
  516.                           "\t</articolo>\n", n, n, title, text, author, time_s->tm_mday, (time_s->tm_mon + 1), (time_s->tm_year + 1900) );     
  517.          
  518.          n = 0;
  519.          
  520.          /* questo pasticcio mi permette di capire da dove devo iniziare a copiare */
  521.          while (1)
  522.          {
  523.                  if (buffer[n] != '>')
  524.                  {
  525.                          n += 1;
  526.                  } else {
  527.                          n += 1;
  528.                          contatore += 1;
  529.                          if (contatore == 3)
  530.                          {
  531.                                  break;
  532.                          }
  533.                  }
  534.          }
  535.          
  536.          /* .. ed aggiunge quelli vecchi */
  537.          for (; buffer[n] != '\0'; n++)
  538.          {
  539.                  fputc (buffer[n], stream);
  540.          }
  541.          
  542.          fclose (stream);
  543.          return;
  544. }
  545.        
  546.  
  547. void retrv (int sockfd, char buffer[])
  548. {
  549.         char buffer1[BUFSIZ];
  550.        
  551.         memset (buffer1, 0, sizeof (buffer1) ); /* azzero i due buffer */
  552.         memset (buffer, 0, sizeof (buffer) );
  553.        
  554.         /* finchè c'è qualche cosa da leggere leggi */
  555.         while (read (sockfd, buffer1, sizeof (buffer1) ) != 0) {
  556.                 strncat (buffer, buffer1, strlen (buffer1) ); /* lo concatena al buffer */
  557.                 memset (buffer1, 0, sizeof (buffer1) ); /* ri-azzera la memoria del buffer1 */
  558.         }
  559.         return;
  560. }



Inutile dire che bisogna usare header dipendenti dall'OS in quanto il C non dispone di funzioni standard per i socket. La risposta ORA, penso sia scontata.

PM Quote
Avatar
Nullable (Normal User)
Expert


Messaggi: 217
Iscritto: 12/07/2011

Segnala al moderatore
Postato alle 16:52
Domenica, 16/10/2011
Testo quotato

Postato originariamente da Pitagora:

Bene bene... Sempre la solita storia:

<< Il C è vecchio e quindi non dispone di funzioni fighe, come quella che cerchi tu. >>



...io non ho scritto che la funzione UploadFile non esiste perchè il C è "vecchio", ho semplicemente informato l'utente del fatto che non esiste nessun header che faccia riferimento ad una funzione che serve per uppare files su un server.

PM Quote
Avatar
Pitagora (Member)
Expert


Messaggi: 367
Iscritto: 12/06/2010

Segnala al moderatore
Postato alle 16:55
Domenica, 16/10/2011
Testo quotato

Postato originariamente da Nullable:
...io non ho scritto che la funzione UploadFile non esiste perchè il C è "vecchio", ho semplicemente informato l'utente del fatto che non esiste nessun header che faccia riferimento ad una funzione che serve per uppare files su un server.


Il mio era solo un incipit, tranquillo :D

Ultima modifica effettuata da Pitagora il 16/10/2011 alle 16:56
PM Quote
Avatar
Umberto (Member)
Pro


Messaggi: 156
Iscritto: 27/09/2011

Segnala al moderatore
Postato alle 17:58
Domenica, 16/10/2011
io userei la funzione system avviando i demoni ftp nei vari os

PM Quote
Avatar
nessuno (Normal User)
Guru^2


Messaggi: 6402
Iscritto: 03/01/2010

Segnala al moderatore
Postato alle 18:31
Domenica, 16/10/2011
Testo quotato

Postato originariamente da Umberto:

io userei la funzione system avviando i demoni ftp nei vari os



I "demoni" avviano i "server ftp" mentre lui chiede il "client ftp" ...

:-?


Ricorda che nessuno è obbligato a risponderti e che nessuno è perfetto ...
---
Il grande studioso italiano Bruno de Finetti ( uno dei padri fondatori del moderno Calcolo delle probabilità ) chiamava il gioco del Lotto Tassa sulla stupidità.
PM Quote
Pagine: [ 1 2 3 ] Precedente | Prossimo