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 254181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:49:30+00:00 2026-05-11T21:49:30+00:00

I am trying to communicate with TCP between a Qt program and a regular

  • 0

I am trying to communicate with TCP between a Qt program and a regular linux program. I have an existing linux client server program and I am trying to replace the server program with a Qt application. Here is the linux client code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 
#include <unistd.h>

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];

    portno = 9876;

    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0) 
    {
       printf("ERROR opening socket");
       return -1;
    }
    server = gethostbyname("localhost");

    if (server == NULL) 
    {
        printf("ERROR, no such host\n");
        return -1;
    }

    memset((char *) &serv_addr,0,sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);

    connect(sockfd,(sockaddr*)&serv_addr,sizeof(serv_addr));

    sprintf(buffer,"This is a test\n");
    n = write(sockfd,buffer,256);

    return 0;
}

Here is the qt code

#include <Qt>
#include <QApplication>
#include <QTcpServer>
#include <QMessageBox>
#include <QTcpSocket>
#include <QtNetwork>

#include "qtserver.h"


Server::Server()
{
   tcp = new QTcpServer(this);
   tcp->listen(QHostAddress::Any,9876);
   QObject::connect(tcp,SIGNAL(newConnection()),this,SLOT(printline()));
}

void Server::printline()
{
   QTcpSocket *client = tcp->nextPendingConnection();
   QObject::connect(client,SIGNAL(disconnected()),
                    client,SLOT(deleteLater()));


   QDataStream in(client);
   in.setVersion(QDataStream::Qt_4_0);

   QString data;
   in >> data;
   printf("String = %s\n",(char*)data.data());
}

int main(int argc,char** argv)
{
   QApplication a(argc,argv);

   Server* server = new Server();

   return a.exec();
}

When i try to run both of them I just get “String = ” instead of the string outputting. Any idea what I’m doing wrong?

  • 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-11T21:49:30+00:00Added an answer on May 11, 2026 at 9:49 pm

    QString::data() returns QChar*, you can’t just cast it to char* and hope that it would always work. For debugging QString, use qPrintable instead.

    Also, QTcpSocket is very easy to use. Still, instead of writing the code from scratch, why not start by checkout out the examples, e.g. Fortune Server example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 205k
  • Answers 205k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A pygame Surface is a wrapper around an underlying SDL… May 12, 2026 at 8:57 pm
  • Editorial Team
    Editorial Team added an answer This is not going to be easy to do with… May 12, 2026 at 8:57 pm
  • Editorial Team
    Editorial Team added an answer You should remove this line $row = mysql_fetch_array($result); Apart from… May 12, 2026 at 8:57 pm

Related Questions

I was hoping to implement a simple XMPP server in Java. What I need
I have client and server programs which now communicate via TCP. I'm trying out
I am trying to communicate with Excel from a Java/SWT application. I have been
I am trying to get an iframe B communicate with site A. I've searched

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.