c++ - UDT::send 在 for 循环中只发送一次。没有错误

标签 c++ c udp udp-data-transfer

这只发送一次数据,这对我来说似乎是不合理的,因为它在 for 循环中:

char* hello;
for (int i = 0; i < 10; i++) {

    hello = "Hello world!\n";
    if (UDT::ERROR == UDT::send(client, hello, strlen(hello) + 1, 0))
    {
        cout << "send: " << UDT::getlasterror().getErrorMessage();
    }

}

不是应该发送10次数据吗?我不明白。这是整个程序(很短):

int main()
{
    UDTSOCKET client;
    sockaddr_in serv_addr;
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(9000);
    inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
    memset(&(serv_addr.sin_zero), '\0', 8);

    client = UDT::socket(AF_INET, SOCK_STREAM, 0);

    // connect to the server, implict bind
    if (UDT::ERROR == UDT::connect(client, (sockaddr*)&serv_addr, sizeof(serv_addr)))
    {
        cout << "connect: " << UDT::getlasterror().getErrorMessage();
    }

    char* hello;
    for (int i = 0; i < 10; i++) {

        hello = "Hello world!\n";

        if (UDT::ERROR == UDT::send(client, hello, strlen(hello) + 1, 0))
        {
            cout << "send: " << UDT::getlasterror().getErrorMessage();
        }

    }

    UDT::close(client);

    system("pause");
}

最佳答案

您检查过服务器端代码了吗?客户端在 for 循环中发送,服务器端如何接收它们?

编辑: 引用你的帖子here .如果你的服务器代码看起来像这样,它只会收到一次。服务器正在从客户端获取连接,然后再次接收,然后再次等待来自客户端的连接。但是客户端一直在循环发送。我只是猜测这是您的服务器代码,我可能是错的。

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;
  }
....................
....................
}

关于c++ - UDT::send 在 for 循环中只发送一次。没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4233179/

相关文章:

python - 通过 TCP/UDP 发送一个大数据包和许多小数据包?

c - 解释打印两个数字的 GCD 和 LCM 的程序的工作原理

node.js - 在 Express 中监听 UDP 消息

c++ - Qt QGraphicsView 或 QLabel

c++ - 强制 const 存储按值返回的值

.net - 如何将 GetType() 用于 CheckBox?

c++ - .lib 子依赖项

c - 在 C 中初始化为零的局部变量

c - 帕斯卡三角形像 C 中的星形图案

c++ - 通过 Visual C++ 通过 UDP 发送得到大量未解析的外部符号 __imp__sendto@24