当前位置: 首页>>代码示例>>C++>>正文


C++ Join::setNickname方法代码示例

本文整理汇总了C++中Join::setNickname方法的典型用法代码示例。如果您正苦于以下问题:C++ Join::setNickname方法的具体用法?C++ Join::setNickname怎么用?C++ Join::setNickname使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Join的用法示例。


在下文中一共展示了Join::setNickname方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: on_joinButton_clicked

void ChatClient::on_joinButton_clicked()
{
    Join *j;
    Confirm *c;
    INet4Address *servaddr;
    Connection *myConnection;
    char* buffer; //for encoded messages
    int bytesToSend;
    int bytesToReceive;
    int bytesReceived;

    //Get the strings out of the address, port and nickname
    //boxes and convert to strings;
    QString qip = ui.addresLine->text();
    QString qport = ui.portLine->text();
    QString qnick = ui.nicknameLine->text();

    //Create new connection objects
    servaddr = new INet4Address(qip.toLatin1().data(),qport.toInt());
    myConnection = new Connection(servaddr);

    //Connect to server

    if(myConnection->createSocket() < 0)
    {
        printf("Socket Error\n");
        return;
    }

    if(myConnection->connectSocket() < 0)
    {
        printf("Connect Error\n");
        delete(myConnection);
        delete(servaddr);
        return;
    }

    //Make a join message
    j = new Join();
    j->setNickname(qnick.toLatin1().data());

    //Send join message (as bytes)
    bytesToSend = j->encode(&buffer);

    printf("Buffer: %s, Nick: %s ",buffer, j->getNickname());

    myConnection->writeSize(bytesToSend);
    myConnection->writen(buffer,bytesToSend);

    delete(buffer);

    //Read response - take action!
    //Read byte count
    bytesToReceive = myConnection->readSize();
    buffer = new char[bytesToReceive];

    //Read message
    bytesReceived=myConnection->readn(buffer,bytesToReceive);

    if(bytesReceived < bytesToReceive)
        printf("Server response truncated!");

    //Convert to message (confirm)
    c = new Confirm();
    c->decode(buffer,bytesToReceive);
    //Destroy socket
    myConnection->closeSocket();

    //check if nick accepted
    if(c->getResponse()==true)
    {
        nickAccepted = true;
        ui.addresLine->setEnabled(false);
        ui.portLine->setEnabled(false);
        ui.nicknameLine->setEnabled(false);
        ui.joinButton->setEnabled(false);

        //Turn on the timer
        timer->start();
    }
    else
    {
        nickAccepted = false;
        ui.nicknameLine->setText(tr("Rejected!"));
    }


    delete myConnection;
    delete servaddr;
}
开发者ID:huwdp,项目名称:Chat-Client-and-Server,代码行数:90,代码来源:chatclient.cpp


注:本文中的Join::setNickname方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。