Esempio n. 1
0
void ClientThread::run()
{
    qDebug() << socketDescriptor << " Starting thread";
    socket = new QTcpSocket();

    if ( !socket->setSocketDescriptor(this->socketDescriptor) ) {
        emit error(socket->error());
        return;
    }


    qDebug() << socketDescriptor << " Waiting for authentication";

    user = new User(socketDescriptor, "bob", QHostAddress::LocalHost);
    socket->write("please enter your login:password\n");
    if (authenticateClient())
    {
        qDebug() << socketDescriptor << " Client authenticated";

        socket->write("authenticated\n");
        socket->write(QString(user->accessLevel).toLocal8Bit());

        emit clientAuthenticated(user);
    }
    else {
        socket->write("authentication failed\n");
        qDebug() << socketDescriptor << " Authentication failed";
        QTimer::singleShot(1, socket, SLOT(disconnectFromHostImplementation()));
    }

    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()), Qt::DirectConnection);
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()), Qt::DirectConnection);

    exec();
}
Esempio n. 2
0
void doit(int clientfd) 
{
    //
    // Code to deal with the client
    //
    char buf[MAXLINE];
    rio_t client_rio;

    puts("INFO: Preparing JARVIS to receive from the client");
    Rio_readinitb(&client_rio, clientfd);

    //
    // Code to deal with FICS
    // 
    int serverfd = 0;
    rio_t server_rio;

    puts("INFO: Preparing JARVIS to receive from FICS");
    Rio_readinitb(&server_rio, serverfd);

    puts("INFO: Opening connection to FICS...");
    serverfd = open_clientfd("freechess.org", 5000);

    if (serverfd < 0) {
        puts("ERROR: Couldn't open connection to FICS!");
        return;
    }
    else
        puts("INFO: Connection to FICS was successful!");

    puts("INFO: Authenticating with FICS using login_key.txt...");
    authenticateClient(clientfd, serverfd);

    //
    // Code to deal with FICS <-> client back and forth
    //
    int numReadFromFICS = 0;
    int numReadFromClient = 0;

    puts("INFO: Creating threads to route traffic between FICS and client...");

    pthread_t fromClientId;
    pthread_t fromFICSId;

    route clientToFICS = {clientfd, serverfd};
    route FICSToClient = {serverfd, clientfd};

    // fromFICSId thread routes traffic from FICS to Client.
    // fromClientId thread routes traffic from Client to FICS.
    pthread_create(&fromFICSId, NULL, route_to, &FICSToClient);
    pthread_create(&fromClientId, NULL, routeClientToFICS, &clientToFICS);

//    pthread_join(fromFICSId, NULL);
    pthread_join(fromClientId, NULL);

    pthread_detach(fromClientId);
    pthread_detach(fromFICSId);

    Close(serverfd);

    printf("INFO: exiting doit\n");
}