English 中文(简体)
在C部分发送TCP Socket录像的最佳方式
原标题:Best way to send video over TCP Socket in C

我正试图想出最佳办法,把录像文件发送到TCP的袖珍片上。 我已经做了一个标准袖珍方案,但在<条码>上之后。 我的指挥并不确定我如何能够拯救它。

守则样本

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

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

    //What should go here?

    close(sfdClient);
    close(sfdServer);

}

Would I just save the buffer as a file movie.mp4 or something like that? I realize I may need to change my buffer size or possibly send it in chunks. But I can t find any good info on the best way to do this. Any help or a point in the right direction would be appreciated!

最佳回答

页: 1

第一,开放一份书面产出文件:

   #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);

请注意,你需要处理一些king/bu死案件,例如:

  • Either the read() or the write() returning zero or -1 (error)
  • Reading until no more bytes are available. Right now, your code only reads 2048 bytes and then closes the socket.
  • The write() writing fewer bytes than requested (this can happen on network filesystems)
问题回答

首先,请您考虑使用sendfile(, 或相当于你的系统。 也就是说,不要把档案中一个小块放在你的记忆中,只是为了在某个地方重新书写,你应当记住——给你的档案描述员,并寄出一切。

至于king,结核病防治中心负责将溪流分解成包装。 理想的情况是,边远的点子应当确切知道有多少东西可以预期会发生对抗,以便它能够处理终止婚前问题(为什么吉大港山区长老会的头盔包含一段内容)。

因此,在发送数据之前使用某种手法。 然后,投稿人应当只打上“sendfile()”的空档,同时注意发送的数据数量和退回的<代码>offet。 接收人应当使用“recv(),以确保所有到达者(没有标准“recvfile()。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签