首页 文章

关于数据包和缓冲区大小的TCP套接字编程

提问于
浏览
2

我知道TCP中没有数据包概念,因为它是一个流套接字,然后例如,如果我有一个2000字节的数据包,比如2000'a',我的默认缓冲区大小是1024,那么它应该发送两次并收到两次?

所以对于send()函数,

iResult = send(s, sendbuf, packet_size, 0);

对于第二个参数,我应该放什么?一个1024字节mallocated的发送缓冲区字符指针或一个2000字节的数据包字符指针,它将自动处理它?

对于recv()阻塞函数,我应该将缓冲区字符指针放到第二个参数或包中吗?

对于 Headers ,我的朋友建议我添加4个字节的 Headers 来存储数据包信息,例如 . 数据包的序列号和大小,如何实现?谢谢

@Giorgi,谢谢!而且我也想问一下,如果我不进行部分写入处理并且while循环中的发送速率非常快(没有睡眠),那么服务器端会出现错误/丢失吗?对于recv()

SOCKET newsfd;
bind(s, (struct sockaddr *)ReceiverSocket, sizeof(struct sockaddr_in));
if (strcmp(protocol, "tcp") == 0 || strcmp(protocol, "TCP") == 0){
    listen(s, 1);
    newsfd = accept(s, 0, 0);
}

//*** Create Update Display Thread
std::thread th(Function_packet_transmission_display, update_interval, (char*) "recv");

//*** Receive Data//*** Set Jitter
long time_old = 0, time_new = 0, time_start = 0;
long float jitter_new = 0, jitter_old = 0;
long long temp_accubyte = 0, temp_pktnum = 0; //testing
char *recvbuf = new char[buffer_size];
long long next_seq_num = 1; int retVal;
do{

    if (strcmp(protocol, "tcp") == 0 || strcmp(protocol, "TCP") == 0){
        retVal = recv(newsfd, recvbuf, packet_size, 0);
        if ((retVal == SOCKET_ERROR) || (retVal == 0)){
            printf("\nreturn fail code:%i\n", WSAGetLastError());
            closesocket(s);
            WSACleanup();
            lck.lock();
            Ended = true;
            lck.unlock();
            return 0;
        }
    }
    else if (strcmp(protocol, "udp") == 0 || strcmp(protocol, "UDP") == 0){
        int fromlen = (int)sizeof(struct sockaddr_in);
        retVal = recvfrom(s, recvbuf, packet_size, 0, (struct sockaddr *)ReceiverSocket, &fromlen);
    }
    //testing
    temp_accubyte += retVal;
    temp_pktnum++;//TEST
    //printf("\racc: %lld %lld    -   ", temp_accubyte, temp_pktnum);
    //if (temp_pktnum==100000)            printf("\nReach 100000\n", temp_accubyte, temp_pktnum);

    if (timer == NULL){
        timer = new ES_FlashTimer();
    }

2 回答

  • 3

    如果我有一个2000字节的数据包,比如2000'a',

    你没有 . 你有一个2000字节的消息 .

    和我的默认缓冲区大小是1024

    不太可能 . 它将至少为8192,可能是数千K.

    然后它应该发送两次

    至少 .

    并收到两次?

    至少 .

    对于第二个参数,我应该放什么?

    要发送的邮件大小:在本例中为2000 .

    发送1024字节mallocated的缓冲区字符指针

    没有 .

    或一个2000字节的数据包字符指针,它将自动处理它?

    是 .

    对于recv()阻塞函数,我应该将缓冲区字符指针放到第二个参数或数据包一个?

    我不能做这个的头或尾,但你应该收到尽可能大的缓冲区并循环,直到你有整个消息 .

    对于 Headers ,我的朋友建议我添加4个字节的 Headers 来存储数据包信息,例如 . 数据包的序列号和大小,如何实现!

    您不需要序列号,但消息大小是个好主意 . 只需将其贴在邮件的前面即可 . 不清楚这里的问题是什么 .

  • 2

    你需要像here这样的sendall,你(可能)需要写一个类似的 receivall . 这是 sendall (稍加修改):

    #include <sys/types.h>
    #include <sys/socket.h>
    
    int sendall(int s, char *buf, int *len)
    {
        int total = 0;        // how many bytes we've sent
        int bytesleft = *len; // how many we have left to send
        int n = -1;
    
        while(total < *len) {
            n = send(s, buf+total, bytesleft, 0);
            if (n <= 0) {  break; }
            total += n;
            bytesleft -= n;
        }
    
        *len = total;  
    
        return (n<=0)?-1:0; // return -1 on failure, 0 on success
    }
    

    你可以在这里阅读如何处理message framing,这就是你需要的 .

相关问题