gpt4 book ai didi

c++ - UDT 服务器缓冲区大小?

转载 作者:行者123 更新时间:2023-11-28 08:25:25 30 4
gpt4 key购买 nike

这是我接收 UTD 消息的程序。我打算用它通过 wifi 接收 640*480 YUV 图像。我应该设置多大的缓冲区?是否可以在接收到第一张图像后设置缓冲区以找出实际大小?

下面是我的全部代码,但基本上我的问题与这一行有关:

memset(&(my_addr.sin_zero), '\0', 8);

获取第一张图片后是否可以设置。

#ifndef WIN32
#include <unistd.h>
#include <cstdlib>
#include <cstring>
#include <netdb.h>
#include <stdlib.h>
#else
#include <winsock2.h>
#include <ws2tcpip.h>
#include <wspiapi.h>
#endif
#include <iostream>
#include <udt.h>
#include "cc.h"
#include <stdio.h>
#include <time.h>

using namespace std;

int main()
{
UDTSOCKET serv = UDT::socket(AF_INET, SOCK_STREAM, 0);

sockaddr_in my_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(9000);
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);

if (UDT::ERROR == UDT::bind(serv, (sockaddr*)&my_addr, sizeof(my_addr)))
{
cout << "bind: " << UDT::getlasterror().getErrorMessage();
//return 0;
}

UDT::listen(serv, 10);

int namelen;
sockaddr_in their_addr;

char ip[16];
char data[350000];
char* temp;
FILE *log = fopen("UDT_log.txt", "at");
time_t rawtime;
struct tm * timeinfo;
int k = 0;
FILE *img;
char filename[32];

while (true)
{
UDTSOCKET recver = UDT::accept(serv, (sockaddr*)&their_addr, &namelen);

cout << "new connection: " << inet_ntoa(their_addr.sin_addr) << ":" << ntohs(their_addr.sin_port) << endl;

if (UDT::ERROR == UDT::recv(recver, data, 100, 0))
{
cout << "recv:" << UDT::getlasterror().getErrorMessage() << endl;
//return 0;
}

time ( &rawtime );
timeinfo = localtime ( &rawtime );
temp = asctime(timeinfo);
fwrite (temp, 1, strlen(temp) , log);
fwrite ("\n", 1, 1 , log);

sprintf (filename, "img%d.txt", k);
img = fopen(filename, "wb");
fwrite (data, 1, strlen(data) , img);
fclose(img);

UDT::close(recver);

k++;
}

fclose(log);

UDT::close(serv);

//return 1;
}

最佳答案

memset写入内存的一部分,它不分配它。您当然可以在收到图像后调用它,但它会产生删除从 sin_zero 地址开始的 8 字节内存的效果。

也许你的意思是malloc ,这将分配内存,但需要使用 free以防止泄漏和空检查以检测内存不足问题。

我还建议使用纯 C++,而不是您在这里使用的 C 和 C++ 函数的混合。这意味着使用 new并且可能是一个智能指针而不是 mallocfree .如果 STL 没有您需要的东西,您也可以在 Boost 库中找到有用的东西。

关于c++ - UDT 服务器缓冲区大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4232945/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com