//---------------------------------------------------------------------------
//  FUNCTION: BOOL DoRnrServer(char * pszServerName, int  nServerType)
//
//  PURPOSE:  The driver for the server side code. It forms the global
//            class id "g_MyGuid" for this service class, installs this
//            service class, advertises the service name "pszServerName" with 
//            the associated bound SOCKADDR addresses and then loop through 
//            for data sent by client. This routine returns if user hits "Ctrl-C" 
//            or any error encountered.
//
//  RETURNS:
//    TRUE if user hits "Ctrl-C" to end the server program.
//    FALSE if there is any error encountered.
//
//---------------------------------------------------------------------------
BOOL DoRnrServer(char * pszServerName, int  nServerType)
{
    // I am using SVCID_NETWARE macro to form a common class id for the
    // client and server program such that the client program can also query 
    // some known SAP services on the network without running this program
    // as a server.
    // You can also generate your own class id by uuidgen.exe or guidgen.exe and
    // distribute the class id to client program.
    //
    // Please check svcguid.h for details about the macro.
    GUID guidNW = SVCID_NETWARE(nServerType); 

    memcpy(&g_MyGuid, &guidNW, sizeof(GUID));

    if (!SetConsoleCtrlHandler(CtrlHandler, TRUE))
    {
        printf ("SetConsoleCtrlHandler failed to install console handler: %d\n", 
                GetLastError());
        return FALSE;
    }
    // Install the server class
    if (!InstallClass(nServerType))
    {
        return FALSE;
    }
    // advertise the server instance
    if (!Advertise(pszServerName))
    {
        return FALSE;
    }

    // Make our bound sockets non-blocking such that
    // we can loop and test for data sent by client
    // without blocking.
    u_long arg = 1L;
    for (int i = 0; i < g_nNumOfUsedSocks; i++)
    {
        // make the socket as non-blocking socket
        if (ioctlsocket(g_aSock[i], FIONBIO, &arg) == SOCKET_ERROR)
        {
            printf("ioctlsocket[%d] error %d\n", i, WSAGetLastError());
            return FALSE;
        }
    }

    // receive data from client who find our address thru Winsock 2 RnR
    for (;;)
    {
        if (ServerRecv() == FALSE)
        {
            return FALSE;
        }
        if (fEndProgram)
            return TRUE;
        Sleep(100);
    }
}
Exemple #2
0
void Pump::ClientRecv(const QByteArray &Data) {
    switch (status) {
        case Initiated: {
            QVariantMap qvm(QJsonDocument::fromJson(Data).toVariant().toMap()),qvm2;
            if ((!Version::CheckVersion(qvm["version"].toString()))||qvm["password"]!=Password) {
                qvm2.insert("status","no");
                emit csock->SendData(QJsonDocument::fromVariant(qvm2).toJson());
                csock->disconnectFromHost();
                Log::log(csock,"was refused");
                break;
            }
            if (qvm["protocol"]=="TCP") {
                ssock=new TcpSocket(this);
                connect(ssock,SIGNAL(RecvData(QByteArray)),this,SLOT(ServerRecv(QByteArray)));
                connect(ssock,SIGNAL(disconnected()),this,SLOT(EndSession()));
                ssock->connectToHost(qvm["host"].toString(),qvm["port"].toUInt());
                status=TCP;
                Log::log(csock,"requested TCP connection to "+qvm["host"].toString()+':'+QString::number(qvm["port"].toUInt()));
            } else if (qvm["protocol"]=="UDP") {
                usock=new UdpSocket(this);
                connect(usock,SIGNAL(RecvData(QHostAddress,unsigned short,QByteArray)),this,SLOT(UDPRecv(QHostAddress,unsigned short,QByteArray)));
                status=UDP;
                Log::log(csock,"requested UDP association");
            }
            qvm2.insert("status","ok");
            qvm2.insert("protocol",qvm["protocol"].toString());
            qvm2.insert("garbage",QString(randombytes_uniform(900),'f'));
            emit csock->SendData(QJsonDocument::fromVariant(qvm2).toJson());
            break;
        }
        case TCP:
            emit ssock->SendData(Data);
            break;
        case UDP: {
            QVariantMap qvm(QJsonDocument::fromJson(Data).toVariant().toMap());
            emit usock->SendData(qvm["host"].toString(),qvm["port"].toUInt(),QByteArray::fromBase64(qvm["data"].toByteArray()));
            Log::log(csock,"sent a UDP package to "+qvm["host"].toString()+':'+QString::number(qvm["port"].toUInt()));
        }
    }