Important: Please read the Qt Code of Conduct -

TCP IP communication

  • I am working on a project with Qt C++ ubuntu. My project need to access the data from Visual studio express 2012 win32 console application project. Is it possible to transmit the data from VS to Qt by using TCPIP protocol ??

    Thanks in advance.

  • Lifetime Qt Champion

  • thank you for the reply. So in this case By creating client application in visual studio to transmit the data and server application the QT linux (Ubuntu). Would The data be successfully transmitted right??

  • Lifetime Qt Champion

    Yes it should transfer fine.

    Notice, that between platform there can be issues with Endianness
    ( Linux <-> Windows often is not affected)

    Also some of the Qt class have a header that the other side must skip.

    So there is some small details but overall it should just work.

  • Client code from Visual studio express 2012

    #define WIN32_LEAN_AND_MEAN

    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>

    // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Mswsock.lib")
    #pragma comment (lib, "AdvApi32.lib")

    #define DEFAULT_BUFLEN 512
    #define DEFAULT_PORT "27015"

    int __cdecl main(int argc, char **argv)
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,
    *ptr = NULL,
    char *sendbuf = "this is a test";
    char recvbuf[DEFAULT_BUFLEN];
    int iResult;
    int recvbuflen = DEFAULT_BUFLEN;

    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s server-name\n", argv[0]);
        return 1;
    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed with error: %d\n", iResult);
        return 1;
    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    // Resolve the server address and port
    iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed with error: %d\n", iResult);
        return 1;
    // Attempt to connect to an address until one succeeds
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {
        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, 
        if (ConnectSocket == INVALID_SOCKET) {
            printf("socket failed with error: %ld\n", WSAGetLastError());
            return 1;
        // Connect to server.
        iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            ConnectSocket = INVALID_SOCKET;
    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        return 1;
    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed with error: %d\n", WSAGetLastError());
        return 1;
    printf("Bytes Sent: %ld\n", iResult);


    return 0;


    Server code from QT creator 4


    #include <QtNetwork>
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>

    class Server: public QObject
    Server(QObject * parent = 0);
    public slots:
    void acceptConnection();
    void startRead();

    //void Server::sendData(QTcpSocket* client, QString data);

    QTcpServer server;
    QTcpSocket* client;


    #include "server.h"
    #include <qcoreapplication.h>

    int main(int argc, char** argv)
    QCoreApplication app(argc, argv);
    Server server;
    return app.exec();


    #include "server.h"
    #include <iostream>
    using namespace std;

    Server::Server(QObject* parent): QObject(parent)
    connect(&server, SIGNAL(newConnection()),
    this, SLOT(acceptConnection()));

    server.listen(QHostAddress::Any, 27015);


    void Server::acceptConnection()
    client = server.nextPendingConnection();

    connect(client, SIGNAL(readyRead()),
    this, SLOT(startRead()));

    void Server::startRead()
    char buffer[512] = {0};
    client->read(buffer, client->bytesAvailable());
    cout << buffer << endl;

    Both codes are working individually within their IDEs . But to build communication between VS and QT it is not working. May the both sockets are not compatible. Could you please check the code and help me out to find the mistake I have done.

Log in to reply