Best way to send video over TCP socket in C

I am trying to think about how to transfer a video file over a TCP socket. I made a standard socket program, but after the command readI’m not sure how I can save it.

Code example

//bind server socketfd
if (bind(sfdServer, (struct sockaddr*)&adrServer, ServerAddrLen) < 0)
    error("ERROR binding");
listen(sfdServer, 5);
while(1){
    printf("Waiting for connections...\n");
    sfdClient = accept(sfdServer, (struct sockaddr*)&adrClient, &ClientAddrLen);
    if(sfdClient < 0)
        error("ERROR accepting");

    printf("Connection Established.\n");
    //set buffer to zero        
    bzero(buff, 2048);
    printf("Reading from client.\n");
    numChar = read(sfdClient, buff, 2048);

    //What should go here?

    close(sfdClient);
    close(sfdServer);

}

Would I just save the buffer as a movie.mp4 file or something like that? I understand that I may need to resize my buffer or perhaps send it to pieces. But I can not find good information on how to do this. Any help or point in the right direction would be appreciated!

+5
source share
2 answers

You must write pieces of buffer to a file.

First open the output file for writing:

   #include <sys/types.h>
   #include <sys/stat.h>
   #include <fcntl.h>


   int outfd;
   outfd = open("movie.mp4", O_WRONLY|);

, read(), :

   numWrite = write(outfd, buff, numChar);

, /, :

  • read(), write() -1 ()
  • , . , 2048 , .
  • write() , ( )
+3

-, sendfile() . , , -, .

chunking, TCP . node , , (, HTTP- ).

- . sendfile() , offet. recv() , , ( "recvfile()" ).

+2

All Articles