简体   繁体   English

无法在QTcpSocket上写入数据

[英]Unable to write data on QTcpSocket

I am trying to implement a client/server application using Qt/C++, but whenever I try to write data on QTcpSocket , then the program is getting terminated. 我正在尝试使用Qt / C ++实现客户端/服务器应用程序,但是每当我尝试在QTcpSocket上写入数据时,程序就会终止。 I get the following error : 我收到以下错误:

Server started. 服务器已启动。 The program has unexpectedly finished. 该程序意外完成。

/home/pavan/qt/build-server-Desktop-Debug/server crashed. / home / pavan / qt / build-server-Desktop-Debug /服务器崩溃。

Here is my code: 这是我的代码:

void server::on_pushButton_clicked()
{

    Server=new QTcpServer(this);
    if(Server->listen(QHostAddress::Any,ui->lineEdit->text().toInt()))
        qDebug()<<"Server started";
    else
    {
        qDebug()<<"Could not start server";
        return;
    }

    QTcpSocket *socket = Server->nextPendingConnection();

    socket->write("Hii server");
    socket->flush();
    socket->waitForBytesWritten((3000));

    socket->close();
}

Thanks in advance. 提前致谢。

Please check the return value of nextPendingConnection. 请检查nextPendingConnection的返回值。 It could return 0 if case no pending connections. 如果没有挂起的连接,它将返回0。 It could lead to crash. 可能导致崩溃。 And you can try to use debugger which help u to identify and trouble shooting a lot. 而且您可以尝试使用调试器,该调试器可以帮助您识别和解决很多问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM