Hello Yunhong Gu,

I am using UDT for transferring data over WAN. When I am using UDT for single connection at a time. I am able to transfer data between the systems over WAN. But when I try to send data simultaneously to two machines on WAN, I receive a error message on one of the recipient machine as "CONNECTION WAS BROKEN". Can you please give some insight into this problem. One more thing if I put breakpoints in code on one of the recipient machines. The two machines are able to get data in this condition, but when I remove breakpoint from code, then only one machine receives the data. Looking for your reply.

void SendOrReceiveFile(char* szModeOfEntry, char* szRemoteIPAddress, char* szRemotePort, char* szFileToSend, char* szUniqueKey, bool bIsMulti, char* szTransferID, bool bIsPDFOrPPT)
{
    sockaddr_in peer;
    int mss = 1052;
    UDTSOCKET client = UDT::socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
    if(bIsMulti)
    {
        QString qTransferID(szTransferID);
        client = qGlobalUDTSocketHash.value(qTransferID); //This is the global hash which contains UDTSOCKET which I've stored for 2nd time onwards use.
    }

    if(client == INVALID_SOCKET)
    {
        UDT::close(client);
        return;
    }

    linger l;
    l.l_onoff = 1;
    l.l_linger = 9000;
    QString qKeyVal;
    if(!bIsMulti)
    {
        UDT::setsockopt(client, 0, UDT_LINGER, &l, sizeof(linger));

        if(strcmp(szModeOfEntry, "send") == 0)
            UDT::setsockopt(client, 0, UDT_MSS, &mss, sizeof(int));

        UDT::setsockopt(client, 0, UDT_RENDEZVOUS, new bool(true), sizeof(bool));
        QString qTransferID(szTransferID);
        SOCKET sockVal = qGlobalStunSocketHash.value(qTransferID);  //This is the global hash which contains SOCKET retured from name server which will be used for RENDEZVOUS CONNECT for the first time.
        if(UDT::ERROR == UDT::bind(client, sockVal))
        {
            UDT::close(client);
            return;
        }
    }

    peer.sin_family = AF_INET;
    peer.sin_port = htons((u_short)atoi(szRemotePort));
    peer.sin_addr.s_addr = inet_addr(szRemoteIPAddress);
    memset(&(peer.sin_zero), '\0', 8);

    if(!bIsMulti)
    {
        bool bCompleted = false;
        for(int i = 0; i < 5; i++)
        {
            if(UDT::ERROR != UDT::connect(client, (sockaddr*)&peer, sizeof(peer)))
            {
                bCompleted = true;
                break;
            }
        }

        if(bCompleted == false)
        {
            UDT::close(client);
            qGlobalStunSocketHash.remove(qKeyVal);
            return;
        }
        QString qTransferID(szTransferID);
        qGlobalUDTSocketHash.insert(qTransferID, client);
    }
    QString qKeyValue(szUniqueKey);
    qFileTransferHash.insert(qKeyValue, client);

    if(strcmp(szModeOfEntry, "send") == 0)
    {
        fstream ifs(szFileToSend, ios::in | ios::binary);
        ifs.seekg(0, ios::end);
        int64_t size = ifs.tellg();
        ifs.seekg(0, ios::beg);

        // send file size information
        if (UDT::ERROR == UDT::send(client, (char*)&size, sizeof(int64_t), 0))
        {
            if(bIsPDFOrPPT == false)
            {
                UDT::close(client);
                qGlobalStunSocketHash.remove(qKeyVal);
            }

            return;
        }
        // send the file
        if (UDT::ERROR == UDT::sendfile(client, ifs, 0, size))
        {
            if(bIsPDFOrPPT == false)
            {
                UDT::close(client);
                qGlobalStunSocketHash.remove(qKeyVal);
            }

            return;
        }

        ifs.close();
    }

    else
    {
        // get size information
        int64_t size;

        if (UDT::ERROR == UDT::recv(client, (char*)&size, sizeof(int64_t), 0))
        {
            if(bIsPDFOrPPT == false)
            {
                UDT::close(client);
                qGlobalStunSocketHash.remove(qKeyVal);
            }

            return;
        }

        // receive the file
        fstream ofs(szFileToSend, ios::out | ios::binary | ios::trunc);
        int64_t recvsize;

        if (UDT::ERROR == (recvsize = UDT::recvfile(client, ofs, 0, size)))
        {
            if(bIsPDFOrPPT == false)
            {
                UDT::close(client);
                qGlobalStunSocketHash.remove(qKeyVal);
            }
            return;
        }

        ofs.close();
    }
    if(bIsPDFOrPPT == false)
        UDT::close(client);
    qGlobalStunSocketHash.remove(qKeyVal);
}

Thanks & Best Regards-
Saurabh Srivastava