UDT::send in a for loop sends only once. No error

834 Views Asked by At

This sends data only once which seems irrational to me as it is in a for loop:

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();
    }

}

Shouldn't it send data 10 times? I don't understand it. Here is the whole program (it's quite short):

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");
}
3

There are 3 best solutions below

1
On BEST ANSWER

Have you checked the server side code. Client is doing sends in a for loop, how is the server side receiving them?

Edit: In reference to your post here. If your server code looks like this, it will receive only once. The server is getting the connection from the client then receives once then again waits for connection from a client. But the client keeps sending in a loop. Its just a guess that this is your server code, I could be wrong.

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

Client and server structure doesn't match.

Client sends 10 times inside 1 connection. Server accepts 10 connections and reads only 1 time inside each one.

Obviously, each connection is simmilar to a container. Data sent but not read after closing a connection is discarded and (of course) not accessible from other connections.

2
On

Does it go through the loop 10 times, or only go through the loop once?

I'm not familiar with the library you're using, but I didn't see any kind of flush command. If it goes through the loop 10 times but only sends data once, you might need to flush.