Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Are Qt Tcp sockets and Visual studio Sockets compatible??
Forum Update on Monday, May 27th 2025

Are Qt Tcp sockets and Visual studio Sockets compatible??

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 4 Posters 3.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    priya reddy
    wrote on last edited by
    #1

    I am trying to connect client from VS and Server from Qt , but the communication is unable to establish. But the individual Communication is working fine.

    I am working on QT c++ Project in Ubuntu . My Project needs to get the data from Sdk which runs only in Visual studio express 2012 windows. I am trying to establish communication between VS windows and QT Ubuntu via TCP/IP protocol. But it is not working.

    Are they not Compatible ?? Qt TCP Sockets and Visual Studio TCP Socket....

    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,
    hints;
    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);
        WSACleanup();
        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, 
            ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET) {
            printf("socket failed with error: %ld\n", WSAGetLastError());
            WSACleanup();
            return 1;
        }
    
        // Connect to server.
        iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
    }
    
    freeaddrinfo(result);
    
    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        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());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }
    
    printf("Bytes Sent: %ld\n", iResult);
    closesocket(ConnectSocket);
    WSACleanup();
    
    return 0;
    }
    

    Server code from QT creator 4

    server.h
    
    #include <QtNetwork>
    #include <QObject>
    #include <QTcpServer>
    #include <QTcpSocket>
    
    class Server: public QObject
    {
    Q_OBJECT
    public:
    Server(QObject * parent = 0);
    ~Server();
    public slots:
    void acceptConnection();
    void startRead();
    
    //void Server::sendData(QTcpSocket* client, QString data);
    
    private:
    QTcpServer server;
    QTcpSocket* client;
    };
    
    main.cpp
    
    #include "server.h"
    #include <qcoreapplication.h>
    
    int main(int argc, char** argv)
    {
    QCoreApplication app(argc, argv);
    Server server;
    return app.exec();
    }
    
    server.cpp
    
    #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);
    }
    
    Server::~Server()
    {
    server.close();
    }
    
    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;
    client->close();
    }
    
    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      It should work. What is the error you receive when you try to connect from the VC client to Qt Server ? Error should be able to identify the actual cause.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      P 1 Reply Last reply
      0
      • dheerendraD dheerendra

        It should work. What is the error you receive when you try to connect from the VC client to Qt Server ? Error should be able to identify the actual cause.

        P Offline
        P Offline
        priya reddy
        wrote on last edited by
        #3

        The server application running fine , but The client showing "Unable to connect to server!" , the one i have mentioned in code when it unable to find server.

        @dheerendra

        Ni.SumiN 1 Reply Last reply
        0
        • P priya reddy

          The server application running fine , but The client showing "Unable to connect to server!" , the one i have mentioned in code when it unable to find server.

          @dheerendra

          Ni.SumiN Offline
          Ni.SumiN Offline
          Ni.Sumi
          wrote on last edited by Ni.Sumi
          #4

          @priya-reddy

          I guess, it is Port number issue. Check the port numbers.

          Did you check with getaddrinfo , before connect().

          P 1 Reply Last reply
          2
          • JohanSoloJ Offline
            JohanSoloJ Offline
            JohanSolo
            wrote on last edited by
            #5

            Just a wild guess, but can your firewall block the data? It just happens to me all the time...

            `They did not know it was impossible, so they did it.'
            -- Mark Twain

            1 Reply Last reply
            3
            • Ni.SumiN Ni.Sumi

              @priya-reddy

              I guess, it is Port number issue. Check the port numbers.

              Did you check with getaddrinfo , before connect().

              P Offline
              P Offline
              priya reddy
              wrote on last edited by priya reddy
              #6

              @Ni.Sumi

              No, ports numbers are good. And I have disabled the firewall settings in both operating systems. The getaddrinfo is included in the client application code.
              Actually My ubuntu operating systems is in Virtual machine, not on real system. Does it effect the outside transmission??

              Ni.SumiN 1 Reply Last reply
              0
              • P priya reddy

                @Ni.Sumi

                No, ports numbers are good. And I have disabled the firewall settings in both operating systems. The getaddrinfo is included in the client application code.
                Actually My ubuntu operating systems is in Virtual machine, not on real system. Does it effect the outside transmission??

                Ni.SumiN Offline
                Ni.SumiN Offline
                Ni.Sumi
                wrote on last edited by Ni.Sumi
                #7

                @priya-reddy

                Normally,. it is allowed to communicate VM with outside OS . Also please check your VM NetWork settings.

                Is your getaddrinfo is the same , what are you looking to connect from the server application?

                Edit::

                I prefer to use some tool to be sure that your unbuntu is receiving the data or not. Some thing like wireshark tool / using network commands. Also few debug checks in the Qt. Try to connect and get some info like google.com , to be sure it has no issues-

                Other than your debug message, Do you habe any error message?

                1 Reply Last reply
                1

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved