Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6066471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:30:16+00:00 2026-05-23T09:30:16+00:00

I am doing a simple server-client application. But, the client gets some undefined behaviour

  • 0

I am doing a simple server-client application. But, the client gets some undefined behaviour from the server side. After I retrieved the error code, I came to know that the server cut the connection.

This is server-side main.cpp

#include <QApplication>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>

class MyMessageBox:public QMessageBox
{
public:
    MyMessageBox(std::string message,QWidget *parent=0):QMessageBox(QMessageBox::NoIcon,QString("ErrorMessage"),QString(message.c_str()),QMessageBox::Ok,parent,Qt::Widget)
    {
    }
};

class My_Server:public QTcpServer
{
    Q_OBJECT
public:
    My_Server();
public slots:
    void on_Connection();
};

My_Server::My_Server():QTcpServer()
{
    connect(this,SIGNAL(newConnection()),this,SLOT(on_Connection()));
}

void My_Server::on_Connection()
{
    MyMessageBox mm("Connection Established");
    mm.exec();
    QTcpSocket * my_Socket = this->nextPendingConnection();

    my_Socket->waitForBytesWritten(30000);

    QByteArray block("Hi all");

    my_Socket->write(block);
}

int main(int argc,char * argv[])
{
    QApplication app(argc,argv);

    My_Server tcp_Server;
    tcp_Server.listen(QHostAddress("127.0.0.1"),15000);

    return app.exec();
}

#include "main.moc"

This is client-side main.cpp

#include <QApplication>
#include <QDataStream>
#include <QFile>
#include <QFileDialog>
#include <QHostAddress>
#include <QMessageBox>
#include <QtNetwork/QTcpServer>
#include <QtNetwork/QTcpSocket>

class MyMessageBox:public QMessageBox
{
public:
    MyMessageBox(std::string message,QWidget *parent=0):QMessageBox(QMessageBox::NoIcon,QString("ErrorMessage"),QString(message.c_str()),QMessageBox::Ok,parent,Qt::Widget)
    {
    }
};

int main(int argc,char * argv[])
{
    QApplication app(argc,argv);

    QTcpSocket client_Socket;

    client_Socket.connectToHost(QHostAddress("127.0.0.1"),15000);

    QDataStream in(&client_Socket);
    in.setVersion(QDataStream::Qt_4_7);

    client_Socket.waitForReadyRead(30000);

    char buf[100]={'\0'};
    client_Socket.read(buf,(quint16)sizeof(buf));
    QString nothing(buf);

    MyMessageBox mm((QString("++ ")+nothing+" ++").toStdString());
    mm.exec();

    MyMessageBox mn(QString::number(client_Socket.error()).toStdString());
    mn.exec();

    return app.exec();
}

This is the pro file( same for both)

TEMPLATE = app
TARGET = 
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += main.cpp

QT += network

I can’t find out that why the server side is cutting the connection. If anybody help me to find out the cause, I will be thankful to them.

Note: I am using Qt-4.7.2 in windows platform

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:30:16+00:00Added an answer on May 23, 2026 at 9:30 am

    The connection is getting closed because your server is exiting right after it’s sent that message.

    This is probably because you’re using QApplication, but don’t actually have a long-lasting GUI widget. So the event loop stops short once you’ve finished displaying that first dialog box.

    Try either:

    • using QCoreApplication on the server side instead. (And don’t start a message box – no GUI allowed with QCoreApplication.)
    • give your server a main widget that is responsible for starting the TCP server.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a simple WCF Server + gSOAP client proof-of-concept application using
I am trying to create a simple client/server application using Thrift which is going
I am doing simple forms authentication for a small ASP.NET (3.5, C#) application and
I am doing some simple sanity validation on various types. The current test I'm
I'm doing some simple tests (in preparation for a larger project) to call an
I'm doing some really simple math and saving the result to a MS SQL2008
I'm doing something really simple: slurping an entire text file from disk into a
I have a somewhat simple Client/Server solution running over C# remoting (System.Runtime.Remoting). The MarshalByRef
I have a pretty simple question which perhaps someone familiar with Server/Client design &
I am trying to create a simple chat client using the red5 media server,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.