gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 07:40:48 24 4
gpt4 key购买 nike

这只发送一次数据,这对我来说似乎是不合理的,因为它在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com