[zeromq-announce] File transfer

Ralph Nemitz nemitz at logicway.de
Mon Jul 3 16:33:50 CEST 2017


Hello together,


I want to transfer files form client to server using zeromq.

It works fine, if I transfer files from server to client (see code), but 
I can't change the sender and the receiver,

id est, I want to transfer the files from client to server and not fron 
server to client.

How I have to change the code? Is ther anybody who can help me please?

Thx for your help!


Ralph



//client
#include <czmq.h>
#define CHUNK_SIZE  250000
#define PIPELINE    10

#define GATEWAY "tcp://192.168.11.1:5711"

int main (void)
{
     FILE *file = fopen ("file_at_client.txt", "w");
     assert (file);

     zsock_t *dealer = zsock_new_dealer (GATEWAY);

     zstr_send (dealer, "fetch");
     size_t total = 0;       //  Total bytes received
     size_t chunks = 0;      //  Total chunks received

     while (true) {
         zframe_t *frame = zframe_recv (dealer);
         if (!frame)
             break;              //  Shutting down, quit

         char *new_chunk = zframe_strdup (frame);
         fprintf( file, "%s", new_chunk );


         chunks++;
         free ( new_chunk );
         size_t size = zframe_size (frame);
         zframe_destroy (&frame);
         total += size;
         if (size == 0)
             break;              //  Whole file received
     }
     printf ("%zd chunks received, %zd bytes\n", chunks, total);
     fclose( file );
     zsock_destroy (&dealer);
     return 0;
}


//server
#include <czmq.h>
#define CHUNK_SIZE  250000
#define PIPELINE    10

#define GATEWAY "tcp://*:5711"

int main (void)
{
     FILE *file = fopen ("file_at_server.txt", "r");
     assert (file);

     zsock_t *router = zsock_new_router (GATEWAY);
     while (true) {
         //  First frame in each message is the sender identity
         zframe_t *identity = zframe_recv (router);
         if (!identity)
             break;              //  Shutting down, quit

         //  Second frame is "fetch" command
         char *command = zstr_recv (router);
         assert (streq (command, "fetch"));
         free (command);

         while (true) {
             byte *data = malloc (CHUNK_SIZE);
             assert (data);
             size_t size = fread (data, 1, CHUNK_SIZE, file);
             zframe_t *chunk = zframe_new (data, size);
             zframe_send (&identity, router, ZFRAME_REUSE + ZFRAME_MORE);
             zframe_send (&chunk, router, 0);
             if (size == 0)
                 break;          //  Always end with a zero-size frame
         }

         zframe_destroy (&identity);
     }
     fclose (file);

     zsock_destroy (&router);
}

-- 

--
###############################################################
Dipl.-Phys. Ralph Nemitz    Tel.:    (0385) 3993 9354
Logic Way GmbH              Fax:     (0385) 3993 458
Hagenower Strasse 73        HTTP:    http://www.logicway.de
19061 Schwerin              E-Mail:  nemitz at logicway.de
###############################################################




More information about the zeromq-announce mailing list