所以这是我接收 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;
}