开发者

UDT server buffer size?

So here is my program for receiving UTD messages. I am planning to use it to receive 640*480 YUV images over wifi. How big buffer should I set? Is it possible to set buffer after receiving first image to find out the actual size?

Bellow is my whole code but basically my question is related to this line:

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

And whether it can be set after getting first image.

#ifndef W开发者_开发知识库IN32
   #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 writes to a portion of memory, it doesn't allocate it. You can certainly call it after receiving an image, but it will have the effect of wiping 8 bytes of memory starting at the address of sin_zero.

Perhaps you mean malloc, which would allocate memory, but requires use of free to prevent leaks and null-checks to detect out-of-memory issues.

I would suggest also using pure C++ rather than the mix of C and C++ functions you are using here. This would mean using new and probably a smart pointer instead of malloc and free. You may also find something of use in the Boost libraries if the stl doesn't have what you need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜