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

标签 c++ sockets tcp

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

因此对于 send() 函数来说,

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

第二个参数应该填什么?一个 1024 字节分配的发送缓冲区字符指针或一个 2000 字节的数据包字符指针,它会自动为我处理它?<​​/p>

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

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

@Giorgi,谢谢!我还想问,如果我不进行部分写入处理并且 while 循环中的发送速率非常快(没有 sleep ),那么服务器端是否会出现错误/丢失?对于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();
    }

最佳答案

If i have a packet of 2000 bytes, say 2000 'a',

你不知道。您有一条 2000 字节的消息

and my default buffer size is 1024

不太可能。至少为 8192,可能有数千 K。

then it is supposed to send twice

至少。

and received twice?

至少。

for the second parameter, what should i put?

您要发送的消息的大小:在本例中为 2000。

sending buffer character pointer with 1024 bytes mallocated

没有。

or a packet character pointer with 2000 bytes and it will handle it for me automatically ?

是的。

and for the recv() blocking function, i should put the buffer character pointer to the second parameter or the packet one?

我无法弄清楚这一点,但您应该接收到尽可能大的缓冲区并循环,直到获得完整的消息。

and for the header, my friend suggested me to add header of 4 bytes to store the packet information, eg. sequence number and size of packet, how can it be implemented!

您实际上并不需要序列号,但消息大小是一个好主意。只需将其粘贴在消息的前面即可。不清楚这里的问题是什么。

关于c++ - 关于数据包和缓冲区大小的 TCP 套接字编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33399909/

相关文章:

c++ - 内部消息循环

c++ - ld工具可以找到该库,但是mingw无法找到(linux)

c++ - Mat 的 tcp 发送缓冲区错误

sockets - Web服务器上的资源耗尽-套接字基本解释

c++ - 为什么该程序返回的是最后一个单词而不是最长的单词?

c++ - 不使用 sqrt 函数求平方根?

winapi - 调优套接字连接调用超时

java - 两个 TCP 客户端作为一个客户端?

macos - 使用 Swift 的套接字服务器示例

c - TCP 标志过滤器的十六进制、按位或反转