how can i in C++ send file to other PC over net ?

  • 16 years ago

    hey


    am new ,and am looking for code that make me get how could i Transfer file to other computer in Client Server style ,
    or in other words "FileTransfer " between 2 computers


    am coding with C++


    i wish some 1 can help me
    thanx a lot


    bye

  • 16 years ago

    Wrong place. (this is vb area)
    However, you can pickup a Winsock tutorial.


    it doesn't allow direct file sending, it works essentially with streams.


    but you can do something like this (after initialised the socket, see a winsock tutorial)




    sender:


    Code:

    define SEND_FILE 0x20


    define ACK 0x47



    struct fileinfo
     {
         char file
    name[MAXPATH];
         unsigned long file
    size;
         unsigned long crc;
     } info;


    char data[513];
    int packs, remain,read;


    openfile(&(info.filename), &(info.file_size), &(info.crc)); //this function doesn't exist, is supposed to open the file, get its size, and calc the crc


    memset(data, 0, 256);


    *memset=SEND_FILE;
    memcpy(data+1,&info,sizeof(info));


    send(socket,data,256);
    recv(socket,data,256);
    if(*data!=ACK)
       ;//transfer error


    remain=info.filesize % 512 ;
    packs=(info.file
    size-ramain)/512;


    while(packs--)
       {
            if(!ReadFile(file,data,512,&read,NULL))
                ; //read error
            if(written!=pack)
                ; //read error
            send(socket,data,512);
            recv(socket,data,256);
            if(*data!=ACK)
               ;//transfer error
        }
        if(!ReadFile(file,data,remain,&read,NULL))
           ; //read error
        if(written!=pack)
           ; //read error


        send(socket,data,remain);
        recv(socket,data,256);
        if(*data!=ACK)
           ;//transfer error
       }




    receiver:

    Code:

    define SEND_FILE 0x20


    define ACK 0x47



    struct fileinfo
     {
         char file
    name[MAXPATH];
         unsigned long file
    size;
         unsigned long crc;
     } info;


    char data[513];
    int packs, remain,written;


    open_file(); //this function doesn't exist, is supposed to get a filename, and open it for writing


    memset(data, 0, 256);


    recv(socket,data,256)
    if(data==SEND_FILE)
       {
           memcopy(&info,data+1,sizeof(info));
           
    data=ACK;
           send(socket,data,256);


           remain=info.filesize % 512 ;
           packs=(info.file
    size-ramain)/512;



           while(packs--)
              {
                  recv(socket,data,512);
                  if(!WriteFile(file,data,512,&written,NULL))
                     ; //write error
                  if(written!=pack)
                     ; //write error
                  *data=ACK;
                  send(socket,data,256);
              }
            recv(socket,data,remain);
            if(!WriteFile(file,data,remain,&written,NULL))
                ; //write error
            if(written!=pack)
                ; //write error
            *data=ACK;
            send(socket,data,256);


            if(calc_crc(file)!=info.crc)
               ; //transfeer error
       
       }



    I hope this work. I didn't test it. ACK and SEND_FILE aren't standards
    The crc is highly recomended


    good luck

  • 16 years ago

    yup ,it help me a lot  man....


    i really appreciate uer answer .


    c ya'  


  • 16 years ago

    How to open the file???And how to calculate the crc????


    Actually i am doing file transfer also.... but face a lot of problem... URGENT.. HELP!!!

  • 16 years ago

    I moved this topic over to the C++ forum.


    Here is a link you can look at for opening files.


    http://www.cpcug.org/user/clemenzi/technical/Languages/Console_C++/fstream.html


    You use fstream.h, so #include <fstream.h> and you have access to those functions.


    You can also try http://www.google.com and search for fstream.h.

  • 16 years ago

    thanx...
    Another question is : How to split a file to chunk files and recombine them???
    This is because i need to send the chunk file between 2 pc using multicast.
    Thanx!

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”