sending and receiving files using winsock

How to implement it properly?

This is what i have so far

Receiver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
 else if(command == 'f'){


    char read;
    std::string collector;          //filename
    std::string sizellector;        //filename

    do{                             //get filename

    recv(sock_CONNECTION,&read,1,0);   // for [filename]& 

    if(read !='&')                     //& is delimiter so no need to add
    collector += read;  

    }   while(read != '&');            //end at &



    read =0;//empty
    do{                                 //for [1400000]& which is the size
    recv(sock_CONNECTION,&read,1,0);    //get file type
    if(read !='&')                      //& is delimiter so no need to add
    sizellector += read;                //

    }while(read != '&');                //end


    //collect and complete
    long size = atol(sizellector.c_str());  //convert string to int


    char * buffer = new char[size];         //alocate



    char ptr[1024];                         //buffer
    int var = 0;                    

    while(var <= size){                     //
    recv(sock_CONNECTION,ptr,1024,0);       //receive
    strcat(buffer,ptr);                     //concatenate
    var=var+1024;                           //increase
    }





    std::ofstream outfile (collector,std::ofstream::binary);
    outfile.write (buffer,size);                    //write 

    SetConsoleTextAttribute(hConsole, 2);
    std::cout<<" >Client:has sent a file named "<<collector<<" size is "<<sizellector<<std::endl;   //output
    SetConsoleTextAttribute(hConsole, csbiInfo.wAttributes);


    send(sock_CONNECTION, "File transfer is successful",28 ,NULL);

    delete [] buffer
    outfile.close();

}


then this is my sender:
send(sock,buffer,size,NULL); //size is like 14000000 or higher, would it be possible to send them in 1 go?

Everything on the sender works fine except when sending the buffer itself(file).

Would it be possible to send in one go and let the receiver iterate? I am trying to send file to my tcp server but i can't seem to find the max values for the send and recv. Based from researching, they say that you have to loop until you get all of the bytes.
Last edited on
You never check the return codes of send/recv.

How do you synchronise what the server and client are doing?
Topic archived. No new replies allowed.